Loops and their types in C - Programming Languages

Latest

Thursday 26 January 2017

Loops and their types in C

C Programming for Loop

Loops are used in program to repeat a specific block of code. After reading this tutorial, you will learn to create a for loop in C programming.
By using special loop keywords,you can loop (jumping for those assembly junkies) through your code. These include following categories:
1. for loop
2. while loop
3. do while.

For Loop
The for statement allows for the controlled loop. The syntax for for loop is as follows: 
for (start condition; continue condition; re-evaluation)
program statement;
The foollowing program shows the use of for loop which prints number from 0 to 10  

#include<stdio.h>
#include<conio.h>
int main()
{
int i;
for (i = 1; i <=10; i = i + 1)
printf(“%d”, count);
printf(\t);
getch();
return 0;
}
Output:1  2  3  4  5  6  7  8  9  10
While Loop
For repeating C statements whiles a condition is true,the while provides a the necessary mechanism. The syntax for while loop is as follows:
while (condition)
program statement;
The foollowing program shows the use of while loop which prints number from 0 to 10.
#include<stdio.h>
#include<conio.h>
int main()
{

int i = 0;
while (i <=10)
{
printf(“%d”, i);
++i
}

getch();
return 0;
}
Output:1  2  3  4  5  6  7  8  9  10
The do while statement
The do {} while statement allows a loop to continue whilst a condition evaluates as TRUE (non-zero value).The loop will execute at least once.The syntax is as follow:
 do {
/* do stuff */
} while (statement);
The following program reverse a number using do while loop

#include<stdio.h>
#include<conio.h>
int main()
{

int a, r;
printf(Enter a number to be reversed.\n);
scanf(%d, &a);

do
{
  r= a % 10;
printf(“%d”, r);
a= a / 10;
} while (a!= 0);
printf(\n);

getch();
return 0;
}
Difference between while and do while loop
The do statement is similar to the while statement except that its termination condition is at the end of the body of the loop only. Thus, you want to use a do statement,if you want to perform the body of the loop at least once, regardless of the condition.
Break Statement
The break statement terminates the loop (for, while and do...while loop) immediately when it is encountered. The break statement is used with decision making statement such as if...else.The syntax is as follow:
break;
 
An Example for Using Break Statement
# include <stdio.h>
# include <conio.h>
int main()
{
    int i;
    double number, sum = 0.0;

    for(i=1; i <= 10; ++i)
    {
        printf("Enter a n%d: ",i);
        scanf("%lf",&number);

        // If user enters negative number, loop is terminated
        if(number < 0.0)
        {
            break;
        }

        sum += number; // sum = sum + number;
    }

    printf("Sum = %.2lf",sum);
 
    getch(); 
    
    return 0;
}
Output:
Enter a n1: 2.4 
Enter a n2: 4.5 
Enter a n3: 3.4
Enter a n4: -3 
Sum = 10.30

This program calculates the sum of maximum of 10 numbers. It's because, when the user enters negative number, the break statement is executed and loop is terminated.
In C programming, break statement is also used with switch...case statement.

continue Statement

The continue statement skips some statements inside the loop. The continue statement is used with decision making statement such as if...else.

Syntax of continue Statement

continue;
#include <stdio.h>
#include <conio.h> 
 
int main () {

   /* local variable definition */
   int a = 10;

   /* while loop execution */
   while( a < 20 ) {
   
      printf("value of a: %d\n", a);
      a++;
  
      if( a > 15) {
         /* terminate the loop using break statement */
         break;
      }
  
   }
 
   getch (); 
 
   return 0;
}
Output:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
  
In the program, when the user enters positive number, the sum is calculated using sum += number; statement.
When the user enters negative number, the continue statement is executed and skips the negative number from calculation.

No comments:

Post a Comment