Cse115 Lab 8
Cse115 Lab 8
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.
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 :
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.
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: