Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 st Semester 2005 1 Module4-1 Iteration statement - while อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer.

Similar presentations


Presentation on theme: "1 st Semester 2005 1 Module4-1 Iteration statement - while อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer."— Presentation transcript:

1 1 st Semester 2005 1 Module4-1 Iteration statement - while อภิรักษ์ จันทร์สร้าง Aphirak Jansang aphirak.j@ku.ac.th http://www.cpe.ku.ac.th/~aphirak Computer Engineering Department Kasetsart University, Bangkok THAILAND

2 1 st Semester 2005 2/39 Outline  Review condition statement  Loop statements while statement do…while statement for statement  break statement

3 1 st Semester 2005 3/39 Simple C# Structure  Namespace Class  Main()Namespace Class Main() Variable & Constant Location statements WriteLine, ReadLine, if, switch, Loop Review condition statement

4 1 st Semester 2005 4/39 Condition Statements condition if (condition) statement; condition if (condition) statement1; else statement2; ( ) switch ( ) { case : ; break; default: ; break; } Review condition statement

5 1 st Semester 2005 5/39 Outline  Review condition statement  Loop statements while statement do…while statement for statement  break statement

6 1 st Semester 2005 6/39 Loop or Iteration in C# for while do…while foreach Iteration Loop statements

7 1 st Semester 2005 7/39 C# Program with Condition Start statement1 statement2 statement3statement4 End Condition true false Loop statements

8 1 st Semester 2005 8/39 Simple C# Program Start statement1 End statement1 Loop statements

9 1 st Semester 2005 9/39 Simple C# Program with Loop Start x=5; statement1 x=x-1; End x>0 true false Loop statements

10 1 st Semester 2005 10/39 Outline  Review condition statement  Loop statements while statement do…while statement for statement  break statement

11 1 st Semester 2005 11/39 while statement condition statement true false while (condition) statement; while statement

12 1 st Semester 2005 12/39 while statement – more statements while (condition) { statement1; statement2; } condition statement1 true false statement2 while statement

13 1 st Semester 2005 13/39 Example1: while statement  2 n = ???  n = 5 result=2*2*2*2*2 = 32 result=2*2*2*2*2 = 32 result=1; result = result*2; n=n-1; n>0 true false n=5; Display result See demo program!!!! prog2-loop.cs while statement

14 1 st Semester 2005 14/39 Example2: while statement  n = 5 result = 1+2+3+4+5 result = 1+2+3+4+5 result=0; result = result+n; n=n-1; n>0 true false n=5; Display result See demo program!!!! prog3-loop.cs while statement

15 1 st Semester 2005 15/39 Iteration Template  Counter Controlled  Sentinel Controlled while statement

16 1 st Semester 2005 16/39 Counter Controlled  Control Variable – counter variable  Three component Initial counter variable condition (counter variable is still valid?) Incrementing/Decrementing counter variable while statement: counter controlled

17 1 st Semester 2005 17/39 Example3: Count Controlled  Display 1 – 7 on screen initial counter statement; dec||inc counter condition true false See demo program!!!! prog4-loop.cs while statement: counter controlled

18 1 st Semester 2005 18/39 Break!!!! (5 minutes) (Elevator) Neo: There is no spoon... there's a difference between knowing the path and walking the path... Morpheus: Neo, sooner or later you're going to realize, just as I did, there's a difference between knowing the path and walking the path... Trinity: Run, Neo. Run! What is he doing? Morpheus: He's beginning to believe * From The Matrix

19 1 st Semester 2005 19/39 Iteration Template  Counter Controlled  Sentinel Controlled while statement

20 1 st Semester 2005 20/39 Sentinel Controlled  Input driven program  #repetition cannot tell  depend on condition and input  Sentinel Value Causes loop to break while statement: sentinel controlled

21 1 st Semester 2005 21/39 Example4: Sentinel Controlled  Find summation from user input  Exit program when user input negative number See demo program!!!! prog5-loop.cs while statement: sentinel controlled

22 1 st Semester 2005 22/39 Break 3 Minutes “With great power comes with great responsibility” *From Spiderman

23 1 st Semester 2005 23/39 Example 5: Display n stars See demo program!!!! prog6-loop.cs while statement: example

24 1 st Semester 2005 24/39 Example 6: Display stars See demo!!!! prog7-loop.cs while statement: example

25 1 st Semester 2005 25/39 Example 7: Display stars See demo!!!! prog8-loop.cs while statement: example

26 1 st Semester 2005 26/39 Example 8: Display stars See demo!!!! prog9-loop.cs while statement: example

27 1 st Semester 2005 27/39 Summary  Loop statements while statement do…while statement for statement  break statement


Download ppt "1 st Semester 2005 1 Module4-1 Iteration statement - while อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer."

Similar presentations


Ads by Google