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

Difference between Binary mode and Text Mode in C programming

There are mainly three difference between binary and text mode. They are as follow.               i.       In text mode, a special character EOF whose ASCII value is 26 is inserted after the last character in the file to mark the end of file. But, there is no such special character present in the binary mode. The binary mode files keep track of the end of file from the number of characters present in the directory entry of the file.             ii.       In text mode of file, text and numbers are stored as one character per byte. For example, the number 1234 occupies two bytes in memory but it occupies 4 bytes (one byte per character) in the file. But, in binary mode, the number occupies the number of bytes as it occupies in the memory. Thus,  the above number occupies two bytes in file also in the case o...

CPU (Central Processing Unit)

PGA reffered as pin grid array in which pins of CPU are lined up in a straight format. SPGA referred as staggered pin grid array. in which pins of CPU are arranged staggered format. LGA reffered to as Land Grid Array in which pins are available within inside the socket but not in the CPU in other words in line grid array, in CPU ther are no pinsbut insteadpins areattached with in a socket which contact with with the CPU. for e.g. LGA775 socket(no pins on cpu) has better cooling system. better contact and better locking(climbing). LGA1155: Letest generation I socket, also reffered as sandy bridge, Turbo boost overclocking. More resent than LGA 1156 socket. LGA1156: ability to north bridge Doul channel DDR3 optional integreted graphics PCI express LGA1366: hi end core i series socket integreted tiple channal memmory controller external control bridge(HUB) Upgrading CPU: need to check its core suppert check multiprocessor supports or not check on the ...

Keywords in C programming

There are certain reserved words, called keywords that have standard, predefined meanings in C. These keywords can be used only for their intended purpose, they cannot be used as programmer-defined identifiers. The standard keywords are: auto, break, case, char, const, continue, default, do, double, else, enum, extern, float, for, goto, if, int, long, register, return, short, signed, sizeof, static, struc, switch, typedef, union, unsigned, void, volatile, while. The keywords are all lowercase. Since upper and lowercase characters are not equivalent, it is possible to utilize an uppercase keyword as an identifier. Normally, however, this is not done, as it is considered a poor programming practice.