"> ">
Download presentation
Presentation is loading. Please wait.
1
Iteration (Loop) partII
Thanachat Thanomkulabut
2
Switch-case example <expression> must be int, char, string
review 2 <expression> must be int, char, string char version int version char op; Console.Write("Select + - / * :"); op=char.Parse(Console.ReadLine()); switch(op) { case '+': Console.Write("{0}+{1}={2}", x,y,x+y); break; case '-': Console.Write("{0}-{1}={2}", x,y,x-y); : default: Console.Write("Try again"); } int day_num; day_num= int.Parse(Console.ReadLine()); switch(day_num ) { case 1: Console.Write ("Sunday"); break; case 2: console.Write("Monday"); : default : Console.Write(“Try again"); } <expression>
3
Switch-case example (continue)
review <expression> must be int, char, string string version string op; Console.Write("Select + - / * :"); op=Console.ReadLine(); switch(op) { case “+”: Console.Write("{0}+{1}={2}", x,y,x+y); break; case “-”: Console.Write("{0}-{1}={2}", x,y,x-y); : default: Console.Write("Try again"); } <expression>
4
Flowchart of Switch-case (1/2)
review int day_num; day_num= int.Parse(Console.ReadLine()); switch(day_num) { case 1: Console.Write ("Sunday"); break; case 2: console.Write("Monday"); : default : Console.Write(“Try again"); } statement1 label (day_num) label=1 label=2 default statement2 statement n (day_num=3) label=3 statement3 : (day_num=2) (day_num=1)
5
Flowchart of Switch-case (2/2)
review Flowchart of Switch-case (2/2) 5 int month; Console.Write("Input Month"); month = int.Parse(Console.ReadLine()); switch(month) {case 1: case 3: case 5: : Console.Write("This month = 31days"); break; case 4: case 6: Console.Write(" This month = 30days"); default : Console.Write ("Input again"); } label (month) label=1,3,5,… default : statement1 (month=1,3,5,…) label=4,6,… Statement2 (month=4,6,…) statement n
6
Outline For statements Nested loops Break statements
Continue statements
7
Looping or Iteration in C#
while Iteration (repeat) foreach do…while for
8
Kinds of Iteration statements
Conditional iteration while do...while Definite iteration For
9
for statement For Single Statement For Multiple Statements
for ( [initializers]; [condition]; [runner] ) statement; For Multiple Statements for ( [initializers]; [condition]; [runner] ) { statement-1; statement-2; . statement-N; }
10
for statement Example of for statement 1 2 3 4
START END condition true false Statement runner initializers for ( [initializers]; [condition]; [runner] ) statement; Example of for statement static void Main() { int i; for(i = 1; i <= 4; i++) Console.WriteLine(i); } FALSE TRUE Output 1 2 i 3 2 5 4 1 3 4
11
for statement example static void Main() Output { int k; A A A A A 5
for (k = 0; k <= 4; k++) Console.Write("A "); Console.Write(k); } Output A A A A A 5
12
for statement example Write the program to show even number 2,4,6,...,n static void Main() { int k,n; Console.Write(“Input n :”); n = int.Parse(Console.ReadLine()); for ( ; ; ) } k = 1 k <= n k++ if(k%2==0) Console.WriteLine(k);
13
for statement example Write the program to show even number 2,4,6,...,n static void Main() { int k,n; Console.Write(“Input n :”); n = int.Parse(Console.ReadLine()); for ( ; ; ) } k = 2 k <= n k = k+2 Console.WriteLine(k);
14
Test I : Find n factorial
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);
15
Test II : Problem #1: Find
5 i2 = 1*1 + 2*2 + 3*3 + 4*4 + 5*5 static void Main() { int N=0, SUM=0, i=0; Console.Write(”Please input number: ”); N = int.Parse(Console.ReadLine()); for ( ??? ; ??? ; ??? ) SUM = SUM + ??? ; Console.WriteLine(”SUM = {0}”, SUM); }
16
Outline For statements Nested loops Break statements
Continue statements
17
Nested Loops : Example Nested for loops
Do not use the same runner variable in the outer loop and the inner loop for (int outer = 1; outer <= 4; outer++) { for (int inner = 1; inner <=3; inner++) { Console.WriteLine("Outer = {1} & Inner = {0}" , inner ,outer); }
18
Nested Loops : Example Output Outer = 1 & Inner = 1
for (int outer = 1; outer <= 4; outer++) { for (int inner = 1; inner <=3; inner++) { Console.WriteLine("Outer = {1} & Inner = {0}" , inner ,outer); } Outer = 1 & Inner = 2 Outer = 1 & Inner = 3 Outer = 2 & Inner = 1 Outer = 2 & Inner = 2 Outer = 2 & Inner = 3 Outer = 3 & Inner = 1 Outer = 3 & Inner = 2 Outer = 3 & Inner = 3 Outer = 4 & Inner = 1 Outer = 4 & Inner = 2 Outer = 4 & Inner = 3
19
Nested Loops : Example Nested while loops int outer = 1;
while (outer <= 4) { int inner = 1 ; while (inner <= 3) { Console.WriteLine("Outer = {1} & Inner = {0}" , inner ,outer); inner := inner +1; } outer := outer +1;
20
Nested Loops : Example Nested do...while loops int outer = 1; do {
int inner = 1; Console.WriteLine("Outer = {1} & Inner = {0}" , inner ,outer); inner := inner +1 } while (inner < 6); outer := outer + 1; inner := 1 }while (outer < 5);
21
Test III : Nested Loops : Example
Write the program to Show output 1 12 123 1234 12345 int i,j; for( ; ; ) { for( , , ) { }
22
Break statement The break statement terminates the closest enclosing loop or switch statement in which it appears
23
Break statement example
int i,j; for(i=1;i<=10;i++) { Console.WriteLine(i); if(i==5) break; } Console.Write(“final i = {0}”,i); i 4 2 1 5 3 Output 1 2 3 4 5 final i = 5
24
Continue Statement i sum 1 3 2 +1 +3 +5 +7 +9 Output final sum = 25
3 2 +1 +3 +5 +7 +9 final sum = 25 int i=0,sum=0; do{ i++; if(i%2==0) continue; sum = sum+i; }while(i<10); Console.WriteLine("final sum={0}",sum);
25
Continue statement example
int i=0,sum=0; for(i=1;i<=10;i++ ) { if(i%2==0) continue; sum = sum+i; } Console.WriteLine("final sum={0}",sum);
26
Any question?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.