290 likes | 310 Views
Learn about Boolean expressions, if statements, nested if statements, and switch-case statements in programming, with examples and syntax explanation. Understand operators, comparisons, and how to implement conditional logic effectively.
E N D
Module3Selection 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 • Operators • Comparison • Equal == • Not equal != • Less < • Greater > • Less than or equal to <= • Greater than or equal to >= • Boolean • And && • Or || • Not !
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
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
Outline • Boolean expression • if statement • nested if statement • switch case statement
if statement • Execute the specific statement when the ”condition” becomes true • Syntax: if (condition) statement;//true if (condition){ statement1; //true statement2; //true }
Weight in Kilograms BMI = (Height in Meters) X (Height in Meters) if statement example • BMI (Body Mass Index)
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
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.”);
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 = ?
Outline • Boolean expression • if statement • nested if statement • switch case statement
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
if#1 if#1 if#2 if#3 else#1 = if#2 if#3 Nested IF Overview
2x+10, x ≤ 5 x2+10, 5 < x ≤ 20 f(x) = x3+10, x > 20 Nested if statement
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
Outline • Boolean expression • if statement • nested if statement • switch case statement
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
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>
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; }
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>
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>
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>
Flowchart Symbols Overview • Graphical representation Terminator Process Input/output Condition Connector Flow line
START statement1 statement2 statement3 statement4 END Program Flowchart Example
if statement flowchart START statement1 if (condition) statement2; //true else{ statement3; //false } statement4; statement1 CONDITION false true statement3 statement2 statement4
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);
Selection Problems Summary • Boolean Expression • Selection Statements • if...else... Statement • switch-case Statement if…else… switch
Exercise1 (Homework) Input: month number (0-12) Output: #day in that month Try to use both if and case !