2077 1

CSC115 ยท 2077 ยท 10 marks

What do you mean by looping?Explain while loop with suitable example.Compare while loop with do while loop.Write a program to find sum and average of first n natural numbers.[1+3+2+4]

What do you mean by looping?Explain while loop with suitable example.Compare while loop with do while loop.Write a program to find sum and average of first n natural numbers.[1+3+2+4]

C ProgrammingChapter 5: Control StatementDecision Making and Looping

Quick answer

Looping (also called iteration) is a programming concept that allows a set of statements to be executed repeatedly as long as a given condition remains true. Instead of writing the same code multiple times, a loop executes it automatically for the required number of times.

Looping, While Loop, Comparison with Do-While, and Sum/Average of First N Natural Numbers


1. What is Looping? [1 mark]

Loops are used to:

  • Repeat a block of code a fixed number of times
  • Process data until a condition is met
  • Reduce code length and redundancy

2. While Loop with Example [3 marks]

Definition

The while loop is an entry-controlled loop, meaning the condition is checked before executing the body of the loop. If the condition is false at the very beginning, the loop body is never executed.

Syntax

while (condition)
{
    // body of the loop
    // increment / decrement statement
}

How it works (Execution Steps):

  1. The condition is evaluated first.
  2. If the condition is true, the loop body executes.
  3. After executing the body, control goes back to step 1.
  4. If the condition is false, the loop terminates and control passes to the next statement after the loop.

Flowchart Description:

Start --> Check Condition --> [False] --> Exit Loop
               |
            [True]
               |
         Execute Body
               |
         Update Variable
               |
         (back to Check Condition)

Example: Print numbers 1 to 5

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

void main()
{
    int i = 1;          /* initialization */
    clrscr();

    while (i <= 5)      /* condition */
    {
        printf("%d\n", i);
        i++;            /* increment */
    }

    getch();
}

Output:

1
2
3
4
5

3. Comparison: While Loop vs Do-While Loop [2 marks]

FeatureWhile LoopDo-While Loop
TypeEntry-controlled loopExit-controlled loop
Condition checkChecked before loop body executesChecked after loop body executes
Minimum executions0 times (if condition is false initially)At least 1 time (body executes before check)
Syntaxwhile(condition){ }do{ } while(condition);
SemicolonNo semicolon after conditionSemicolon required after while(condition);
Use caseWhen execution depends on condition being true firstWhen the loop body must run at least once

Syntax Comparison:

While Loop:

while (condition)
{
    // statements
}

Do-While Loop:

do
{
    // statements
} while (condition);

Key Difference Example:

If the condition is false from the start:

  • while loop: body executes 0 times
  • do-while loop: body executes 1 time

4. Program to Find Sum and Average of First N Natural Numbers [4 marks]

Algorithm:

  1. Start
  2. Read the value of n
  3. Initialize sum = 0, i = 1
  4. While i <= n, do:
    • sum = sum + i
    • i = i + 1
  5. Calculate average = sum / n
  6. Display sum and average
  7. Stop

Program:

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

void main()
{
    int i, n, sum;
    float average;

    clrscr();

    /* Input */
    printf("Enter the value of n: ");
    scanf("%d", &n);

    /* Initialize */
    sum = 0;
    i = 1;

    /* While loop to calculate sum */
    while (i <= n)
    {
        sum = sum + i;
        i++;
    }

    /* Calculate average */
    average = (float) sum / n;

    /* Output */
    printf("Sum of first %d natural numbers = %d\n", n, sum);
    printf("Average of first %d natural numbers = %.2f\n", n, average);

    getch();
}

Sample Output (for n = 5):

Enter the value of n: 5
Sum of first 5 natural numbers = 15
Average of first 5 natural numbers = 3.00

Verification (n = 5):

  • Natural numbers: 1, 2, 3, 4, 5
  • Sum = 1 + 2 + 3 + 4 + 5 = 15
  • Average = 15 / 5 = 3.00

Note: (float) is used to cast the integer sum to float so that the average is computed as a decimal value, not an integer.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.