C PROGRAMMING
MULTIPLE CHOICE QUESTION
OLD QUESTION BANK
SAMPLE QUESTION 2080 AND SOLUTION

5.4. Looping Statement


Until now, we are able to execute instructions in sequential order, perform conditional execution of statements. C programming also provides us with functionality to execute a statement or set of statements repeatedly, such type of instructions are called a Looping Statement, which performs repeated execution of instructions a fixed number of times or until a particular condition is satisfied.we can represent the looping statement in flowchart as follows:

The above diagram clearly states that the instruction or set of instructions keeps on executing as long as the condition specified is TRUE, when the condition becomes FALSE, the looping statement is terminated and program controls transfer to the immediate next statement after the looping statement.

There are three ways by which we can repeat execution of instruction in C:

  1. while loop
  2. for loop
  3. do ..while loop

 

5.4.1 While loop

The while loop in C-programming can be used to execute, the given statements fixed number of times, the general form of while loop is as follows:

initialize loop counter variable
while(test loop counter variable using condition)
{
    statement 1;
    statement 2;
    statement 3;
    .
    .
    statement n;
    increment or decrement loop counter variable
} 

The above syntax states that:

  • After initialization of the loop counter variable , the counter variable is tested for its Boolean value either True or False. Till the condition is tested True, the body of while loop(single statement, then curly bracket is optional, or multiple statement,curly bracket is required to enclose them as body of while loop) keeps on executing. When the condition is evaluated to False(zero(0)) value the body of the while loop is terminated and program control transfers to the immediate next statement in the program.
  • The loop counter variable is incremented or decremented so that the condition will become False after fixed number of execution, otherwise the condition keeps on being True and the loop will keep on executing infinitely.

Write a C program to print hello world  5 times.

#include <stdio.h>

int main() {
    // Initialize loop counter variable
    int counter = 0;

    // While loop with the condition (counter < 5)
    while (counter < 5) {
        // Statement inside the loop
        printf("Hello, world!\n");
        
        // Incrementing the counter
        counter++;
    }

    return 0;
}

In the above program:

  • We initialize the loop counter variable counter to 0.
  • We enter the while loop with the condition counter < 5.
  • Inside the loop, we print "Hello, world!".
  • We increment the counter by 1 in each iteration.
  • The loop continues until the condition counter < 5 is no longer true (when counter becomes 5), and then the while loop exits

Output:

Hello, world!
Hello, world!
Hello, world!
Hello, world!
Hello, world!

Write a C program, to print all the numbers from 1 to 10.

#include <stdio.h>

int main() {
    // Initialize loop counter variable
    int i = 1;

    // While loop with the condition (i <= 10)
    while (i <= 10) {
        // Statement inside the loop to print the current value of i
        printf("%d\n", i);
        
        // Incrementing the counter
        i++;
    }

    return 0;
}

Write a C program, to print all the numbers from 10 to 1.

#include <stdio.h>

int main() {
    // Initialize loop counter variable
    int i = 10;

    // While loop with the condition (i >= 1)
    while (i >= 1) {
        // Statement inside the loop to print the current value of i
        printf("%d\n", i);
        
        // Decrementing the counter
        i--;
    }

    return 0;
}

It is perfectly valid to use conditional statements or any valid C-statement within the body of the while loop. Let us understand the concept using following example:

Write a C program, to print all the even numbers from 1 to 10.

#include <stdio.h>

int main() {
    // Initialize loop counter variable
    int i = 1;

    // While loop with the condition (i <= 10)
    while (i <= 10) {
        // Check if i is even
        if (i % 2 == 0) {
            // If i is even, print it
            printf("%d\n", i);
        }
        
        // Incrementing the counter
        i++;
    }

    return 0;
}

In the above program:

  • We initialize the loop counter variable i to 1.
  • We enter the while loop with the condition i <= 10.
  • Inside the loop, we check if the current value of i is even using the condition i % 2 == 0.
  • If i is even, we print its value.
  • We increment i by 1 in each iteration.
  • The loop continues until the condition i <= 10 is no longer true (when i becomes 11), and then the program exits.

5.4.2 for loop

We can achieve a loop in a C program by either using for or while loop. For loop is more popular among developer compared to while loop as it allow us to write:

  • Initialization of loop counter
  • Test of loop counter
  • Update of loop counter

In a single line. Due to this ease of use, the for loop is more commonly used compared to while or do..while loop. The general form of for loop is as follows:


for(Initialize counter;test loop counter;update loop counter)
{
    Statement 1;
    Statement 2;
    Statement 3;
    .
    Statement n
}


The above mentioned syntax of for loop, works as follows:

  • The loop starts by executing the initialization part, where the loop counter variable is initialized.
  • Next, the test condition is evaluated. If it is true, the loop body is executed. If it is false, the loop terminates, and the program continues execution after the loop.
  • After executing the loop body, the update part is executed, which typically increments or decrements the loop counter variable.
  • The test condition is evaluated again. If it is true, the loop body is executed again, and the process repeats. If it is false, the loop terminates.
  • This process continues until the test condition becomes false.

The diagram below also shows, the order of execution in for loop:

Write a C program to print Hello World 5 Times:

#include <stdio.h>

int main() {
    int i;
    // Iterate 5 times using a for loop
    for (i = 0; i < 5; i++) {
        // Print "Hello, world!"
        printf("Hello, world!\n");
    }

    return 0;
}

Write a C program to print multiplication table of given number:

#include <stdio.h>

int main() {
    int number;
    
    // Input the number from the user
    printf("Enter the number: ");
    scanf("%d", &number);
    
    // Print the multiplication table
    printf("Multiplication table for %d:\n", number);
    for (int i = 1; i <= 10; i++) {
        printf("%d X %d = %d\n", number, i, number * i);
    }

    return 0;
}

5.4.3 do..while loop

The general form of do.. while loop is as follows: 

initialize loop counter
do{
    statement 1;
    statement 2;
    .
    statement n;
    update loop counter
}while(condition is true);

The major difference between while and do while loop is:

Let us write a C program , to print all the numbers from 0 to 5:

  1. Using while loop
#include <stdio.h>

int main() {
int i = 0;
while (i <= 5) {
printf("%d ", i);
i++;
}
return 0;
}

The same program can be written using do while loop as follows:

#include <stdio.h>

int main() {
int i = 0;
do {
printf("%d ", i);
i++;
} while (i < 5);
return 0;
}

 

Write a C program ,that asks a user to enter a number as long as user wants, and find sum of all these numbers:

#include <stdio.h>

int main() {
    int number, sum = 0; // Initialize sum to 0
    char choice = 'y';

    do {
        printf("Enter a number: ");
        scanf("%d", &number);
        sum += number;
        printf("Do you want to enter another number y or n:  ");
        scanf(" %c", &choice);
    } while (choice=='y'); // Continue looping until choice=='n

    printf("The sum of all entered numbers is: %d\n", sum);

    return 0;
}

Output:

Enter a number: 2
Do you want to enter another number y or n:  y
Enter a number: 3
Do you want to enter another number y or n:  y
Enter a number: 4
Do you want to enter another number y or n:  y
Enter a number: 4
Do you want to enter another number y or n:  n
The sum of all entered numbers is: 13