Skip to main content

Control Statements: Loops in C programming

Loops are used when we want to execute a part of program or block of statement several times. So, a loop may be defined as a block of statements which are repeatedly executed for a certain number of times or until a particular condition is satisfied. There are three types of loop statements in C:

1. For
2. While
3. Do...while

Each loop consists of two segments, one is k/a the control statement and the other is the body of the loop. The control statement in loop decides whether the body is to be executed or not. Depending on the position of control statement in the loop, loops may be classified either entry_controlled loop or exit_controlled loop. While and For are entry_controlled loops where as do...while is exit_controlled loop.

For Loop:

For loops is useful to execute a statement for a number of times. When the number of repetitions is known in advance, the use of this loop will be more efficient. Thus, this loop is also known as determinate or definite loop.
The general syntax

for (counter initialization ; test condition ; increment or decrement)
{
| * body of loop * |
}



 
  (Fig : Flowchart of For Loop)


For example:

|* Calculate the factorial of a number *|
    #include<stdio.h>
    main()
    {
    int num, i
    long fact = 1
    printf (“ln Enter a number whose factorial is to be calculated : “) ;
    scanf (“%d”, &num) ;
    for (i=1 ; i<=num ; i++)
    fact * = i ; |* fact = fact*i *|
    printf (“\n The factorial is : %d”, fact ) ;
    }

    Output:
      Enter a number whose factorial is to be calculated: 5
      The factorial is: 120

 



While Loop:
The while statement can be written as
while(condition)                                  while(condition)
statement ;                                           {
statement ; | * bed of the loop *|
statement ;
- - - - - - -
}
First the condition is evaluated; if it is true then the statements in the body of loop are executed. After the execution, again the condition is checked and if it is found to be true then again the statements in the body of loop are executed. This means that these statements are executed continuously till the condition is true and when it becomes false, the loop terminates and the control comes out of the loop. Each execution of the loop body is called iteration.
             |* Program to print the sum of digits of any num *|
            #include<stdio.h>
            main()
            {
            int n, sum = 0, rem ;
            printf (“Enter the number :”) ;
            scanf (%d”, &n) ;
            while (n>0)
            {
            rem = n%10 ; |* taking last digit of number *|
            sum+ = rem ; |* sum = sum + rem * |
            n/ = 10 ; |* skipping last digit *|
            } |*n = n/10 *|
            printf (“Sum of digits = %d \n”, sum) ;
            }
            Output:
            Enter the number : 1452
            Sum of digits = 12

 
 




















do...while loop:
The do...while statement is also used for looping. The body of this loop may contain a single statement or a block of statements. The general syntax is :
do                                                        do
statement ;                                           {
while(condition) ;                               Statement 1 ;
Statement 2 ;
- - - - - - - -
Statement n ;}
while(condition) ;

Here firstly the segments inside the loop body are executed and then the condition is evaluated. If the condition is true, then again the loop body is executed and this process continues until the condition becomes false. Unlike while loop, here a semicolon is placed after the condition. In a ‘while’ loop, first the condition is evaluated and then the statements are executed whereas in do while loop, first the statements are executed and then the condition is evaluated. So, if initially the condition is false the while loop will not execute at all, whereas the do while loop will always execute at least once.
                   |* program to print the number from 1 to 10 using do while *|
                 #include <stdio.h>
                 main()
                 {
                  int i = 1 ;
                  do
                  {
                  printf (“%dlt”, i) ;
                  itt ;
                  } while (i<=10) ;
                  printf(“ln”) ;
                  }
                Output : 1 2 3 4 5 6 7 8 9 10

 
 















Differences between while loop and do while loop:
while                                                                                                        
  1. while loop is entry controlled loop i.e. test condition is evaluated first and body of loop  is executed only if this test is true.
  2. The body of the loop may not be executed  at all if the condition is not satisfied at the  very first attempt.
  3. syntax :
   while (condition)
{    body of the loop }
      4. Draw flowchart
do...while
1. do...while loop is exit controlled loop i.e. the body of the loop is executed first without checking condition and at the end of body of loop, the condition is evaluated.
2. The body of the loop is always executed at least once.
3. syntax :
do
{
body of the loop
} while (condition)
4. Draw flowchart


Nesting of loops:
When a loop is written inside the body of another loop, then it is k/a nesting of loops. Any type of loop can be nested inside any other type of loop. For example, a for loop may be nested inside another for loop or inside a while or do...while loop. Similarly, while and do while loops can be nested.
| * program to understand nesting in for loop * |
#include<stdio.h>
main()
{
int i,j ;
for (i = 1 ; i<=3 ; itt) |* Outer loop *|
{
printf (“i=%d|n”, i)
for (j=1 ; j<=4 ; j++) |* inner loop *|
printf (“j=%d\t”,j) ;
printf (“\n”) ;
}
}

Output:
i = 1
j = 1 j = 2 j = 3 j = 4
i = 2
j = 1 j = 2 j = 3 j = 4
i = 3

j = 1 j = 2 j = 3 j = 4

Comments

Popular posts from this blog

Passing arrays to functions in C programming

Like any other variables, we can also pass entire array to a function. An array name can be named as an argument for the prototype declaration and in function header. When we call the function no need to subscript or square brackets. When we pass array that pass as a call by reference because the array name is address for that array. /* Program to illustrate passing array to function */ #include<stdio.h> void display(int) ; /* function prototype */ main( ) { int num[5] = {100, 20, 40, 15, 33, i ; clrscr( ) ; printf (“\n The content of array is \n”) ; for (i=0; i<5; i++) display (num[i]) ; /*Pass array element fo fun */ getch{ } ; } void display(int n) { printf (“\t%d”, n ) ; } Output:     The content of array is 100      20       40       15 3 /* Program to read 10 numbers from keyboard to store these num into array and then c...

Explanation of Graphics in C programming

   The header file graphics.h should be included to use graphics related built in functions. Again, the variable gDriver is an integer variable that specifies graphics driver to be used. Device drivers are small programs which talk directly to the hardware. Graphics drivers are a subset of device drivers and are applicable only in the graphics mode. The initialization of this variable to DETECT is to set another variable gMode to the highest resolution available for the detected driver.  The value of this variable gDriver may be another than DETECT (e.g. 1 for CGA, 9 for VGA.). The variable gMode is an integer that specifies initial graphics mode.       The built in function initgraph() is for graphics initialization. The syntax for this is as   initgraph(&graphics_driver, &graphics_mode, path_to_driver);   path_to_driver specifies the directory path where initgraph() looks for graphics drivers ( .BGI files). Thi...

BIOS

BIOS stands for Basic Input Output System. It is is kind of OS pre- attached in the system Design to detect every I/O devices It holds basic drivers -checks all I/O devices -Loads main OS -Responsible for starting System switched on manufactured by AMI,Phonix,award,Baiostar POST error test, boots trap load, BIOS is unintrusive system. CMOS and BIOS are two different components of the motherboard. CMOS work as storage if BIOS. A basic software program containing all BIOS is permanently stored in the ROM. every essencial holds program Tresh passing without knowinig is called intrusive.