Lab 4: Looping in Control Structure in C

Lab Report Information
Lab No.: 4
Title: Looping in Control Structure in C
Course: Computer Programming (CT 101), I Year I Part
Credit: Important Notes
C Programming Lab Report: Theory for Lab 4
Advanced Control Structures in C
This C Programming Lab Report focuses on advanced control flows, including nested loops for pattern generation and jump statements for altering loop behavior.
Nested Loops
A nested loop is a loop inside another loop. This is essential for tasks like processing 2D arrays or creating complex patterns, as the inner loop completes all its iterations for each single iteration of the outer loop.
Switch Statement
The switch
statement is a multi-way branch that tests a variable against a list of constant values (cases). It is a clean alternative to a long if-else-if
ladder.
Jump Statements
break
: Immediately terminates the innermost loop orswitch
statement.continue
: Skips the rest of the current loop iteration and proceeds to the next one.
C Programming Lab Report: Programs for Lab 4
1. Print a Full Pyramid Pattern
Question:
Write a C program to display a full pyramid of asterisks.
Program Code:
#include <stdio.h> #include <conio.h> void main() { int i, j, k, r = 4; clrscr(); for (i = 0; i < r; i++) { for (j = 0; j < (r - i - 1); j++) { printf(" "); } for (k = 0; k < (2 * i + 1); k++) { printf("*"); } printf("\n"); } getch(); }
Output:
* *** ***** *******
Algorithm:
- Start
- Declare variables i, j, k, r. Set r to the number of rows.
- Outer loop for rows (i from 0 to r-1).
- Inner loop 1 for spaces (j from 0 to r-i-2) to print leading spaces.
- Inner loop 2 for asterisks (k from 0 to 2*i) to print stars.
- Print a newline character after the inner loops.
- End outer loop.
- Stop.
Flowchart:
Set r = 4
2. Print a Half Pyramid with Numbers
Question:
Write a C program to display a half pyramid pattern using numbers.
Program Code:
#include <stdio.h> #include <conio.h> void main() { int i, j, r = 4; clrscr(); for (i = 1; i <= r; i++) { for (j = 1; j <= i; j++) { printf("%d ", j); } printf("\n"); } getch(); }
Output:
1 1 2 1 2 3 1 2 3 4
Algorithm:
- Start
- Declare variables i, j, r. Set r to the number of rows.
- Outer loop for rows (i from 1 to r).
- Inner loop for columns (j from 1 to i).
- Inside the inner loop, print the value of j.
- After the inner loop, print a newline.
- End outer loop.
- Stop.
Flowchart:
Set r = 4
3. Demonstrate the break Statement
Question:
Write a C program to demonstrate the use of the `break` statement.
Program Code:
#include <stdio.h> #include <conio.h> void main() { int i; clrscr(); for (i = 1; i <= 10; i++) { if (i == 5) { break; // Exit the loop } printf("%d ", i); } getch(); }
Output:
Algorithm:
- Start
- Start a for loop from i = 1 to 10.
- Inside the loop, check if i is equal to 5.
- If true, execute `break` to terminate the loop.
- If false, print the value of i.
- End loop.
- Stop.
Flowchart:
4. Demonstrate the switch Statement
Question:
Write a C program to take an integer from 1 to 7 and print the corresponding day of the week.
Program Code:
#include <stdio.h> #include <conio.h> void main() { int n; clrscr(); printf("Enter a number (1-7): "); scanf("%d", &n); switch (n) { case 1: printf("\nSUNDAY"); break; case 2: printf("\nMONDAY"); break; case 3: printf("\nTUESDAY"); break; case 4: printf("\nWEDNESDAY"); break; case 5: printf("\nTHURSDAY"); break; case 6: printf("\nFRIDAY"); break; case 7: printf("\nSATURDAY"); break; default: printf("\nINVALID DATE"); } getch(); }
Output:
MONDAY
Algorithm:
- Start
- Read an integer n.
- Use a switch statement on n.
- For case 1, print "SUNDAY".
- For case 2, print "MONDAY", and so on for all 7 days.
- For the default case, print "INVALID DATE".
- Stop.
Flowchart:
5. Demonstrate the continue Statement
Question:
Write a C program to demonstrate the `continue` statement by printing all odd numbers from 1 to 10.
Program Code:
#include <stdio.h> #include <conio.h> void main() { int i; clrscr(); for (i = 1; i <= 10; i++) { if (i % 2 == 0) { continue; // Skip even numbers } printf("%d ", i); } getch(); }
Output:
Algorithm:
- Start
- Start a for loop from i = 1 to 10.
- Inside the loop, check if i is an even number (i % 2 == 0).
- If true, execute `continue` to skip to the next iteration.
- If false, print the value of i.
- End loop.
- Stop.
Flowchart:
Discussion and Conclusion
This C Programming Lab Report on Looping in Control Structure in C successfully demonstrates the use of advanced loop control and branching. The exercises involving nested loops were crucial for understanding how to generate complex patterns by controlling inner and outer iterations independently. Furthermore, the practical application of jump statements (`break` and `continue`) and the `switch` statement illustrated powerful techniques for managing program flow with more clarity and efficiency than simple `if-else` structures alone. This lab provides a solid foundation for building more complex algorithms that require precise control over repetition and decision-making.