LOOPINGITERATIVE STRUCTURE IN C PROGRAMMING Outline Introduction Loop

  • Slides: 16
Download presentation
LOOPING(ITERATIVE) STRUCTURE IN C PROGRAMMING

LOOPING(ITERATIVE) STRUCTURE IN C PROGRAMMING

Outline Ø Introduction Ø Loop (What, Why) ØVarious Loops in C - while loop

Outline Ø Introduction Ø Loop (What, Why) ØVarious Loops in C - while loop - for Loop - do…while Loop Ø comparison between looping structures ØExercise 2

Introduction Ø Normally, c program executed in following three ways. (1) sequential (2) selection

Introduction Ø Normally, c program executed in following three ways. (1) sequential (2) selection (3) looping (Iterative) Ø In sequential method all the statements of a c program is executed one by one. i. e. 1 st statement executed first then second and so on. ØMany times it is require to execute some statement and skip other statements based on some condition. Selection statements allows alternative courses of action to be taken depending on the result of an operation. Øif, if. . else, if…else, nested if, switch…case, break and continue etc are example of selection structure. 3

ØMany a times some statements needs to be executed multiple times. Looping structure can

ØMany a times some statements needs to be executed multiple times. Looping structure can be used for this purpose. ØThere are 3 types of loops available in C language. (1) While loop (2) for loop (3) do…while loop

Looping (Repetitive) Generally, statements are executed sequentially: The first statement in a function is

Looping (Repetitive) Generally, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. Sometimes you may encounter situations, when a block of code needs to be executed several number of times. A loop statement allows us to execute a statement or group of statements multiple times. C provides 3 types of loops: (1) While loop (2) For loop (3) Do…while loop

(1) While Loop A while loop executes a target statement repeatedly until a given

(1) While Loop A while loop executes a target statement repeatedly until a given condition is true. Syntax : initialization; while(condition) { statement(s); increment/decrement; } Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any nonzero value. The loop iterates while the condition is true. When the condition becomes false, the program control passes to the line immediately following the loop. 6

Flow Diagram

Flow Diagram

Example // c program to print numbers 1 to 5 using while loop. #include

Example // c program to print numbers 1 to 5 using while loop. #include <stdio. h> #include <conio. h> void main () { int i; clrscr(); i=1; while( i <= 5 ) { printf(“%d ", i); i++; } getch(); } Output : 1 2 3 4 5

(2) For Loop A for loop is a repetition control structure that allows you

(2) For Loop A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times. Syntax for ( init; condition; increment ) { statement(s); } The init step is executed first, and only once. Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute. After the body of the 'for' loop executes, the flow of control jumps back up to the increment statement. 9

Flow Diagram

Flow Diagram

Example #include <stdio. h> void main () { int i; clrscr(); for( i =

Example #include <stdio. h> void main () { int i; clrscr(); for( i = 1; i <= 5; i++) { printf(“%d ", i); } getch(); } Output : 1 2 3 4 5

(3) Do While Loop Unlike for and while loops, which test the loop condition

(3) 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 checks its condition at the bottom of the loop. A do. . . while loop is similar to a while loop, except the fact that it is guaranteed to execute at least one time. Syntax The syntax of a do. . . while loop in C programming language is − do { statement(s); //body of loop } while( condition );

Flow Diagram Action true Expression false

Flow Diagram Action true Expression false

Example #include <stdio. h> #include <conio. h> void main () { int i =

Example #include <stdio. h> #include <conio. h> void main () { int i = 1; clrsccr(); do { printf(“%d ", i); i = i + 1; }while( i <= 5 ); getch(); } Output: 1 2 3 4 5

Comparison between while loop and do. . while loop COMPARISON Syntax WHILE while (

Comparison between while loop and do. . while loop COMPARISON Syntax WHILE while ( condition) { statements; //(Body of loop) } DO-WHILE do { statements; //(Body of loop) } while( Condition ) Condition In 'while' loop the condition appears at the start of the loop. In 'do-while' loop the condition appears at the end of the loop. Execution While loop does not execute body of loop if, the condition at the first iteration, appears false. The body of loop executes at least once even if the condition is false at the first iteration.

Exercises 1. Find the errors, if any in each of following statements: a) for

Exercises 1. Find the errors, if any in each of following statements: a) for (i=1; i<=10; i++; ) b) for (i=1, i<3, i++) c) for (i=1; i<=10; i++); d) for (i=50; i<40; i++) e) for (i=1; i<100; ) { i++; } f) for (i=10; i>100; i--) g) for (i=20; i>10; i--) i=i*3; (h) for (; ; ) printf(“hello”); 2. Create a program using while, for and do while that print only even numbers from 1 to 500. 3. Create a program using while loop that print the average of first 100 numbers. 16