by Dinesh Thakur Category: Control Structures

C has looping , in which a sequence of statements are executed until some conditions for termination of the loop are satisfied.

 

A program loop therefore consists of two segments, one known as the ‘body of the loop’ and the other known as the ‘control statement’ .The control statement tests certain conditions and then directs the repeated execution of the statements contained in the body of the loop.

Depending on the position of the control of the control statement in the loop, a control structure may be classified either as the ‘entry-controlled loop (top down controlled loop) or as the ‘exit-controlled loop( bottom up controlled loop)’ .The entry controlled and exit controlled loop.

Entry-controlled loops:- while( ) , for () .

Exit controlled loop:- do-while().

C has three forms of iteration statement:

do

<statement>

while ( <expression> ) ;

while ( <expression> )

<statement>

for ( <expression> ; <expression> ; <expression> )

<statement>

In the while and do statements, the substatement is executed repeatedly so long as the value of the expression remains nonzero (true). With while, the test, including all side effects from the expression, occurs before each execution of the statement; with do, the test follows each iteration. Thus, a do statement always executes its substatement at least once, whereas while may not execute the substatement at all.

If all three expressions are present in a for, the statement

for (e1; e2; e3)

s;

is equivalent to

e1;

while (e2)

{

s;

e3;

}

except for the behavior of a continue; statement (which in the for loop jumps to e3 instead of e2).

Any of the three expressions in the for loop may be omitted. A missing second expression makes the while test always nonzero, creating a potentially infinite loop.

Since C99, the first expression may take the form of a declaration, typically including an initializer, such as

for (int i=0; i< limit; i++){

...

}

The declaration's scope is limited to the extent of the for loop.

 



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Search Content





Popular Article