CONDITIONAL STATEMENTS AND LOOPS PART-III



2.  do-while loop:
    do-while loop construct is another method used in c programming. do-while loop ensure that the program is executed at least once and checks whether the condition at the end of the do-while loop is true or false. As long as the test condition is true, the statements will be repeated. The control will come out from the loop, only when the test condition is false. do-while loop is an exit-controlled loop. The given instruction are first executed and then given condition is checked while exiting the loop.








The do-while has the following from:
do    {
statement1 ;
statement2 ;
…………………        }
while(condition) ;
    The blocks of statement with in double braces {} following the word do are executed at least once. Then the condition is evaluated. If the condition is true, the block of statement are executed again until the value of condition tested is false.
For Example:
#include<stdio.h>
main()   {
int num, fact;
fact=1;
printf( "Enter the number: ");
scanf(" %d", &num);
do  {
fact = fact * num--;   }
while (num >1);
printf("The factorial is %d", fact); }
Output:







3.  for loop
     for loop construct is used to execute a set of statements for a given number of times. The for loop is an entry-controlled loop.
The syntax is:
for( initial condition; test condition; incrementer or decrementer)  {
statement1;
statement2;     }
for loop construct requires to specify three characteristics. These are:

  • The initial value of loop counter
  • Testing the loop counter value to determine whether its current value has reached the number of repetitions desired. 
  •  Increasing or decreasing the value of loop counter by a specified number, each time the program segment is executed

For Example:
#include<stdio.h>
main()  {
int i, first = 0, second = 1, sum, limit;
printf ("Enter The Limit: ");
scanf ("%d", &limit);
printf ("%d %d\n",first, second);
for (i=1; i <= limit; i++)   {
sum = first + second;
printf ("%d \n",sum);
first = second;
second = sum;   }
printf("\nThe %dth Fibonacci number is %d",limit, sum);  }
Output:








4.  Nested for loop construct:
    A loop may also contain another loop within its body. This form of a loop inside a loop is called nested loop. In a nested loop construction, the inner loop must terminate before the outer loop can be ended.
for (i = 0; i<= 2; i+++)
{  for (j = 0; j <=4; j++)
 {   statement1;
statement2;
……………       }}
For Example:
#include<stdio.h>
main()  {
int i, j;
for ( i = 1; i < 5; i++)  {
for(j = 1; j < 5; j++)   {
printf(" %d", j);    }
printf(" \n");      }}
Output:








5.  The break statement
    The break statement causes an immediate exit from the innermost loop structure. The break statement can also be used with switch case structure, that we studied earlier.
The general format of the break statement is:
break;
The break is a keyword in programming language C and a semicolon must be inserted after the word break.
 break statement with switch-case structure
switch (day) {
case 1:  printf(“Monday \n ”);
break;
case 2:  printf(“Tuesday \n”);
break
default: 
……………..
……………..   }
CONDITIONAL STATEMENTS AND LOOPS PART-III CONDITIONAL STATEMENTS AND LOOPS PART-III Reviewed by vijay pratap singh on September 19, 2017 Rating: 5

No comments:

Powered by Blogger.