Your SlideShare is downloading. ×
Mesics lecture 7   iteration and repetitive executions
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Mesics lecture 7 iteration and repetitive executions

651
views

Published on

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

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

Published in: Technology, Health & Medicine

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
651
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
39
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Iteration and Repetitive Executions www.eshikshak.co.in
  • 2. Introduction• 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.
  • 3. 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
  • 4. Counter 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
  • 5. Sentinel-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.
  • 6. Types of ‘C’ loopsfor while do-whilefor(expression 1; expression 2 expression 1; expression 1;; expression 3) while(expression 2) do{ { { statement 1; statement 1; statement 1; statement 2; statement 2; statement 2; . . . statement N; statement N; statement N;} expression 3; } while(expression 3); }
  • 7. for 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;}
  • 8. for 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.
  • 9. for 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.
  • 10. for LoopVarious formats of ‘for’ loopSyntax Output Remarksfor( ; ; ) Infinite loop No Argumentsfor( a=0; a<=20 ; ) Infinite loop ‘a’ is neither incremented nor decrementedfor( a=0; a<=10 ; a++) Displays value from 0 to 10 ‘a’ is increment from 0 to 10.printf(“%d”, a); Curly braces are not necessary. Default scope of the for loop is one statement after the for loop.for( a=10; a>=0 ; a--) Displays value from 10 to 0 ‘a’ is decremented from 10 to 0.printf(“%d”, a);
  • 11. Print the first five numbers starting from 1 together with their squaresvoid main(){ int I; clrscr(); for(i=1;i<=5;i++) { printf(“Number :%d it’s Square : %d”, i, i * i); }}OUTPUT :Number : 1 it’s Square: 1Number : 2 it’s Square: 4Number : 3 it’s Square : 9Number : 4 it’s Square: 16Number : 5 it’s Square: 25
  • 12. Print the first five numbers starting from 1 together with their squaresvoid main(){ int i; clrscr(); for(i=1;i<=5;i++) printf(“Number :%d it’s Square : %d”, i, i * i);}OUTPUT :Number : 1 it’s Square: 1Number : 2 it’s Square: 4Number : 3 it’s Square : 9Number : 4 it’s Square: 16Number : 5 it’s Square: 25
  • 13. Print the first five numbers starting from 1 together with their squaresvoid main(){ int i=1; clrscr(); for(;i<=5;i++) printf(“Number :%d it’s Square : %d”, i, i * i);}OUTPUT :Number : 1 it’s Square: 1Number : 2 it’s Square: 4Number : 3 it’s Square : 9Number : 4 it’s Square: 16Number : 5 it’s Square: 25
  • 14. Print the first five numbers starting from 1 together with their squaresvoid main(){ int i=1; clrscr(); for(;i<=5;) { printf(“Number :%d it’s Square : %d”, i, i * i); i++; }}OUTPUT :Number : 1 it’s Square: 1Number : 2 it’s Square: 4Number : 3 it’s Square : 9Number : 4 it’s Square: 16Number : 5 it’s Square: 25
  • 15. Print the first five numbers starting from 1 together with their squaresvoid main(){ int I; clrscr(); 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); getch();}
  • 16. Nested for 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.
  • 17. Print the first five numbers starting from 1 together with their squaresvoid main(){ int a, b, sub; clrscr(); for(a=3;a>=1;a--); { for(b=1; b<=2; b++) { sub = a – b; printf(“a = %d b = %d a – b = %dn”, a, b, sub); } } getch();}Output:a=3b=1a–b=2a=3b=2a–b=1a=2b=1a–b=1a=2b=2a–b=0a=2b=2a–b=0a=1b=1a–b=0a = 1 b = 1 a – b = -1
  • 18. while 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.
  • 19. while loopThe syntax of while loopwhile(test-condition){ body of the loop;}
  • 20. while 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.
  • 21. • Steps of while loops are as Entry follows – The test condition is Initialize evaluated and if it is true, the body of the loop is executed. – On execution of the body, F Test test condition is repetitively Condition checked and if it is true the body is executed. Stop T – The process of execution of the body will be continued tillBody of the loop the test condition becomes true. – The control is transferred out Update of the loop if test condition fails.
  • 22. Print “You are in MESICS” string for nine times OUTPUT : You are in MESICS You are in MESICSvoid main() You are in MESICS{ You are in MESICS You are in MESICS int i=1; You are in MESICS clrscr(); You are in MESICS You are in MESICS while(i<=9) You are in MESICS { printf(“You are in MESICSn”); i++; }}
  • 23. do…while loopThe syntax of do…while loopdo{ statements;} while(test-condition);
  • 24. do…while 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.
  • 25. Comparison of while and do..whileSr. No while loop do…while loop Condition is specified at the Condition is mentioned at the1. top. bottom. Body statement/s is/are Body statement/s executes even2. executed when condition is when the condition is false. satisfied. No brackets for a single Brackets are essential even when a3. statement. single statement exists.4. It is an entry-controlled loop. It is an exit-controlled loop.
  • 26. Print “You are in MESICS” string for nine times OUTPUT : You are in MESICS You are in MESICSvoid main() You are in MESICS{ You are in MESICS You are in MESICS int i=1; clrscr(); do { printf(“You are in MESICSn”); i++; } while(i<=5);}
  • 27. Sr. No. Program1.