1 / 33

Iteration (Loop) partI

Iteration (Loop) partI. Thanachat Thanomkulabut. Consider the following program!. How to write “Hello, Kitty” 2000 times?. using System; Namespace SimPleExample { class SimPleC { static void Main() { Console.WriteLine(”Hello, Kitty”); Console.WriteLine(”Hello, Kitty”);

christoffer
Download Presentation

Iteration (Loop) partI

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Iteration (Loop) partI Thanachat Thanomkulabut

  2. Consider the following program! How to write “Hello, Kitty” 2000 times? using System; Namespace SimPleExample { class SimPleC { static void Main() { Console.WriteLine(”Hello, Kitty”); Console.WriteLine(”Hello, Kitty”); Console.WriteLine(”Hello, Kitty”); Console.WriteLine(”Hello, Kitty”); Console.WriteLine(”Hello, Kitty”); Console.WriteLine(”Hello, Kitty”); Console.WriteLine(”Hello, Kitty”); } } } #Hello, Kitty 7 times

  3. Looping Version How to write “Hello, Kitty” 2000 times? using System; Namespace SimPleLoopExample { class SimLoopPleC { static void Main() { int i; i = 0; while (i<7) { Console.WriteLine(”Hello, Kitty”); i++; } } } } #Hello, Kitty 7 times

  4. Iteration (repeat) Looping or Iteration in C# while do…while foreach for

  5. Kinds of Iteration statements

  6. Outline • while statements • Loop controlled • do...while

  7. while statement For Single Statement while (condition) statement; while (condition) { statement-1; statement-2; . . . statement-N; } FALSE Condition TRUE Statement For Multiple Statements

  8. while...example 1. initialize value How this program works? N static void Main() { int N = 0; while (N < 3) { Console.WriteLine(”Kitty-{0}”, N); N++; } } 3 1 2 0 3. finish condition False TRUE Monitor 2. runner Kitty-0 Kitty-1 Kitty-2

  9. while...example Monitor Kitty-0 Kitty-0 How this program works? 1. initialize value Kitty-0 N static void Main() { int N = 0; while (N < 3) Console.WriteLine(”Kitty-{0}”, N); N++; } 0 3. finish condition TRUE { Loop infinite } 2. runner

  10. while...example Monitor SUM N Please input N: 5 static void Main() { int N, SUM; SUM = 0; N = 0; while (N >= 0) { Console.Write(”Please input N: ”); N = int.Parse(Console.ReadLine()); if (N>=0) SUM = SUM+N; } Console.WriteLine(“SUM = {0}”, SUM); } 5 0 -1 3 0 5 8 Please input N: 3 Please input N: -1 1. initialize value TRUE SUM = 8 FALSE 3. finish condition FALSE TRUE 2. runner

  11. while...example static void Main() { int n,x,fact;   Console.Write(“Enter value of n : “); n = int.Parse(Console.ReadLine()); fact = n; x = n-1; while (x >= 1) { fact = fact*x; x = x-1; } Console.WriteLine(“{0}! = {1}”,n,fact); } n x fact 4 4 0 3 2 1 *3 *2 *1 Monitor 1. initialize value Enter value of n : 4 FALSE TRUE 3. finish condition 4! = 24 2. runner

  12. Test I • Write the program which • Input : n (positive interger number) • Output : sumation of 1+2+3+...+n

  13. Test I static void Main() { } int n ; ,x=1 ; ,sum=0; Console.Write(“Enter value of n : ”); n = int.Parse(Console.ReadLine()); while(x<=n) { } sum = sum+x; x++; Console.Write(“sum = {0}”,sum);

  14. While Statements • Since the condition is checked before the execution of the loop body • While loop is also called a pre-conditional loop. • As long as the entry condition is still true, the while loop will be repeated • Zero loop will be executed if the entry condition is false at the beginning • while statements are conditional iteration statements which execute the loop body only if the entry condition is true

  15. Outline • while statements • Loop controlled • do...while

  16. Loop Controlled

  17. N <= 5 N++; Counter Controlled Monitor N 1 2 4 3 6 5 1 2 static void Main() { int N; N = 1; while ( ) { Console.WriteLine(”{0}”, N); } } 3 FALSE 4 TRUE 5 How to display even number 1 to J on screen?

  18. N <= J N%2==0 N++; Counter Controlled • Display only even number between 1 to J static void Main() { int N, J; J = int.Parse(Console.ReadLine()); N = 1; while ( ) { if ( ) Console.WriteLine(”{0}”, N); } } 2 4 6 8 . . . How to display odd number between 1 to J on screen?

  19. Loop Controlled

  20. Sentinel-Controlled Monitor SUM N Please input N: 5 static void Main() { int N, SUM; SUM = 0; N = 0; while (N >= 0) { Console.Write(”Please input N: ”); N = int.Parse(Console.ReadLine()); if (N>=0) SUM = SUM+N; } Console.WriteLine(“SUM = {0}”, SUM); } 0 5 0 5 3 -1 8 Please input N: 3 Please input N: -1 TRUE SUM = 8 FALSE FALSE TRUE

  21. Outline • while statements • Loop controlled • do...while

  22. Kinds of Iteration statements

  23. Statement3 Statement2 Statement1 do...while statement do{ statement1; statement2; statement3; }while (condition); true condition false

  24. do...while example How this program works? N Monitor 0 1 2 3 0 static void Main() { int N = 0; do{ Console.WriteLine(N); N++; }while (N < 3); } 1 2 FALSE TRUE

  25. do...while example Input your password : Kaset How this program works? Input your password : bkk input static void Main() { string input; do{ Console.Write("Input your password:"); input = Console.ReadLine(); }while(input != "ABCD"); Console.WriteLine("Password Correct");} Input your password : ABCD bkk Kaset ABCD Password Correct TRUE FALSE

  26. do...while example How this partial code works? Monitor n x Input : 6 int n,x=1; Console.Write("Input:"); n = int.Parse(Console.ReadLine()); Console.WriteLine("Factor of {0} is",n); do{ if(n%x==0) Console.WriteLine(x); x++; }while(x<=n); 6 7 1 6 5 4 3 2 Factor of 6 is 1 2 3 FALSE TRUE 6 TRUE FALSE

  27. Test II • Write the program • Input : n (negative interger number) • Output : sumation of (-1)+(-2)+(-3)+...+n

  28. Test II static void Main() { } int n ; ,x=-1 ; ,sum=0; Console.Write(“Enter value of n : ”); n = int.Parse(Console.ReadLine()); do{ }while(x>=n); sum = sum+x; x--; Console.Write(“sum = {0}”,sum);

  29. Do..while Statement • The loop body of a do..while statement will be executed first before the exit condition is checked • Do..while loop is also called a post-conditional loop because the condition is checked after the loop body • Do..while statement is a conditional iteration statements • A do..while statement will execute at least once

  30. Statement Statement Statement Do..while vs. While Do..while statement While statement false condition true Statement Statement true condition Statement false

  31. while & do..while more example • This program will Reverse integer input • Example • input : 1502 • output : 2051 static void Main() { int number, right_digit;   Console.Write("Enter your number : "); number = int.Parse(Console.ReadLine()); Console.Write("Reverse number is : "); while (number != 0) { right_digit = number%10; Console.Write("{0}",right_digit); number = number/10; } }

  32. while & do..while more example static void Main() { int n, count, k;   Console.Write ("Please enter number of n : "); n = int.Parse(Console.ReadLine()); k = 1; count = 1; do{ Console.Write("{0} ",k); k = k+2; count = count+1; }while (count<=n); } • This program will show odd number n numbers • Example • input : 5 • output : 1 3 5 7 9

  33. Any question?

More Related