mesics lecture 7 iteration and repetitive executions

26
Iteration and Repetitive Iteration and Repetitive Executions Executions www.eshikshak.co.in www.eshikshak.co.in

Upload: eshikshak

Post on 17-May-2015

667 views

Category:

Technology


2 download

DESCRIPTION

Iteration and Repetitive Execution for, while, do...while

TRANSCRIPT

Page 1: Mesics lecture 7   iteration and repetitive executions

Iteration and Repetitive ExecutionsIteration and Repetitive Executions

www.eshikshak.co.inwww.eshikshak.co.in

Page 2: Mesics lecture 7   iteration and repetitive executions

IntroductionIntroduction

• It is a tedious process perform such kind of tasks repeatedly with pen/pencil and paper.

• Computer Programming language and packages, the task becomes easy, accurate and fast.

Page 3: Mesics lecture 7   iteration and repetitive executions

What is a “Loop” ?What is a “Loop” ?

• A loop is defined as a block of statements, which are repeatedly executed for a certain number of times.

• The loops are of two types– Counter Controlled Repetition– Sentinel Controlled Repetition

Page 4: Mesics lecture 7   iteration and repetitive executions

Counter Controlled RepetitionCounter Controlled Repetition• It is also called the definite repetition action i.e. the

number of iteration to be performed is defined in advance in the program itself.

• Steps in this type of loop– Loop Variable

• Variable used in the loop– Initialization

• It is first step in which starting and final values are assigned to the loop variable.

• Each time the value updated is checked by the loop itself.– Increment and Decrement

• It is the numerical value added or subtracted to the variable in each round of the loop

• The updated value is compared with the final value and it is found less than the final value the steps in the loop are executed

Page 5: Mesics lecture 7   iteration and repetitive executions

Sentinel-Controlled RepetitionSentinel-Controlled Repetition• It is also called the indefinite repetition action

i.e. One cannot estimate how many iteration are to be performed.

• In this type, Loop termination happens on the basis of certain condition using decision-making statement.

Page 6: Mesics lecture 7   iteration and repetitive executions

Types of ‘C’ loopsTypes of ‘C’ loops

for while do-while

for(expression 1; expression 2 ; expression 3){ statement 1; statement 2; . statement N;}

expression 1;while(expression 2){ statement 1; statement 2; . statement N; expression 3;}

expression 1;do{ statement 1; statement 2; . statement N;} while(expression 3);

Page 7: Mesics lecture 7   iteration and repetitive executions

forfor Loop Loop

• for loop allows to execute a set of instruction until certain condition is satisfied.

• Condition may be predefined or open-ended.for(initialize counter; test condition; re-evaluation parameter){ statement 1; statement 2; . statement N;}

Page 8: Mesics lecture 7   iteration and repetitive executions

forfor Loop Loop

• The initialize counter sets to an initial value. This statement is executed only once.

• The test condition is a relational expression that determines the number of iterations desired or determines when to exit from the loop.

• The “for” loop continues to execute as long as conditional test is satisfied.

• When condition becomes false the control of program exists from the body of the “for” loop and executes next statement after the body of the loop.

Page 9: Mesics lecture 7   iteration and repetitive executions

forfor Loop Loop

• The re-evaluation parameter decides how to make changes int the loop (increment or decrement operations are to be used quite often).

• The body of loop may contain either a single statement or multiple statements.

Page 10: Mesics lecture 7   iteration and repetitive executions

forfor Loop Loop

Various formats of ‘for’ loop

SyntaxSyntax OutputOutput RemarksRemarks

for( ; ; ) Infinite loop No Arguments

for( a=0; a<=20 ; ) Infinite loop ‘a’ is neither incremented nor decremented

for( a=0; a<=10 ; a++)printf(“%d”, a);

Displays value from 0 to 10 ‘a’ is increment from 0 to 10. Curly braces are not necessary. Default scope of the for loop is one statement after the for loop.

for( a=10; a>=0 ; a--)printf(“%d”, a);

Displays value from 10 to 0 ‘a’ is decremented from 10 to 0.

Page 11: Mesics lecture 7   iteration and repetitive executions

Print the first five numbers starting from 1 together with their squares

