KNOW ABOUT LOOP CONTROL STRUCTURE IN C

LOOP CONTROL STRUCTURE IN C

Looping is a repetitive structure.In looping a group of structures are executed until some condition has been satisfied. In looping a group of statements are executed until some condition has been satisfied.

In C following loop structure are available:
  1. for loop
  2. while loop
  3. do-while loop

 

1. for loop

For loop is very simple loop structure in C.In for loop we can specify initialization expression , Condition expression and increment expression in a single statement. It is also called short cut loop. 


Syntax:

for ( initialization expressions;
test expression;
update expression;)

body-of-the-loop;

Example:

int i; //declare variable

for(i=1;i<=10;i++)

    printf("\n%d",i);

getch();
Note:
case 1.:

for(initialize exp1 initialize exp2;
test expression 1 test expression 2;
update expression 1 update expression 2)

case 2.:

for( ;test expression;update expression)

case 3.:

for(initialize exp; ;update expression) -- infinite
 
for( ; ;)--infinite

case 4.:

for(initialize exp;test expression;update expression); -- empty no body

case 5.:

for(int i=1;i<10;i++) -- deceleration of i
 
 

2. while Loop
While loop is an entry control loop. In a while loop control variable should be initialized before the loop begins as an uninitialized variable can not be used in an expression.The loop variable should be updated inside the body of the while.
Syntax:

while(expression)

loop-body;

Example:

int i=0;

while(i<=10)
{

    printf("%d",i);

    i++;

}

Note:

repeat expression.

syntax: while (boolean) statement 1;

example while(1);

while(0);
 
 

3. do-while Loop
do-while loop is an exit-control loop. do-while loop execute statement at least once. because test condition evaluates at the bottom of loop.
Syntax:

do
{

    statements;

}while(test-condition);
 

Example:

int i=1;

do
{

    printf("%d\n",i);

}while(i<10);
 
 

 

PRACTICE QUESTIONS: 

1. Write a program to display count from 0 to 9 by using For Loop | while loop | do while loop and Analysis which is the best approach to do.

 

2. Write a program to display even no , above 20 & below 40 by using For Loop | while loop | do while loop and Analysis which is the best approach to do.

 

3. Write a program to find average by using For Loop | while loop | do while loop and Analysis which is the best approach to do.

 

4. Write a program to find mirror image of a number by using For Loop | while loop | do while loop and Analysis which is the best approach to do.

 

5. Write a program to display sum of digits of a number  by using For Loop | while loop | do while loop and Analysis which is the best approach to do.

 

6. Write a program to find out odd numbers form 1 to 100 by using For Loop | while loop | do while loop and Analysis which is the best approach to do.

 

7. Write a program to print out the even number  form 1 to 100 by using For Loop | while loop | do while loop and Analysis which is the best approach to do.

 

8. Write a program to reverse the digits of any given number  by using For Loop | while loop | do while loop and Analysis which is the best approach to do.

 

9. Write a program  for Fibonacci series  by using For Loop | while loop | do while loop and Analysis which is the best approach to do.

 

10. Write a program to print out the value of the following series.

        a. SUM =  1 + 2 +3 + 4 + ....... +  20

        b. SUM = 20 + 19 + 18 + 17 ------ + 1

 

 

 

No comments:

Post a Comment