Course Content
Detailed Content of Programming in C
0/1
Introduction
0/1
Structure of C program
0/1
Answers of ‘C’ Pointers Programs
0/1
About Lesson

A loop contains set of statements which are executed again and again in a program till a condition is true. For Constructing a loop three steps must be performed. The first step is initialization of Conditions for repetition of loop, second step is increment, decrement or change of value for the identifier used for condition check and the third step is to check the condition for loop.

To explain more accurately we use same program example in while, do while and for loop:

1. While Loop:

While loop is used when a set of statement are to be executed again and again. The set of statements written in while will be executed until condition is true. If condition is false at the short, then while loop will not be executed.

FlowChart:

Syntax:

Initialize

while(expression)

{

………………

//set of statements

…………….

Increment/decrement

}

Example:

#include<stdio.h>
#include<conio.h>
void main()
{
  int num, x=0, y=0,r=0;
  printf(“nenter a number”);
  scanf(“%d”,&num); //123
  while(num>0) // 123>0=>T, 12>0=>T , 1>0=>T, 0>0=>F
  {
    x++;  // 1, 2 , 3
    y=y+num%10;
    r=r*10+num%10;
    num=num/10; // 123/10=>12, 12/10=>1 , 1/10=>0
  }
  printf(“nDigits=%d”,x);
  printf(“nSum of Digits =%d”,y);
  printf(“nReverse of a number is %d”,r);
  getch();
}

Explanation of  Program:

  1. Header Files:

    • The program includes two header files: stdio.h for standard input and output functions and conio.h for console input and output functions. Note that the use of conio.his non-standard and might not be supported by all compilers.
  2. Main Function:

    • The main function is the entry point of the program.
  3. Variables:

    • num: Holds the input number.
    • x: Counts the number of digits in the input.
    • y: Stores the sum of the digits.
    • r: Stores the reverse of the number.
  4. Input:

    • The user is prompted to enter a number using printf and the input is read using scanf.
  5. While Loop:

    • The program enters a while loop that continues as long as num is greater than 0.
    • In each iteration:
      • x is incremented, counting the number of digits.
      • y is updated by adding the last digit of num (obtained using % 10).
      • r is updated to create the reverse of the number.
      • num is updated by removing the last digit (using / 10).
  6. Output:

    • The program prints the number of digits, the sum of the digits, and the reverse of the number using printf.
  7. getch():

    • This function waits for a key press before the program exits. It’s part of the conio.h library.

Overall, this program calculates and displays the number of digits, the sum of digits, and the reverse of a given number entered by the user.

 

2. do While Loop:

do While loop is somewhat similar to while loop, but in do while condition is checked after execution of loop. It is used when number of times set of statements in loop to be executed are not known in advance. It is used when loop is to be executed at least once.

FlowChart:

 

Syntax:

Initialize

do{

………………

//set of statements

…………….

Increment/decrement

}while(condition)

Example:

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

void main()
{
    int num, x = 0, y = 0, r = 0;

    printf(“nEnter a number: “);
    scanf(“%d”, &num); // 123

    do {
        x++;  // 1, 2, 3
        y = y + num % 10;
        r = r * 10 + num % 10;
        num = num / 10; // 123/10=>12, 12/10=>1 , 1/10=>0
    } while (num > 0); // 123>0=>T, 12>0=>T , 1>0=>T, 0>0=>F

    printf(“nDigits = %d”, x);
    printf(“nSum of Digits = %d”, y);
    printf(“nReverse of the number is %d”, r);

    getch();
}

Explanation of  Program:

    1. Header Files:

      • The program includes two header files: stdio.h for standard input and output functions and conio.h for console input and output functions. Note that the use of conio.h is non-standard and might not be supported by all compilers.
    2. Main Function:

      • The main function is the entry point of the program.
    3. Variables:

      • num: Holds the input number.
      • x: Counts the number of digits in the input.
      • y: Stores the sum of the digits.
      • r: Stores the reverse of the number.
    4. Input:

      • The user is prompted to enter a number using printf and the input is read using scanf.
    5. do-while Loop:

      • The program enters a do-while loop that always executes the loop body at least once before checking the loop condition.
      • In each iteration:
        • x is incremented, counting the number of digits.
        • y is updated by adding the last digit of num (obtained using % 10).
        • r is updated to create the reverse of the number.
        • num is updated by removing the last digit (using / 10).
      • The loop continues as long as num is greater than 0.
    6. Output:

      • The program prints the number of digits, the sum of digits, and the reverse of the number using printf.
    7. getch():

      • This function waits for a key press before the program exits. It’s part of the conio.h library.

    Overall, this program calculates and displays the number of digits, the sum of digits, and the reverse of a given number entered by the user using a do-while loop for iteration.

3. for Loop:

The for loop is normally based on counters i.e. generally a counter is used which is initialized, checked for and incremented/decremented for continuing the loop. 

FlowChart:

 

Syntax:

for (initialization part; Conditional part; increment/decrement)

Example:

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

void main()
{
    int num, x = 0, y = 0, r = 0;

    printf(“nEnter a number: “);
    scanf(“%d”, &num); // 123

    for (; num > 0; x++, num /= 10) {
        y += num % 10;
        r = r * 10 + num % 10;
    }

    printf(“nDigits = %d”, x);
    printf(“nSum of Digits = %d”, y);
    printf(“nReverse of the number is %d”, r);

    getch();
}

Explanation of  Program:

    1. Header Files:

      • The program includes two header files: stdio.h for standard input and output functions and conio.h for console input and output functions. Note that the use of conio.h is non-standard and might not be supported by all compilers.
    2. Main Function:

      • The main function is the entry point of the program.
    3. Variables:

      • num: Holds the input number.
      • x: Counts the number of digits in the input.
      • y: Stores the sum of the digits.
      • r: Stores the reverse of the number.
    4. Input:

      • The user is prompted to enter a number using printf and the input is read using scanf.
    5. For Loop:

      • The for loop is used for iteration.
      • The loop initialization section (; num > 0; x++, num /= 10) initializes variables and performs the condition check and update simultaneously.
      • The loop continues as long as num is greater than 0.
      • In each iteration:
        • x is incremented, counting the number of digits.
        • y is updated by adding the last digit of num (obtained using % 10).
        • r is updated to create the reverse of the number.
        • num is updated by removing the last digit (using / 10).
    6. Output:

      • The program prints the number of digits, the sum of digits, and the reverse of the number using printf.
    7. getch():

      • This function waits for a key press before the program exits. It’s part of the conio.h library.

    The program uses a for loop for a concise representation of the loop structure, achieving the same result as the previous do-while loop.