Loops in C Programming

10
LO0PS IN C-PROGRAMMING HIMANSHU NEGI http://himanshuneg i.in

description

This presentation is about Loops in C Programming Language. This Power Point Presentation (PPT) includes Syntax of Loops as well as example of For loop, do loop, do while loop. http://himanshunegi.in/category/cpp-programming/

Transcript of Loops in C Programming

Page 1: Loops in C Programming

LO0PS IN

C-PROGRAMMING

HIMANSHU NEGIhttp://himanshunegi.in

Page 2: Loops in C Programming

LOOPS

• Loops statements are used to repeat the execution of statement or blocks.

• Two types of loop structure are:

– Prestest :– Posttest :

Page 3: Loops in C Programming

Pretest Vs. Posttest

• Pretest : Condition is tested before each iteration to check if loops should occur. eg: For & While loop

• Posttest : Condition is tested after each iteration to check if loop should continue (at least a single iteration occurs).

eg: Do-while loop

Page 4: Loops in C Programming

FOR LOOP

For loop has three parts:

Initializer is executed at start of loop.

Loop condition is tested before iteration to decide whether to continue or terminate the loop.

Increment is executed at the end of each loop iteration.

Page 5: Loops in C Programming

• Syntax: for( [incrementor]; [condition]; [incrementor] )

<statement/block>;

Example: for(i=0; i<3; i++)printf(“Hello\n”);

Output: HelloHelloHello

Page 6: Loops in C Programming

WHILE LOOP

• It has a loop condition only that is tested before each iteration to decide whether to continue or terminate the loop.

Syntax:

While (<condition>) <statement/block>;

Page 7: Loops in C Programming

Example :

• While(i=0; i<3; i++) printf(“Hello\n”);

Output:HelloHelloHello

Page 8: Loops in C Programming

DO…WHILE LOOP

• Do while has a loop condition only that is tested after each iteration to decide whether to continue with next iteration or terminate the loop.

Syntax: do <statement/block>;

While(condition);

Page 9: Loops in C Programming

Example:

Do{ Printf (“Hello\n”); } while (i<3)

Output

HelloHelloHello

Page 10: Loops in C Programming

Thank You !&

HAPPY PROGRAMMING

http://himanshunegi.in