0% found this document useful (0 votes)
1 views4 pages

Cse115 Lab 8

The document outlines Lab 8 for CSE 115L at North South University, focusing on nested loops and conditional execution in programming. It includes objectives such as understanding nested loops, optimizing code readability, and enhancing code efficiency, along with examples and practice problems. Additionally, it provides classwork tasks for students to implement various patterns using nested loops in C programming.

Uploaded by

astronautst42
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views4 pages

Cse115 Lab 8

The document outlines Lab 8 for CSE 115L at North South University, focusing on nested loops and conditional execution in programming. It includes objectives such as understanding nested loops, optimizing code readability, and enhancing code efficiency, along with examples and practice problems. Additionally, it provides classwork tasks for students to implement various patterns using nested loops in C programming.

Uploaded by

astronautst42
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

North South University

Department of Electrical and Computer Engineering


CSE 115L (Programming Language I Lab)
Lab 8: Nested Loop
Instructor: Samiha Islam

Objective:
● Understand Conditional Execution: Learn how to control program flow based on
multiple conditions using nested if-else statements.
● Implement Multi-level Decision Making: Develop the ability to make hierarchical
decisions where one condition depends on another.
● Optimize Code Readability: Learn best practices to minimize deep nesting and improve
code clarity.
● Understand Nested Loop Statements: Learn how nested loop work for various problems.
● Enhance Code Efficiency: Use nested loop for optimized execution of loops.

What is a Nested Loop?


A nested for loop is a control flow statement where one for loop is placed inside another
for loop. The inner loop executes completely for each iteration of the outer loop. This
structure is commonly used for iterating over multi-dimensional data structures, printing
patterns, and solving computational problems efficiently.

Components:

1. Initialization: This part is executed only once before the loop starts. It is typically
used to initialize a loop control variable.
2. Condition: This is evaluated before each iteration. If it evaluates to true (non-
zero), the loop executes; otherwise, it stops.
3. Update: This runs after each iteration and updates the loop control variable.
Example :

#include <stdio.h> #include <stdio.h>

int main() { int main() {


int n = 3; // Number of rows int n = 3; // Number of rows
int row, column; int row, column;

for (row = 1; row <= n; row++) { // for (row = 1; row <= n; row++) { //
Outer loop for rows Outer loop for rows
for (column = 1; column <= row; for (column = 1; column <= row;
column++) { // Inner loop for columns column++) { // Inner loop for columns
printf("%d ", column); printf("%d ", row);
} }
printf("\n"); // Move to the next printf("\n"); // Move to the next
line line
} }

return 0; return 0;
} }
Practice Problem :

The same above problem in reverse The same above problem in reverse order.
order.

#include <stdio.h> #include <stdio.h>

int main() { int main() {


int n = 3; // Number of rows int n = 3; // Number of rows
int row, column; int row, column;

for (row = n; row >= 1; row--) { // for (row = n; row >= 1; row--) { // Outer loop
Outer loop for rows (reverse order) for rows (reverse order)
for (column = 1; column <= row; for (column = 1; column <= row;
column++) { // Inner loop for columns column++) { // Inner loop for columns
printf("%d ", column); printf("%d ", row);
} }
printf("\n"); // Move to the next printf("\n"); // Move to the next line
line }
}
return 0;
return 0; }
}
Classwork:

1. Write down a C program to print the following pattern where number of


rows will be taken as input from user.
Sample Output:

2. Write down a C program to print the following pattern where number of


rows will be taken as input from user.
Sample Output:

3. Write down a C program to print the following pattern where number of


rows will be taken as input from user.
Sample Output:

No homework for today.

You might also like