void main()void main(){{ int I;int I; clrscr();clrscr(); for(i=1;i<=5;i++)for(i=1;i<=5;i++) {{ printf(“\Number :%d it’s Square : %d”, i, i * i); printf(“\Number :%d it’s Square : %d”, i, i * i); } }}}

OUTPUT :OUTPUT :Number : 1 it’s Square: 1Number : 1 it’s Square: 1Number : 2 it’s Square: 4Number : 2 it’s Square: 4Number : 3 it’s Square : 9Number : 3 it’s Square : 9Number : 4 it’s Square: 16Number : 4 it’s Square: 16Number : 5 it’s Square: 25Number : 5 it’s Square: 25

Page 12: Mesics lecture 7   iteration and repetitive executions

Print the first five numbers starting from 1 together with their squares

void main()void main(){{ int i;int i; clrscr();clrscr(); for(i=1;i<=5;i++)for(i=1;i<=5;i++) printf(“\Number :%d it’s Square : %d”, i, i * i);printf(“\Number :%d it’s Square : %d”, i, i * i);}}

OUTPUT :OUTPUT :Number : 1 it’s Square: 1Number : 1 it’s Square: 1Number : 2 it’s Square: 4Number : 2 it’s Square: 4Number : 3 it’s Square : 9Number : 3 it’s Square : 9Number : 4 it’s Square: 16Number : 4 it’s Square: 16Number : 5 it’s Square: 25Number : 5 it’s Square: 25

Page 13: Mesics lecture 7   iteration and repetitive executions

Print the first five numbers starting from 1 together with their squares

void main()void main(){{ int i=1;int i=1; clrscr();clrscr(); for(;i<=5;i++)for(;i<=5;i++) printf(“\Number :%d it’s Square : %d”, i, i * i);printf(“\Number :%d it’s Square : %d”, i, i * i);}}

OUTPUT :OUTPUT :Number : 1 it’s Square: 1Number : 1 it’s Square: 1Number : 2 it’s Square: 4Number : 2 it’s Square: 4Number : 3 it’s Square : 9Number : 3 it’s Square : 9Number : 4 it’s Square: 16Number : 4 it’s Square: 16Number : 5 it’s Square: 25Number : 5 it’s Square: 25

Page 14: Mesics lecture 7   iteration and repetitive executions

Print the first five numbers starting from 1 together with their squares

void main()void main(){{ int i=1;int i=1; clrscr();clrscr(); for(;i<=5;)for(;i<=5;) {{ printf(“\Number :%d it’s Square : %d”, i, i * i);printf(“\Number :%d it’s Square : %d”, i, i * i); i++;i++; } }}}

OUTPUT :OUTPUT :Number : 1 it’s Square: 1Number : 1 it’s Square: 1Number : 2 it’s Square: 4Number : 2 it’s Square: 4Number : 3 it’s Square : 9Number : 3 it’s Square : 9Number : 4 it’s Square: 16Number : 4 it’s Square: 16Number : 5 it’s Square: 25Number : 5 it’s Square: 25

Page 15: Mesics lecture 7   iteration and repetitive executions

Print the first five numbers starting from 1 together with their squares

void main()void main(){{ int I;int I; clrscr();clrscr(); for(i=1;i<=5;i++);for(i=1;i<=5;i++); {{ printf(“\Number :%d it’s Square : %d”, i, i * i); printf(“\Number :%d it’s Square : %d”, i, i * i); } } printf(“\Number :%d it’s Square : %d”, i, i * i);printf(“\Number :%d it’s Square : %d”, i, i * i); getch(); getch();}}

Page 16: Mesics lecture 7   iteration and repetitive executions

Nested Nested forfor loops loops

• We can use loop inside loop, which is known as nested loop

• In nested for loops one or more for statements are included in the body of the loop.

• The numbers of iterations in this type of structure will be equal to the number of iteration in the outer loop multiplied by the number of iterations in the inner loop.

Page 17: Mesics lecture 7   iteration and repetitive executions

Print the first five numbers starting from 1 together with their squares

