control structures week 4 control structures
play

Control Structures Week 4: Control Structures Week 4: Monchai - PowerPoint PPT Presentation

204111: Computer and 204111: Computer and Programming Programming Control Structures Week 4: Control Structures Week 4: Monchai Sopitkamon Sopitkamon, Ph.D. , Ph.D. Monchai Overview Overview Types of control structures Types of


  1. 204111: Computer and 204111: Computer and Programming Programming Control Structures Week 4: Control Structures Week 4: Monchai Sopitkamon Sopitkamon, Ph.D. , Ph.D. Monchai

  2. Overview Overview � Types of control structures Types of control structures � � Using selection structure Using selection structure � � Using repetition structure Using repetition structure �

  3. Types of control structures Types of control structures � C# provides 8 types of control structures C# provides 8 types of control structures � � one sequence structure � one sequence structure – – statements are executed statements are executed one after the other in the order they appear in the one after the other in the order they appear in the program. program. � three selection structure � three selection structure – – statement(s statement(s) is/are ) is/are executed if a condition is true or skipped if the executed if a condition is true or skipped if the condition is false. E.g., if if , , if/else if/else , , switch switch condition is false. E.g., � four repetition structure � four repetition structure – –statement(s statement(s) is/are ) is/are executed over and over again (loops) until or while a executed over and over again (loops) until or while a specific condition is satisfied. E.g., while while , , do/while do/while , , specific condition is satisfied. E.g., for , , foreach foreach . . for

  4. Selection Structures Selection Structures � if � if (single (single- -selection structure) selection structure) � if/else � if/else (double (double- -selection structure) selection structure) � switch � switch (multiple (multiple- -selection structure) selection structure)

  5. Selection Structure Selection Structure IF/ELSE IF if ( condition ) statement1 ; if ( condition ) else statement ; statement2 ; condition condition True False False True statement1 statement2 statement

  6. Selection Structure Selection Structure � switch � switch (multiple (multiple- -selection structure) selection structure) � syntax: � syntax: switch (< expression > ) { case < constant-expression > : < statements > ; break; : required default: < statements > ; optional break; } � < constant-expression> must be integral or string type.

  7. switch (< expression > ) { case < constant-expression > : < statements > ; Switch statement Switch statement break; : default: < statements > ; break; � < < expression expression > > is evaluated. is evaluated. � } � Result is compared to each case, sequentially. Result is compared to each case, sequentially. � � If a match is found, the statements after the If a match is found, the statements after the � case keyword are executed. case keyword are executed. � If no match is found, the (optional) default case � If no match is found, the (optional) default case takes effect. takes effect. � “ “break; break;” ” is required after each block, since C# is required after each block, since C# � does not support an explicit “ “fall fall- -through through” ” from from does not support an explicit one case label to another. However, it is one case label to another. However, it is allowed to stack case labels to specify that allowed to stack case labels to specify that certain statements are to be executed for certain statements are to be executed for several cases. several cases.

  8. Switch example - - Simple Calculator Simple Calculator Switch example If we want 'x' to be If we want 'x' to be double a, b; char op; another multiplicative another multiplicative double sol=0; operator as well, we can operator as well, we can a = double.Parse(Console.ReadLine()); insert a line as follows: insert a line as follows: op = char.Parse(Console.ReadLine()); b = double.Parse(Console.ReadLine()); switch(op) { case "+": sol = a+b; A “fall- break; case "* ": case "-": through” case "x": sol = a-b; case break; sol = a* b; case "*": example break; sol = a*b; break; case "/": sol = a/b; break; } Console.WriteLine("{0} {1} {2} = {3}", a, op, b, sol);

  9. do/while Statement : Syntax do/while Statement : Syntax do { do { Statement; Statement; Statement Statement; Statement; Statement Statement; Statement; } while Statement } while ( condition ) ; ( condition ) ; true condition could be boolean constant, boolean false variable, or boolean expression

  10. do/while Statement : Example do/while Statement : Example Output: Hello World Hello World Output: 3

  11. do/while Statement : Operation � Do/ while statement is a conditional iteration statements � The loop body of a do/ while statement will be executed first before the exit condition is checked � Do/while loop is also called post-conditional because the condition is checked after the loop body � A do/while statement always gets executed at least once

  12. do/while statement: Example1 do/while statement: Example1 (n = 0) int n = 0; n = 0; int do { do { write n Console.Write(“ “{ 0} { 0} “ “, n); , n); Console.Write( n+ + n+ + ; n+ + ; } while ( ( n < 5) n < 5); ; } while (n < 5)? False True Output: 0 1 2 3 4

  13. do/while statement: Example2 do/while statement: Example2 (flag = true) bool flag = flag = true true; ; bool string s; int int n; n; string s; read n do { do { false s= Console.ReadLine s= Console.ReadLine(); (); (n > 0)? n= int.Parse(s int.Parse(s); ); n= true set flag if (n > 0) if (n > 0) write n = false Console.Write(“ “{ 0} { 0} “ “ Console.Write( , n); , n); false else else (flag= = false)? flag = false false; ; flag = true } while (flag = = true); } while (flag = = true);

  14. While Statement : Syntax While Statement : Syntax could be boolean constant, boolean false variable, or boolean expression condition true { while (condition ) (condition ) { while Statement Statement; Statement; Statement Statement; Statement; Statement Statement; Statement; } }

  15. While Statement : Example While Statement : Example Output: Hello World Hello World Output: 3

  16. While Statement : Operation � Since the condition is checked before the execution of the loop body, a while loop is also called pre-conditional . � As long as the entry condition remains true, the while loop will keep repeating. � No loop will be executed if the entry condition is false from the beginning.

  17. While Statement : Example1 While Statement : Example1 (n=0) False (n < = 5) ? int n = 0; n = 0; int while (n < = 5) { while (n < = 5) { True Console.Write(“ “{ 0} { 0} “ “, n); , n); Console.Write( write n n+ + ; n+ + ; } } n+ + Output: 0 1 2 3 4 5

  18. While Statement : Example2 While Statement : Example2 bool flag = true; flag = true; bool False string s; int int n; n; string s; flag = = true while (flag= = true) { while (flag= = true) { true s= Console.ReadLine Console.ReadLine(); (); s= read n n= int.Parse(s int.Parse(s); ); n= False (n > 0) ? if (n > 0) if (n > 0) True Console.Write(n) ) Console.Write(n flag = false Write n else else flag = false false; ; flag = } }

  19. Sentinel- -Controlled Loops Controlled Loops Sentinel � Template: � Template: Read the first value of input variable Read the first value of input variable while (input variable is not equal to sentinel) while (input variable is not equal to sentinel) { { ... ... Read next value of input variable Read next value of input variable } }

  20. Sentinel- -Controlled Loops Controlled Loops Sentinel � Example: Accumulating exam scores � Example: Accumulating exam scores � Steps � Steps 1.Initialize total to 0 to 0 1.Initialize 2.Read the first value and assign it to score 2.Read the first value and assign it to score 3.While score is not the sentinel (e.g. - -1) do 1) do 3.While score is not the sentinel (e.g. 3.1 Add score 3.1 Add score to to total 3.2 Read the next value and assign it to score score 3.2 Read the next value and assign it to

  21. Sentinel- -Controlled Loops Controlled Loops Sentinel Use of Sentinel int total =0, score; � A sentinel is a value Convert.Write("Enter score (-1 to end): "); used to control the score=int.Parse((Console.ReadLine()); termination of a while (score != -1) { loop. total := total + score; � E.g. User can Convert.Write("Enter score (-1 to end): "); choose to use “–1” score=int.Parse((Console.ReadLine()); as the sentinel value } to terminate the iteration.

  22. Sentinel- -Controlled Loop: Example1 Controlled Loop: Example1 Sentinel

  23. Loops Controlled by Boolean Flags Loops Controlled by Boolean Flags � Flag: Flag: � � A Boolean variable whose value is changed from False � A Boolean variable whose value is changed from False to True when a particular event occurs to True when a particular event occurs � Boolean Flags Template: Boolean Flags Template: � Initialize flag to false Initialize flag to false while (flag is still False) { while (flag is still False) { ... ... Reset flag to True if event being monitored Reset flag to True if event being monitored occurs occurs } }

Recommend


More recommend