C - While Loop, Do While Loop, For Loop

 Iteration statements:

Looping or iteration statement is use to execute and repeat a block of statement depending on the condition.


1.     While Loop

2.      DWhile Loop

3.      For Loop


1. While Loop:-

The while loop first check the condition if the condition is true then the body of the loop statement will be executed. After execution loop checks condition and execute statements until condition will be false.

SYNTAX-

while (Condition)

{

Block statement;

Increment / decrement;

}

 EXAMPLE-

main()

{

int a=1;

clrscr();

while (a<6)

{

printf(“%d \n”,a);

a=a+1;

}

getch();

}

Output-

1

2

3

4

5

 

2. do while Loop:-

do while loop is use to similar as while loop. In basic difference of while loop first check the condition then execute the loop block, but do while loop first execute statement and then check condition. If condition is false in first time then statement will be executed at least one time.

SYNTAX-

do

{

Block statement1;

Block statement2;

Block statement3;

Block statement4;

Block statement5;

Increment / decrement;

} while (Condition);

 

EXAMPLE-

main()

{

int a=1;  clrscr ();  do

{

printf (“%d \n”,a);

a=a+1;

} while (a<1);

getch();

}

Output-

1

 

3. for Loop:-

The for loop is very flexible and is preferable when there is a simple initialization and increment, as it keeps the loop control statements close together and visible at the top of the loop.

SYNTAX-

for (initialization ; test condition ; increment /decrement)

{

statement -block;

}

EXAMPLE-

main()

{

int a;

clrscr ();

for (a=1;a<6;a++)

{

printf (“%d \n”,a);

}

getch();

}

Output-

1

2

3

4

5

Comments

Popular posts from this blog

Courses After 12th Science

MS Office Versions List

Computer - Network Topologies