350 likes | 441 Views
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”);
E N D
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”); Console.WriteLine(”Hello, Kitty”); Console.WriteLine(”Hello, Kitty”); Console.WriteLine(”Hello, Kitty”); Console.WriteLine(”Hello, Kitty”); Console.WriteLine(”Hello, Kitty”); } } } #Hello, Kitty 7 times
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
Iteration (repeat) Looping or Iteration in C# while do…while foreach for
Outline • while statements • Loop controls • do...while
while statement For Single Statement while (condition) statement; while (condition) { statement 1; statement 2; . . . statement N; } FALSE Condition TRUE Statement For Multiple Statements
while...example 1. initialize value How this program works? n static void Main() { intn = 0; while (n < 3) { Console.WriteLine(”Current value of n: {0}”,n); n++; } } 3 1 2 0 3. condition FALSE TRUE Monitor 2. runner Current value of n: 0 Current value of n: 1 Current value of n: 2
while...example Monitor Current value of n: 0 Current value of n: 0 How this program works? 1. initialize value Current value of n: 0 n static void Main() { intn = 0; while (n < 3) Console.WriteLine(”Current value of n:{0}”, n); n++; } 0 3. condition TRUE { Infinite Loop } 2. runner
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. condition FALSE TRUE 2. runner
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
Test I • Write a program which • Input : n (positive integer number) • Output : summation of 1+2+3+...+n
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);
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
Outline • while statements • Loop controls • do...while
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 numbers 1 to J on screen?
N <= J N%2==0 N++; Counter Controlled • Display only even numbers 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 numbers between 1 to J on screen?
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
Outline • while statements • Loop controls • do...while
Statement3 Statement2 Statement1 do...while statement do{ statement1; statement2; statement3; }while (condition); true condition false
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
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
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 6 7 1 5 3 2 4 Factor of 6 is 1 2 3 FALSE TRUE 6 TRUE FALSE
Test II • Write a program • Input : n (negative integer number) • Output : summation of (-1)+(-2)+(-3) +...+n
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);
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 be executed at least once
Statement Statement Statement Do..while vs. While Do..while statement While statement false condition true Statement Statement true condition Statement false
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; } } TRUE FALSE number right_digit 1 5 0 2 1 0 15 1502 150 Monitor Reverse number is: 2051 2 205 20
while & do..while more example • This program shows odd n numbers starting from 1 • Example • input : 5 • output : 1 3 5 7 9 static void Main() {int n, count, k; Console.Write ("Please enter number of odd numbers: "); n = int.Parse(Console.ReadLine()); k = 1; count = 1; do{ Console.Write("{0} ",k); k = k+2; count = count+1; }while (count<=n); } n k count TRUE FALSE 5 5 9 3 11 1 7 2 1 4 5 6 3 Monitor 1 3 5 9 7