1 / 29

Module 3 Selection Statement

Module 3 Selection Statement. Thanawin Rakthanmanon Email: fengtwr@ku.ac.th Create by: Aphirak Jansang Computer Engineering Department Kasetsart University, Bangkok THAILAND. Outline. Boolean expression if statement nested if statement switch case statement. Boolean Expression.

khogan
Télécharger la présentation

Module 3 Selection Statement

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Module3Selection Statement Thanawin Rakthanmanon Email: fengtwr@ku.ac.th Create by: Aphirak Jansang Computer Engineering Department Kasetsart University, Bangkok THAILAND

  2. Outline • Boolean expression • if statement • nested if statement • switch case statement

  3. Boolean Expression • Operators • Comparison • Equal == • Not equal != • Less < • Greater > • Less than or equal to <= • Greater than or equal to >= • Boolean • And && • Or || • Not !

  4. Boolean Expression Example • From the equation: X2+9X+10 = 0 • How can we check that value of X is the answer for above equation? • Condition: Is value Y even number? ((X*X +9*X +10) == 0)//true if X is the answer (Y%2 == 0)//true if Y is even OR (Y%2 != 1)//true if Y is even

  5. Example: Boolean Expressions double x = 4.0; Expression Value x < 5.0 ___________ x > 5.0 ___________ x <= 5.0 ___________ 5.0 == x ___________ x != 5.0 ___________ true false true false true

  6. Outline • Boolean expression • if statement • nested if statement • switch case statement

  7. if statement • Execute the specific statement when the ”condition” becomes true • Syntax: if (condition) statement;//true if (condition){ statement1; //true statement2; //true }

  8. Weight in Kilograms BMI = (Height in Meters) X (Height in Meters) if statement example • BMI (Body Mass Index)

  9. if…else… statement • If condition is true execute statement1 • If condition isfalse execute statement2 • Syntax: if (condition) statement1; //true else{ statement2; //false statement3; //false } if (condition) statement1; //true else statement2; //false

  10. if…else… statement example • Question • Value in variable N is Odd or Even Number? if (___________________) Console.WriteLine(“It’s even number.”); else Console.WriteLine(“It’s odd number.”);

  11. y z Quiz • Fill the following blank • What x, y, z are called ? • Rewrite this sentence • 10110102 = ? X%2== 0 if (___________________) Console.WriteLine(“It’s even number.”); else Console.WriteLine(“It’s odd number.”); x1 x2 int Width,High; Width=10; High=5; int _______; int _______; Hint: 26 + 24 + 23 + 21 = ?

  12. Outline • Boolean expression • if statement • nested if statement • switch case statement

  13. Nested if statement int N; N = int.Parse(Console.ReadLine()); if (N >= 0) { if (N==0) Console.WriteLine(“N is zero number”); else Console.WriteLine(“N is positive number”); } else Console.WriteLine(“N is negative number”); if#1 if#2

  14. if#1 if#1 if#2 if#3 else#1 = if#2 if#3 Nested IF Overview

  15. 2x+10, x ≤ 5 x2+10, 5 < x ≤ 20 f(x) = x3+10, x > 20 Nested if statement

  16. 2x+10, x ≤ 5 f(x) = x2+10, 5 < x ≤ 20 x3+10, x > 20 Nested if statement double fx = 0; double x = double.Parse(Console.ReadLine()); #1 if ( ) #2 fx = 2*x + 10; #3 else if ( ) #4 fx = x*x + 10; #5 else #6 fx = x*x*x + 10; #7 #8 Console.WriteLine(“f(x) = {0}”, fx); X <= 5 5 < x <= 20

  17. Outline • Boolean expression • if statement • nested if statement • switch case statement

  18. switch…case statement • For selecting a statement where its label corresponds to the value of the switch expression. switch (<expression>) { case <constant-expression>: <statements>; break; [default: <statements>; break;] } <expression> must be int, char, string

  19. Example: switch-case (1) int day_num; Console.Write("Input the day"); day_num = int.Parse(Console.ReadLine()); switch(day_num) {case1: Console.Write ("Today is Sunday"); break; case2: Console.Write("Today is Monday"); break; : default : Console.Write ("I don’t know"); break; } <expression> <constant-expression>

  20. Example: switch-case (2) int month; Console.Write("Input Month"); month = int.Parse(Console.ReadLine()); switch(month) {case1: case3: case5: Console.Write("This month has 31day"); break; case4: case6: Console.Write("This month has 30day"); break; default : Console.Write ("Input again"); break; }

  21. Example: switch-case (3) <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); break; : default: Console.Write("Try again"); break; } int day_num; day_num= int.Parse(Console.ReadLine()); switch(day_num ) { case 1: Console.Write ("Sunday"); break; case 2: console.Write("Monday"); break; : default : Console.Write(“Try again"); break; } <expression> <constant-expression>

  22. Example: switch-case (4) <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); break; : default: Console.Write("Try again"); break; } <expression> <constant-expression>

  23. if else version if (a == 1 || a == 2) Console.WriteLine("Hi"); else if (a == 3 || a == 4) Console.WriteLine("Hello"); else Console.WriteLine("Bye"); switch version without default switch (a) { case 1 : case 2 : Console.WriteLine("Hi"); break; } Convert switch-case to if else switch version with default int a; a= int.Parse(Console.ReadLine()); switch (a) { case 1 : case 2 : Console.WriteLine("Hi"); break; case 3 : case 4 : Console.WriteLine("Hello"); break; default : Console.WriteLine("Bye"); break; } <expression> must be int, char, string <constant-expression>

  24. Flowchart Symbols Overview • Graphical representation Terminator Process Input/output Condition Connector Flow line

  25. START statement1 statement2 statement3 statement4 END Program Flowchart Example

  26. if statement flowchart START statement1 if (condition) statement2; //true else{ statement3; //false } statement4; statement1 CONDITION false true statement3 statement2 statement4

  27. 2x+10, x ≤ 5 f(x) = x2+10, 5 < x ≤ 20 x3+10, x > 20 Quiz2 • Fill blanks in the program. • Write the program flowchart. double fx = 0; double x = double.Parse(Console.ReadLine()); if ( ) fx = 2*x + 10; else if ( ) fx = x*x + 10; else fx = x*x*x + 10; Console.WriteLine(“f(x) = {0}”, fx);

  28. Selection Problems Summary • Boolean Expression • Selection Statements • if...else... Statement • switch-case Statement if…else… switch

  29. Exercise1 (Homework) Input: month number (0-12) Output: #day in that month Try to use both if and case !

More Related