The Loops

19

Transcript of The Loops

Page 1: The Loops
Page 2: The Loops

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.

Page 3: The Loops

• 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.

Page 4: The Loops

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.

Page 5: The Loops

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); }

Page 6: The Loops

FLOWCHART

Page 7: The Loops

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

Page 8: The Loops

OUTPUT

Page 9: The Loops

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 ; }

Page 10: The Loops

FLOWCHART OF WHILE LOOP

Page 11: The Loops

Ex.2. A PROGRAM USING WHILE LOOP.

Page 12: The Loops

OUTPUT

Page 13: The Loops

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.

Page 14: The Loops

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

Page 15: The Loops

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); }

Page 16: The Loops

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

Page 17: The Loops

Ex.4. A program using nested for loop.

Page 18: The Loops

OUTPUT

Page 19: The Loops

PREPARED BY-

KRISHMA PAREKH 2nd year C.E.