Lab 4: Looping in Control Structure in C – C Programming Lab Report

Lab 4: Looping in Control Structure in C

A C Programming Lab Report for 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 or switch 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:

  1. Start
  2. Declare variables i, j, k, r. Set r to the number of rows.
  3. Outer loop for rows (i from 0 to r-1).
  4. Inner loop 1 for spaces (j from 0 to r-i-2) to print leading spaces.
  5. Inner loop 2 for asterisks (k from 0 to 2*i) to print stars.
  6. Print a newline character after the inner loops.
  7. End outer loop.
  8. Stop.

Flowchart:

Start
Declare i, j, k, r
Set r = 4
i = 0
i < r?
True
j = 0
j < (r-i-1)?
True
Print " "
j++
False
k = 0
k < (2*i+1)?
True
Print "*"
k++
False
Print "\n"
i++
False
Stop

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:

  1. Start
  2. Declare variables i, j, r. Set r to the number of rows.
  3. Outer loop for rows (i from 1 to r).
  4. Inner loop for columns (j from 1 to i).
  5. Inside the inner loop, print the value of j.
  6. After the inner loop, print a newline.
  7. End outer loop.
  8. Stop.

Flowchart:

Start
Declare i, j, r
Set r = 4
i = 1
i <= r?
True
j = 1
j <= i?
True
Print j
j++
False
Print "\n"
i++
False
Stop

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:

1 2 3 4

Algorithm:

  1. Start
  2. Start a for loop from i = 1 to 10.
  3. Inside the loop, check if i is equal to 5.
  4. If true, execute `break` to terminate the loop.
  5. If false, print the value of i.
  6. End loop.
  7. Stop.

Flowchart:

Start
i = 1
i <= 10?
True
i == 5?
True
break
False
Print i
i++
False
Stop

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:

Enter a number (1-7): 2
MONDAY

Algorithm:

  1. Start
  2. Read an integer n.
  3. Use a switch statement on n.
  4. For case 1, print "SUNDAY".
  5. For case 2, print "MONDAY", and so on for all 7 days.
  6. For the default case, print "INVALID DATE".
  7. Stop.

Flowchart:

Start
Read n
n = ?
1
Print "SUNDAY"
2
Print "MONDAY"
3
Print "TUESDAY"
4
Print "WEDNESDAY"
5
Print "THURSDAY"
6
Print "FRIDAY"
7
Print "SATURDAY"
default
Print "INVALID DATE"
Stop

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:

1 3 5 7 9

Algorithm:

  1. Start
  2. Start a for loop from i = 1 to 10.
  3. Inside the loop, check if i is an even number (i % 2 == 0).
  4. If true, execute `continue` to skip to the next iteration.
  5. If false, print the value of i.
  6. End loop.
  7. Stop.

Flowchart:

Start
i = 1
i <= 10?
True
i % 2 == 0?
True
continue
i++
False
Print i
i++
False
Stop

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.

Scroll to Top