Presentation is loading. Please wait.

Presentation is loading. Please wait.

Iteration (Loop) partI Thanachat Thanomkulabut. Consider the following program! using System; Namespace SimPleExample { class SimPleC { class SimPleC.

Similar presentations


Presentation on theme: "Iteration (Loop) partI Thanachat Thanomkulabut. Consider the following program! using System; Namespace SimPleExample { class SimPleC { class SimPleC."— Presentation transcript:

1 Iteration (Loop) partI Thanachat Thanomkulabut

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

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

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

5 Kinds of Iteration statements 5 Iteration Conditional iteration whiledo...while Definite iteration For

6 Outline  while statements  Loop controls  do...while 6

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

8 while...example static void Main() { int n = 0; int n = 0; while (n < 3) while (n < 3) { Console.WriteLine(””,n); Console.WriteLine(” Current value of n: {0} ”,n); n++; } n++; }} n 0 TRUE Current value of n: 0 Monitor 1 Current value of n: 1 2 Current value of n: 2 3 FALSE 8 How this program works? 1. initialize value 2. runner 3. condition

9 while...example static void Main() { int n = 0; int n = 0; while (n < 3) while (n < 3) Console.WriteLine(” { 0}”, n); Console.WriteLine(” Current value of n: { 0}”, n); n++; n++;} n 0 TRUE Current value of n: 0 Monitor Current value of n: 0 { } Infinite Loop 9 How this program works? 1. initialize value 3. condition 2. runner

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

11 while...example 11 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); } nxfact Enter value of n : Monitor 4 4 4 3 TRUE *32*21*10 FALSE 4! = 24 1. initialize value 2. runner 3. finish condition

12 Test I 12  Write a program which  Input : n (positive integer number)  Output : summation of 1+2+3+...+n

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

14 While Statements 14  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 controls  do...while 15

16 Loop Controls 16 Loop Controls Counter Controlled Sentinel Controlled

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

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

19 Loop Controls 19 Loop Controls Counter Controlled Sentinel Controlled

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

21 Outline  while statements  Loop controls  do...while 21

22 Kinds of Iteration statements 22 Iteration Conditional iteration whiledo...while Definite iteration For

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

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

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

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

27 Test II 27  Write a program  Input : n (negative integer number)  Output : summation of (-1)+(-2)+(-3) +...+n

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

29 Do..while Statement 29  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

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

31 while & do..while more example 31 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; }  This program will reverse integer input  Example  input : 1502  output : 2051 number right_digit 1502 FALSE TRUE 2 15015 051 0 Monitor 2 20 2052051 1 Reverse number is:

32 while & do..while more example 32  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); } k count Monitor 1 1 TRUEFALSE 3579 1 2 3 3 5 4 7 5 9 11 6 n 5

33 Any question?


Download ppt "Iteration (Loop) partI Thanachat Thanomkulabut. Consider the following program! using System; Namespace SimPleExample { class SimPleC { class SimPleC."

Similar presentations


Ads by Google