Module 5 Looping Techniques Part 2 Thanawin Rakthanmanon

  • Slides: 20
Download presentation
Module 5 Looping Techniques: Part 2 Thanawin Rakthanmanon Email: fengtwr@ku. ac. th Create by:

Module 5 Looping Techniques: Part 2 Thanawin Rakthanmanon Email: [email protected] ac. th Create by: Aphirak Jansang Computer Engineering Department Kasetsart University, Bangkok THAILAND 1 nd Semester 200 7 1

review Switch-case example <expression>must be int, char, string char version int version char op;

review Switch-case example <expression>must be int, char, string char version int version char op; int day_num; Console. Write("Select + - / * : "); day_num= op=char. Parse(Console. Read. Line()); int. Parse(Console. Read. Line()); switch(op) { <expression> case '+': Console. Write("{0}+{1}={2}", x, y, x+y); break; case '-': Console. Write("{0}-{1}={2}", x, y, x-y); break; : default: Console. Write("Try again"); break; } nd 1 Semester 200 7 switch(day_num ) { case 1: Console. Write ("Sunday"); break; case 2: console. Write("Monday"); break; : default : Console. Write(“Try again"); break; } 2

review Switch-case example (continue) <expression>must be int, char, string version string op; Console. Write("Select

review Switch-case example (continue) <expression>must be int, char, string version string op; Console. Write("Select + - / * : "); op=Console. Read. Line(); switch(op) { <expression> case “+”: Console. Write("{0}+{1}={2}", x, y, x+y); break; case “-”: Console. Write("{0}-{1}={2}", x, y, x-y); break; : default: Console. Write("Try again"); break; } 1 nd Semester 200 7 3

review Flowchart of Switch-case int day_num; day_num= int. Parse(Console. Read. Line()); switch(day_num ) {

review Flowchart of Switch-case int day_num; day_num= int. Parse(Console. Read. Line()); switch(day_num ) { case 1: Console. Write ("Sunday"); break; case 2: console. Write("Monday"); break; : default : Console. Write(“Try again"); break; } 1 nd Semester 200 7 label (day_num) label=1 statement 1 (day_num=1) label=2 statement 2 (day_num=2) label=3 statement 3 (day_num=3) : : default statementn 4

Looping or Iteration in C# while for Iteration foreach do…while 1 nd Semester 200

Looping or Iteration in C# while for Iteration foreach do…while 1 nd Semester 200 7 5

Outline do…whilestatement ¡ for statement ¡ 1 nd Semester 200 7 6

Outline do…whilestatement ¡ for statement ¡ 1 nd Semester 200 7 6

while statement while (condition) statement; 1 nd Semester 200 7 7

while statement while (condition) statement; 1 nd Semester 200 7 7

do…while statement For Single Statement do { statement; } while (condition); For Multiple Statements

do…while statement For Single Statement do { statement; } while (condition); For Multiple Statements do { statement-1; statement-2; . 1 nd Semester 200 7 statement-N; } while (condition); 8

Sentinel Loops: example static void Main() { int N, SUM; SUM = 0; 1

Sentinel Loops: example static void Main() { int N, SUM; SUM = 0; 1 st Time (redundant part) N = 0; Console. Write(”Enter number or -1 to quit”); N = int. Parse(Console. Read. Line()); while (N != -1) { SUM = SUM+N; Console. Write(”Enter number or -1 to quit”); N = int. Parse(Console. Read. Line()); } Console. Write. Line(“The sum is {0}. ”, SUM); } 1 nd Semester 200 7 9

Sentinel Loops: do…while Version static void Main() { int N, SUM; SUM = 0;

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. Read. Line()); if (N!= -1) SUM = SUM+N; } while (N != -1); Console. Write. Line(”The sum is {0}. ”, SUM); } 1 nd Semester 200 7 10

review Factorial Number: C# Program ¡ QUIZ 1 N! = N * (N-1) *

review Factorial Number: C# Program ¡ QUIZ 1 N! = N * (N-1) * (N-2) * (N-3) *. . . * 1 Example 5! = 5*4*3*2*1 static void Main() { int N, FACT=1; FACT = 1; Console. Write(”Please input number: ”); N = int. Parse(Console. Read. Line()); Find N! N > 0 ) while ( ? ? ? { FACT * N FACT = ? ? ? ? ? N-- ; ; }; Console. Write. Line(”The factorial is {0}. ”, FACT); } 1 nd Semester 200 7 11

Outline do…whilestatement ¡ for statement ¡ 1 nd Semester 200 7 12

Outline do…whilestatement ¡ for statement ¡ 1 nd Semester 200 7 12

for statement For Single Statement for ( [initializers]; [expression]; [iterators] ) statement; For Multiple

for statement For Single Statement for ( [initializers]; [expression]; [iterators] ) statement; For Multiple Statements for ( [initializers]; [expression]; [iterators] ) { statement-1; statement-2; . . statement-N; } 1 nd Semester 200 7 13

for statement START for ( [initializers]; [expression]; [iterators] ) statement; initializers Example of for

for statement START for ( [initializers]; [expression]; [iterators] ) statement; initializers Example of for statement static void Main() { int k; for (k = 0; k <= 3; k++) Console. Write("A "); Console. Write(k); } k=? expression false true Statement iterators AAAA 4 1 nd Semester 200 7 END 14

Display 1 to 5 on screen static void Main() { int i; i =

Display 1 to 5 on screen static void Main() { int i; i = 1; while (i <= 5) { Console. Write. Line(“{0}”, i); while version i++; } } static void Main() { int i; for (i=1; i<=5; i++) { Console. Write. Line(“{0}”, i); } } 1 nd Semester 200 7 for version 15

Problem #1 – Display 10 stars START ***** initializers i=0 static void Main() {

Problem #1 – Display 10 stars START ***** initializers i=0 static void Main() { int i=0; for ( i = 0 ; i < 10 ; i++ ) Console. Write(”*”); expression true false i<10 Statement write(“*”) } iterators i++ How to display N stars? END 1 nd Semester 200 7 16

QUIZ 2 Problem #1: Find 5 i 2 = 1*1 + 2*2 + 3*3

QUIZ 2 Problem #1: Find 5 i 2 = 1*1 + 2*2 + 3*3 + 4*4 + 5*5 i=1 static void Main() { int N=0, SUM=0, i=0; Console. Write(”Please input number: ”); N = int. Parse(Console. Read. Line()); for ( i <= N i ? ? ? =1 ; ? ? ? SUM = SUM + ; i++ ? ? ? ) ? ? i*i ; Console. Write. Line(”SUM = {0}”, SUM); } 1 nd Semester 200 7 17

Problem #3 – Display 4 x 3 stars **** static void Main() { int

Problem #3 – Display 4 x 3 stars **** static void Main() { int N=0, M=0; for ( M = 0 ; M<3 ; M++ ) { for ( N = 0 ; N < 4 ; N++ ) { Console. Write(”*”); } Console. Write. Line(); } } How to display Nx. N stars? 1 nd Semester 200 7 18

Problem #4 – Triangle shape#1 *** static void Main() { ** int N=0, M=0;

Problem #4 – Triangle shape#1 *** static void Main() { ** int N=0, M=0; * for ( M = 3 ; M>0 ; M-- for ( N = 0 ; N < M ; N++ Console. Write(”*”); } Console. Write. Line(); ) { } } 1 nd Semester 200 7 19

Problem #5: Find 2 n QUIZ 3 static void Main() { int N=0, SUM=1,

Problem #5: Find 2 n QUIZ 3 static void Main() { int N=0, SUM=1, i=1; Console. Write(”Please input number: ”); N = int. Parse(Console. Read. Line()); while (i<=N){ SUM = SUM*2; i++; } Console. Write. Line(”SUM = {0}”, SUM); } for ( i = 1 ; i <= N SUM = SUM * 2; 1 nd Semester 200 7 ; i++ ) 20