300 likes | 324 Views
Learn about boolean expressions, if statements, nested if statements, and switch-case statements in programming. Practice with examples to understand selection problems and solutions.
E N D
Selection Statement Computer and Programming (204111)
Review • Input Statement • Numeric Type Conversion • More Arithmetic Expressions • Modify-n-Assign; e.g. sum += x; • Increment & Decrement; x++ and x-- • Math Class string yourname; yourname = System.Console.ReadLine();
Lab Review • Statements • Input Statement • Fahrenheit to Celsius Conversion int value; string input; input = System.Console.ReadLine(); value = int.Parse(input);
Obtaining “F” Converse string to int Formula! Lab Review (Cont’) using System; namespace C2F_DegreeConverter { class C2F_DegreeConverterClass { static void Main() { string input; float Ctemp, Ftemp; Console.Write("Please input farenheit: "); input = Console.ReadLine(); Ftemp = int.Parse(input); Ctemp = (Ftemp-32)*5/9; Console.WriteLine("{0} degrees in farenheit is" + "equivalent to {1} in celcius", Ftemp, Ctemp); } } }
Outline • Boolean expression (นิพจน์ทางตรรกศาสตร์) • if statement (คำสั่งเงื่อนไข) • nested if statement (คำสั่งเงื่อนไขซ้อน) • switch case statement (คำสั่งทางเลือก)
Selection: Why do we need it? • Everything in life involves selection Steak Hungry MK Check Money Bar Mai
Boolean Expression • Operators • Comparison • Equal == • Not equal != • Less < • Greater > • Less than or equal to <= • Greater than or equal to >= • Boolean • And &&- Not ! • Or ||
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 the value Y an 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 • ifstatement • 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 is false 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.”);
Outline • Boolean expression • if statement • nested if statement • switch case statement
if#1 if#1 if#2 if#3 else#1 if#2 if#3 Nested IF Overview
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
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 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); Nested if statement
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
Exercise 1: switch-case How to write ”IF Statement” and ”Switch-case statement”?
Exercise 2 Input: month number (0-12) Output: #day in that month
Operator: LEMON Promotion Type Example 4Program Payment price Usage time Exercise 3 • Calculate payment for Air-time usage Input: Promotion Type & Usage time Output: Payment price
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 false CONDITION true statement2 statement3 END
Selection Problems Summary • Boolean Expression • Selection Statements • if...else... Statement • switch-case Statement if…else… switch
Quiz • จงเติมคำลงในช่องว่าง using System; namespace C2F_DegreeConverter { class C2F_DegreeConverterClass { static void Main() { string input; // declare a var as string for ReadLine() float Ctemp, Ftemp; // temp in Celsius & Fahrenheit Console.Write("Please input farenheit: "); // ……(1)…… input = Console.ReadLine(); // ……(2)…… Ftemp = int.Parse(input); // ……(3)…… Ctemp = (Ftemp-32)*5/9; // ……(4)…… Console.WriteLine("{0} degrees in farenheit is" + "equivalent to {1} in celcius", Ftemp, Ctemp); // ……(5)…… } } }
จงเขียนโปรแกรมเพื่อทำรับค่าอุณหภูมิองศาเซลเซียสแล้วทำการแปลงให้เป็นองศาฟาเรนไฮท์โดยแสดงค่าออกที่หน้าจอจงเขียนโปรแกรมเพื่อทำรับค่าอุณหภูมิองศาเซลเซียสแล้วทำการแปลงให้เป็นองศาฟาเรนไฮท์โดยแสดงค่าออกที่หน้าจอ โปรแกรมที่เขียนขึ้นควรให้ลักษณะการใช้งานออกหน้าจอดังนี้ C x 9= (F – 32) x 5 Please enter the temperature in Celsius: 32 32 degrees in Celsius is equal to 95 degrees in Fahrenheit