Selection Statement Thanachat Thanomkulabut
Outline 2 Boolean expression if if statement nested if nested if statement switch case switch case statement Flowchart
Boolean Expression Operators Comparison == Equal == != Not equal != < Less < > Greater > <= Less than or equal to <= >= Greater than or equal to >= Boolean && And && || Or || ! Not ! 3 Boolean Expression 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
Boolean Expression Example From the equation: X 2 +9X+10 = 0 How can we check that value of X is the answer for above equation? Condition: Is value Y even number? //true if X is the answer ((X*X +9*X +10) == 0) //true if X is the answer //true if Y is even (Y%2 == 0) //true if Y is evenOR //true if Y is even (Y%2 != 1) //true if Y is even 4 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 5 Boolean Expression false true
Outline 6 Boolean expression if if statement nested if nested if statement switch case switch case statement Flowchart
if statement ”condition”true Execute the specific statement when the ”condition” becomes true Syntax: if (condition) statement; statement; if (condition) { statement1; statement1; statement2; statement2;} if (condition) { statement; statement;} 7 if statementtrue true
8 condition False True Statement if (condition) statement; statement; if (condition) { statement; statement;} if statement
9 condition False True Statement1 Statement2 if (condition) { statement1; statement1; statement2; statement2;} if statement
10 if statement example BMI (Body Mass Index) BMIWeight Status Below 18.5Underweight 18.5 – 24.9Normal 25.0 – 29.9Overweight 30.0 and AboveObese (Extremely Fat) BMI = Weight in Kilograms (Height in Meters) X (Height in Meters) 10
if statement BMIWeight Status Below 18.5Underweight 18.5 – 24.9Normal 25.0 – 29.9Overweight 30.0 and AboveObese (Extremely Fat) BMI = Weight in Kilograms (Height in Meters) X (Height in Meters) 11 double BMI = W /(H*H); if(BMI<18.5) Console.WriteLine(“Underweight”); Console.WriteLine(“Underweight”); if(BMI>=18.5 && BMI =18.5 && BMI<=24.9) Console.WriteLine(“Normal”); Console.WriteLine(“Normal”); if(BMI>=25.0 && BMI =25.0 && BMI<=29.9) Console.WriteLine(“Overweight”); Console.WriteLine(“Overweight”); if statement example if(BMI>=30.0) Console.WriteLine(“Obese”); Console.WriteLine(“Obese”);
if statement Test I Selfish Ratio RatioOutput More than 1You are selfish 1You break even Less than 1You are giver 12 Selfish Ratio =RecievingGiving
if…else… statement conditiontruestatement1 If condition is true execute statement1 conditionfalsestatement2 If condition is false execute statement2 Syntax: if (condition) statement1; //true statement1; //trueelse statement2; //false if (condition) statement1; //true statement1; //true else { statement2; //false statement2; //false statement3; //false } 13 if statement
if…else… statement 14 condition False True Statement2 Statement1 if (condition) statement1; //true statement1; //trueelse statement2; //false if statement
if…else… statement 15 if (condition) statement1; //true statement1; //true else { statement2; //false statement2; //false statement3; //false } condition False True Statement2 Statement1 Statement3 if statement
if…else… statement example 16 Write the program which check input number. input : integer number output : message to inform that number is odd or even. Value in NOutput Even NumberIt’s even number. Odd NumberIt’s odd number. if statement
if…else… statement example 17 Value in NOutput Even NumberIt’s even number. Odd NumberIt’s odd number. if statement if(n%2 == 0) Console.WriteLine(“It’s even number”); Console.WriteLine(“It’s even number”); else Console.WriteLine(“It’s odd number”); Console.WriteLine(“It’s odd number”);
Test II 18 Write the program which find the value of function input : number output : f(x)x x 2 +1x<0 x+2x≥0 if statement
Outline 19 Boolean expression if if statement nested if nested if statement switch case switch case statement FlowChart
Nested if statement int N; N = int.Parse(Console.ReadLine()); if (N >= 0) { if (N==0) if (N==0) Console.WriteLine(“N is zero number”); Console.WriteLine(“N is zero number”); else else Console.WriteLine(“N is positive number”); Console.WriteLine(“N is positive number”);}else Console.WriteLine(“N is negative number”); Console.WriteLine(“N is negative number”); if#1 20 Nested if statementif#2
int N; N = int.Parse(Console.ReadLine()); if (N > 0) Console.WriteLine(“N is positive number”); Console.WriteLine(“N is positive number”);else if (N==0) if (N==0) Console.WriteLine(“N is zero number”); Console.WriteLine(“N is zero number”); else else Console.WriteLine(“N is negative number”); Console.WriteLine(“N is negative number”); if#1 if#2 21 Nested if statement
int N; N = int.Parse(Console.ReadLine()); if (N >= 0) { if (N==0) if (N==0) Console.WriteLine(“N is zero number”); Console.WriteLine(“N is zero number”); else else Console.WriteLine(“N is positive number”); Console.WriteLine(“N is positive number”);}else Console.WriteLine(“N is negative number”); Console.WriteLine(“N is negative number”); >= 0 False N is negative number N >= 0 True True N is zero number >= 0 False N is positive number 22
Nested if statement int N; N = int.Parse(Console.ReadLine()); if (N > 0) Console.WriteLine(“N is positive number”); Console.WriteLine(“N is positive number”);else if (N==0) if (N==0) Console.WriteLine(“N is zero number”); Console.WriteLine(“N is zero number”); else else Console.WriteLine(“N is negative number”); Console.WriteLine(“N is negative number”); N > 0 False False N is negative number > 0 True N is zero number > 0 True N is positive number 23
Exercise 1: Separated IF (simple) 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”);} 1. Have you eaten lunch? 2. Do you like noodle? 24
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”);} } 1. Do you like noodle? 2. If you don’t like noodle, do you like fried rice? Exercise 2: Related two IF (full version) 25
if (like_noodle==true) {Console.WriteLine(“I like noodle”);} else if (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? Exercise 2: Related two IF (short version) 26
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”);} } else if (like_friedrice==true) {Console.WriteLine(“I like fried rice”);} else {Console.WriteLine(“I don’t like fried rice”);} 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? Exercise 3: Nested two IF (short version) 27
Nested if statement example 28 Write the program which show student’s grade input : score of student output : ScoreGrade 80 – 100A 70 – 79B 60 – 69C 50 – 59D 0 – 49F Nested if statement
Test III 29 Write the program which calculation value of following function input : value of x output : function value of x according with... Nested if statement f(x) = 2x+10, x ≤ 5 x 2 +10, 5 < x ≤ 20 x 3 +10, x > 20
Outline 30 Boolean expression if if statement nested if nested if statement switch case switch case statement Flowchart
switch…case statement For selecting a statement where its label corresponds to the value of the switch expression. switch ( ) { case : ; ; break; [default: ; ; break; break;] } must be int, char, string must be int, char, string 31 switch...case statement
32 Example: switch-case (1) int day_num; day name 1Sunday 2Monday 3Tuesday 4Wednesday 5Thursday 6Friday 7Saturday int day_num; Console.Write("Input the day"); day_num = int.Parse(Console.ReadLine()); day_num switch(day_num) 1: {case 1: Console.Write ("Today is Sunday"); break; 2: case 2: Console.Write("Today is Monday"); break; : : default : Console.Write ("I don’t know"); break; } <expression> <constant-expression> 32 switch...case statement 1230
Test III 33 Write the program which show numbers of day in each months input : Abbreviation of months output : numbers of day in each months InputOutput Jan31 Feb28 or 29 Mar31 Api30 May31... switch...case statement
34 Example: switch-case (2) string month; Console.Write("Input Month"); month = Console.ReadLine(); month switch(month) “Jan”: {case “Jan”: “Mar”: case “Mar”: “May”: case “May”: Console.Write("This month has 31days"); break; “Apr”: case “Apr”: “Jun”: case “Jun”: Console.Write("This month has 30days"); break; : default : Console.Write (“Wrong Input"); break; } 34 switch...case statement InputOutput Jan31 Feb28 or 29 Mar31 Api30 May31...
35 Example: switch-case (3) 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; } must be int, char, string must be int, char, string 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> int version char version <constant-expression> 35 switch...case statement
36 Example: switch-case (4) 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; } must be int, char, string must be int, char, string string version <expression> <constant-expression> 36 switch...case statement
Convert switch-case to if else 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; } switch version with default if (a == 1 || a == 2) Console.WriteLine("Hi"); else if (a == 3 || a == 4) Console.WriteLine("Hello"); else Console.WriteLine("Bye"); if else version switch (a) { case 1 : case 2 : Console.WriteLine("Hi"); break; } switch version without default 37 switch...case statement
Outline 38 Boolean expression if if statement nested if nested if statement switch case switch case statement Flowchart
Flowchart Symbols Overview Graphical representation Terminator Process Input/output Condition Connector Flow line 39 Flowchart
Program Flowchart Example Start Statement1 Statement2 Statement3 Statement4 End 40 Flowchart
if statement flowchart statement1; if (condition) statement2; //true statement2; //true else { statement3; //false statement3; //false} statement4; Start Statement1 Condition Statement2 Statement3 true false Statement4 End 41 Flowchart
if statement flowchart 42 n= int.Parse(Console.ReadLine()); if (n>0) n= 2*n+5; n= 2*n+5; else { Console.Write(“Go”); Console.Write(“Go”); n = n%4; n = n%4;} Start n>0 n=2*n+5; true false n=n%4; End n=int.Parse(Console.ReadLine()); Console. Write(“Go”);
Test IV 43 Write flow chart which accord to following program n= int.Parse(Console.ReadLine()); n = n%5; if (n==2) Console.WriteLine(“Remainder is 2”); Console.WriteLine(“Remainder is 2”); else { n = n*10; n = n*10;} Console.WriteLine(“Program End”);
Any question?