1 / 46

Selection Statement

Selection Statement. Thanachat Thanomkulabut. Outline. Boolean expression if statement nested if statement switch case statement Flowchart. Boolean Expression. Boolean Expression. Operators Comparison Equal == Not equal != Less < Greater > Less than or equal to <=

sjeffery
Download Presentation

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. Selection Statement Thanachat Thanomkulabut

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

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

  4. Boolean Expression 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. Boolean Expression Example: Boolean Expressions double x = 4.0; Expression Value x < 5.0 ___________ x > 5.0 ___________ x <= 5.0 ___________ 5.0 == x ___________ x != 5.0 ___________ (3!=4)&&(7<5) ___________ (4>4)||(5<=10) ___________ true false true false true false true

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

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

  8. if statement condition False True Statement if statement if (condition) statement; if (condition){ statement; }

  9. if statement if statement if (condition){ statement1; statement2; } condition True False Statement1 Statement2

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

  11. if statement Weight in Kilograms BMI = (Height in Meters) X (Height in Meters) if statement example 11 double BMI = W /(H*H); if(BMI<18.5) Console.WriteLine(“Underweight”); if(BMI>=18.5 && BMI<=24.9) Console.WriteLine(“Normal”); if(BMI>=25.0 && BMI<=29.9) Console.WriteLine(“Overweight”); if(BMI>=30.0) Console.WriteLine(“Obese”);

  12. if statement Recieving Selfish Ratio = Giving Test I 12 • Selfish Ratio

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

  14. if statement condition False True Statement2 Statement1 if…else… statement if (condition) statement1; //true else statement2; //false

  15. if statement if…else… statement condition False True Statement2 if (condition) statement1; //true else{ statement2; //false statement3; //false } Statement1 Statement3

  16. if statement if…else… statement example • Write the program which check input number. • input : integer number • output : message to inform that number is odd or even.

  17. if statement if…else… statement example if(n%2 == 0) Console.WriteLine(“It’s even number”); else Console.WriteLine(“It’s odd number”);

  18. if statement Test II • Write the program which find the value of function • input : number • output :

  19. Outline • Boolean expression • if statement • nested if statement • switch case statement • FlowChart

  20. Nested if statement Nested if statement 20 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

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

  22. Nested if statement N -5 3 0 -5 3 0 N is negative number N is positive number N is zero number 22 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”); -5 >= 0 3 >= 0 0 >= 0  False True  False True

  23. Nested if statement Nested if statement N 3 -4 0 0 3 N is positive number N is zero number N is negative number -4 23 int N; N = int.Parse(Console.ReadLine()); if (N > 0) Console.WriteLine(“N is positive number”); else if (N==0) Console.WriteLine(“N is zero number”); else Console.WriteLine(“N is negative number”); -4 > 0 0 > 0 3 > 0 False True False True

  24. Exercise 1: SeparatedIF(simple) 24 1. Have you eaten lunch? 2. Do you like noodle? if (eaten==true) {Console.WriteLine(“I’ve already eaten”);} else {Console.WriteLine(“I’ve not eat yet”);} if (like_noodle==true) {Console.WriteLine(“I like noodle”);} else {Console.WriteLine(“I don’t like noodle”);}

  25. Exercise 2: Relatedtwo IF(full version) 25 1. Do you like noodle? 2. If you don’t like noodle, do you like fried rice? if (like_noodle==true) {Console.WriteLine(“I like noodle”);} else { Console.WriteLine(“I don’t like noodle”); if (like_friedrice==true) {Console.WriteLine(“I like friedrice”);} else {Console.WriteLine(“I don’t like friedrice”);} }

  26. Exercise 2: Relatedtwo IF(short version) 26 if (like_noodle==true) {Console.WriteLine(“I like noodle”);} elseif (like_friedrice==true) {Console.WriteLine(“I like friedrice”);} else {Console.WriteLine(“I don’t like friedrice”);} 1. Do you like noodle? 2. If you don’t like noodle, do you like fried rice?

  27. Exercise 3: Nestedtwo IF(short version) 27 1. Do you like noodle? 1.1 If you like noodle, do you love Sen-Yai? 1.2 If you don’t like noodle, do you like fried rice? if (like_noodle==true) {Console.WriteLine(“I like noodle”); if (love_SenYai==true) {Console.WriteLine(“I love Sen-Yai”);} else {Console.WriteLine(“I don’t love Sen-Yai”);} } elseif (like_friedrice==true) {Console.WriteLine(“I like fried rice”);} else {Console.WriteLine(“I don’t like fried rice”);}

  28. Nested if statement Nested if statement example • Write the program which show student’s grade • input : score of student • output :

  29. Nested if statement 2x+10, x ≤ 5 f(x) = x2+10, 5 < x ≤ 20 x3+10, x > 20 Test III • Write the program which calculation value of following function • input : value of x • output : function value of x according with ...

  30. Outline • Boolean expression • if statement • nested if statement • switch case statement • Flowchart

  31. เปรียบเทียบ “Read” แต่ละแบบ • Console.Read() • ผู้ใช้ใส่สตริงใดๆเข้ามา รอจนกว่าผู้ใช้จะเคาะ Return แล้วค่อยทำคำสั่งต่อไป • ให้ค่า int กลับมา เป็น int ที่แทนรหัส ASCII ของ character ตัวหน้าสุด • Console.Read() ถูกเรียกอีกครั้ง จะให้ค่า ASCII ของ character ถัดไป เมื่อถึงตัวสุดท้าย รอผู้ใช้เคาะ Return อีกครั้งจึงจะทำคำสั่งต่อไป • Console.ReadLine() • ให้ค่าสตริงตามที่ผู้ใช้พิมพ์เข้ามา รอจนกว่าเคาะ Return แล้วทำคำสั่งต่อไป • Console.ReadKey() • ให้ค่า character ที่ผู้ใช้พิมพ์ และทำคำสั่งต่อไปทันที

  32. using System;    class Program    {public static void Main(string[] args)        {            Console.Write("Input any string: ");char ch = (char)(Console.Read());            Console.WriteLine(ch);            ch = (char)(Console.Read());            Console.WriteLine(ch);            ch = (char)(Console.Read());            Console.WriteLine(ch);            ch = (char)(Console.Read());            Console.WriteLine(ch);            ch = (char)(Console.Read());            Console.WriteLine(ch);            Console.WriteLine("Hello World!");            Console.ReadKey(true);        }    }

  33. switch...case statement switch…case statement 33 • 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

  34. switch...case statement Example: switch-case (1) 34 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; } 1 2 30 <expression> <constant-expression>

  35. switch...case statement Test III • Write the program which show numbers of day in each months • input : Abbreviation of months • output : numbers of day in each months

  36. switch...case statement Example: switch-case (2) 36 string month; Console.Write("Input Month"); month = Console.ReadLine(); switch(month) {case“Jan”: case“Mar”: case“May”: Console.Write("This month has 31days"); break; case“Apr”: case“Jun”: Console.Write("This month has 30days"); break; default : Console.Write (“Wrong Input"); break; }

  37. switch...case statement Example: switch-case (3) <expression> must be int, char, string 37 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>

  38. switch...case statement Example: switch-case (4) <expression> must be int, char, string 38 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>

  39. switch...case statement 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 39 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; }

  40. Outline • Boolean expression • if statement • nested if statement • switch case statement • Flowchart

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

  42. Flowchart Program Flowchart Example 42 Start Statement1 Statement2 Statement3 Statement4 End

  43. Flowchart if statement flowchart Start 43 statement1; if (condition) statement2; //true else{ statement3; //false } statement4; Statement1 Condition true false Statement2 Statement3 Statement4 End

  44. if statement flowchart Start n= int.Parse(Console.ReadLine()); if (n>0) n= 2*n+5; else{ Console.Write(“Go”); n = n%4; } n=int.Parse(Console.ReadLine()); n>0 false true Console.Write(“Go”); n=2*n+5; n=n%4; End

  45. Test IV • Write flow chart which accord to following program n= int.Parse(Console.ReadLine()); n = n%5; if (n==2) Console.WriteLine(“Remainder is 2”); else{ n = n*10; } Console.WriteLine(“Program End”);

  46. Any question?

More Related