"> ">
Download presentation
Presentation is loading. Please wait.
1
Module5 Looping Techniques: Part 2
Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart University, Bangkok THAILAND
2
Switch-case example <expression> must be int, char, string
review Switch-case example <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 Switch-case example (continue) <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
review Flowchart of Switch-case 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"); } label (day_num) label=1 statement1 (day_num=1) label=2 statement2 (day_num=2) label=3 statement3 (day_num=3) : default statement n
5
Looping or Iteration in C#
while Iteration for foreach do…while
6
Outline do…while statement for statement
7
while statement while (condition) statement;
8
do…while statement For Single Statement do { statement;
} while (condition); For Multiple Statements do { statement-1; statement-2; . statement-N; } while (condition);
9
Sentinel Loops: example
static void Main() { int N, SUM; SUM = 0; N = 0; Console.Write(”Enter number or -1 to quit”); N = int.Parse(Console.ReadLine()); while (N != -1) { SUM = SUM+N; } Console.WriteLine(“The sum is {0}.”, SUM); 1st Time (redundant part)
10
Sentinel Loops: do…while Version
static void Main() { int N, SUM; SUM = 0; N = 0; do { Console.Write(”Enter number or -1 to quit”); N = int.Parse(Console.ReadLine()); if (N!= -1) SUM = SUM+N; } while (N != -1); Console.WriteLine(”The sum is {0}.”, SUM); }
11
Outline do…while statement for statement
12
for statement For Single Statement For Multiple Statements
for ( [initializers]; [expression]; [iterators] ) statement; For Multiple Statements for ( [initializers]; [expression]; [iterators] ) { statement-1; statement-2; . statement-N; }
13
for statement k = ? for ( [initializers]; [expression]; [iterators] )
START END expression true false Statement iterators initializers for ( [initializers]; [expression]; [iterators] ) statement; static void Main() { int k; for (k = 0; k <= 3; k++) Console.Write("A "); Console.Write(k); } Example of for statement k = ? A A A A 4
14
Display 1 to 5 on screen while version for version
static void Main() { int i; i = 1; while (i <= 5) { Console.WriteLine(“{0}”, i); i++; } while version static void Main() { int i; for (i=1; i<=5; i++) { Console.WriteLine(“{0}”, i); } for version
15
Problem #1 – Display 10 stars
START END expression true false Statement iterators initializers ********** static void Main() { int i=0; for ( ; ; ) Console.Write(”*”); } i=0 i = 0 i < 10 i++ i<10 write(“*”) i++ How to display N stars?
16
Problem #3 – Display 4x3 stars
**** static void Main() { int N=0, M=0; for ( ; ; ) { for ( ) { Console.Write(”*”); } Console.WriteLine(); M = 0 M < 3 M++ N = 0 N < 4 N++ How to display NxN stars?
17
Problem #4 – Triangle shape#1
*** ** * static void Main() { int N=0, M=0; for ( ; ; ) { for ( ; ; ) { Console.Write(”*”); } Console.WriteLine(); M = 3 M > 0 M-- N = 0 N < M N++
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.