the loops

Post on 17-Aug-2015

43 Views

Category:

Education

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

LOOPING• In computer programming, a loop is a

sequence of instructions that is continually repeated until a certain condition is reached. 

• Programming languages provide various control structures that allow for more complicated execution paths.

• Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.

• A loop statement a l lows us to execute a statement or group of statements mult ip le t imes and fo l lowing is the genera l form of a loop statement in most of the programming languages.

DIFFERENT TYPES OF LOOP

Loop Type Description

While loop Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.

For loop Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable.

Do……While loop Like a while statement, except that it tests the condition at the end of the loop body

Nested loop You can use one or more loop inside any another while, for or do..while loop.

FOR LOOP SYNTAX :The syntax of a for loop in C programming

language is:

In computer science a for loop is a programming language statement which allows code to be repeatedly executed.

for ( init; condition; increment ) { statement(s); }

FLOWCHART

Ex.1. A program to store 10 real nos. using for loop.

OUTPUT

WHILE LOOPSYNTAX: The syntax of a while loop in C programming

language is:

A while loop statement in C programming language repeatedly executes a target statement as long as a given condition is true.

Initialization ;

while( condition ) { statement (s) ;

Increment ; }

FLOWCHART OF WHILE LOOP

Ex.2. A PROGRAM USING WHILE LOOP.

OUTPUT

DO…..WHILE loop Unlike for and while loops, which test the loop

condition at the top of the loop, the do...while loop in C programming language checks its condition at the bottom of the loop.

A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.

Ex.3. A program using DO…WHILE loop with its OUTPUT.

Nested loopsThe syntax for a nested for loop statement in

C is as follows:

for ( init; condition; increment ) { for ( init; condition; increment ) { statement(s); } statement(s); }

The syntax for nested while and do….while loop:

do { statement(s); do { statement(s); }while(condition); }while( condition );

while( condition ){while( condition ){ statement(s);}statement(s);}

Nested loops

Ex.4. A program using nested for loop.

OUTPUT

PREPARED BY-

KRISHMA PAREKH 2nd year C.E.

top related