void main()void main(){{ int a, b, sub;int a, b, sub; clrscr();clrscr(); for(a=3;a>=1;a--);for(a=3;a>=1;a--); {{ for(b=1; b<=2; b++)for(b=1; b<=2; b++) { { sub = a – b; sub = a – b; printf(“a = %d b = %d a – b = %d\n”, a, b, sub); printf(“a = %d b = %d a – b = %d\n”, a, b, sub); }} } } getch();getch();}}Output:Output:a = 3 b = 1 a – b = 2a = 3 b = 1 a – b = 2a = 3 b = 2 a – b = 1a = 3 b = 2 a – b = 1a = 2 b = 1 a – b = 1a = 2 b = 1 a – b = 1a = 2 b = 2 a – b = 0 a = 2 b = 2 a – b = 0 a = 2 b = 2 a – b = 0a = 2 b = 2 a – b = 0a = 1 b = 1 a – b = 0a = 1 b = 1 a – b = 0a = 1 b = 1 a – b = -1a = 1 b = 1 a – b = -1

Page 18: Mesics lecture 7   iteration and repetitive executions

whilewhile loop loop

• The while loop is frequently used in programs for the repeated execution of statement in a loop.

• The loop continues until a certain condition is satisfied.

Page 19: Mesics lecture 7   iteration and repetitive executions

whilewhile loop loopThe syntax of while loop

while(test-condition){ body of the loop;}

Page 20: Mesics lecture 7   iteration and repetitive executions

whilewhile loop loop

• The test condition is indicated at the top and its tests the value of the expression before processing the body of the loop.

• The loop statements will be executed till the condition is true.

• When the condition becomes false the execution will be out of the loop.

• The braces are needed only if the body of the loop contains more than one statement.

• However, it is a good practice to use braces even if the body of the loop contains only one statement.

Page 21: Mesics lecture 7   iteration and repetitive executions

• Steps of while loops are as follows– The test condition is

evaluated and if it is true, the body of the loop is executed.

– On execution of the body, test condition is repetitively checked and if it is true the body is executed.

– The process of execution of the body will be continued till the test condition becomes true.

– The control is transferred out of the loop if test condition fails.

Stop

Initialize

Test Condition

F

Body of the loop

Update

T

Entry

Page 22: Mesics lecture 7   iteration and repetitive executions

Print “You are in MESICS” string for nine times

void main()void main(){{ int i=1;int i=1; clrscr();clrscr(); while(i<=9)while(i<=9) {{ printf(“You are in MESICS\n”);printf(“You are in MESICS\n”); i++; i++; } }}}

OUTPUT :OUTPUT :You are in MESICSYou are in MESICSYou are in MESICSYou are in MESICSYou are in MESICSYou are in MESICSYou are in MESICSYou are in MESICSYou are in MESICSYou are in MESICSYou are in MESICSYou are in MESICSYou are in MESICSYou are in MESICSYou are in MESICSYou are in MESICSYou are in MESICSYou are in MESICS

Page 23: Mesics lecture 7   iteration and repetitive executions

do…whiledo…while loop loopThe syntax of do…while loop

do{ statements;} while(test-condition);

Page 24: Mesics lecture 7   iteration and repetitive executions

do…whiledo…while loop loop

• The difference between the while and do-while loop is the place where the condition is to be tested.

• In the while loops the condition is tested following the while statement, and then the body gets executed.

• In the do…while the condition is checked at the end of the loop.

• The do…while loop will execute at least one time even if the condition is false initially.

• The do…while loop executes until the condition becomes false.

Page 25: Mesics lecture 7   iteration and repetitive executions

Comparison of while and do..whileComparison of while and do..while

Sr. NoSr. No while loopwhile loop do…while loopdo…while loop

1. Condition is specified at the top.

Condition is mentioned at the bottom.

2.Body statement/s is/are executed when condition is satisfied.

Body statement/s executes even when the condition is false.

3. No brackets for a single statement.

Brackets are essential even when a single statement exists.

4. It is an entry-controlled loop. It is an exit-controlled loop.

Page 26: Mesics lecture 7   iteration and repetitive executions

Print “You are in MESICS” string for nine times

void main()void main(){{ int i=1;int i=1; clrscr();clrscr(); dodo {{ printf(“You are in MESICS\n”);printf(“You are in MESICS\n”); i++; i++; } while(i<=5); } while(i<=5);}}

OUTPUT :OUTPUT :You are in MESICSYou are in MESICSYou are in MESICSYou are in MESICSYou are in MESICSYou are in MESICSYou are in MESICSYou are in MESICSYou are in MESICSYou are in MESICS