Presentation is loading. Please wait.

# 2 nd Semester 2005 1 Selection Statement Computer and Programming (204111)

## Presentation on theme: "2 nd Semester 2005 1 Selection Statement Computer and Programming (204111)"— Presentation transcript:

2 nd Semester 2005 1 Selection Statement Computer and Programming (204111)

2 Review Input Statement Input Statement Numeric Type Conversion Numeric Type Conversion More Arithmetic Expressions More Arithmetic Expressions Modify-n-Assign; e.g. sum += x; Modify-n-Assign; e.g. sum += x; Increment & Decrement; x++ and x-- Increment & Decrement; x++ and x-- Math Class Math Class string yourname; yourname = System.Console.ReadLine(); string yourname; yourname = System.Console.ReadLine();

3 Lab Review Statements Statements Input Statement Input Statement Fahrenheit to Celsius Conversion Fahrenheit to Celsius Conversion int value; string input; input = System.Console.ReadLine(); value = int.Parse(input); int value; string input; input = System.Console.ReadLine(); value = int.Parse(input);

4 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); } 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); } Obtaining “F” Converse string to int Formula!

5 Outline Boolean expression ( นิพจน์ทาง ตรรกศาสตร์ ) Boolean expression ( นิพจน์ทาง ตรรกศาสตร์ ) if statement ( คำสั่งเงื่อนไข ) if statement ( คำสั่งเงื่อนไข ) nested if statement ( คำสั่งเงื่อนไขซ้อน ) nested if statement ( คำสั่งเงื่อนไขซ้อน ) switch case statement ( คำสั่งทางเลือก ) switch case statement ( คำสั่งทางเลือก )

6 Selection: Why do we need it? Everything in life involves selection Everything in life involves selection Hungry Check Money MK Steak Bar Mai

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

8 Boolean Expression Example From the equation: X 2 +9X+10 = 0 From the equation: X 2 +9X+10 = 0 How can we check that value of X is the answer for above equation? How can we check that value of X is the answer for above equation? Condition: Is the value Y an even number? Condition: Is the value Y an 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

9 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

10 Outline Boolean expression Boolean expression if statement if statement nested if statement nested if statement switch case statement switch case statement

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

12 if statement example BMI (Body Mass Index) BMI (Body Mass Index) BMI Weight Status Below 18.5 Underweight 18.5 – 24.9 Normal 25.0 – 29.9 Overweight 30.0 and Above Obese (Extremely Fat) BMI = Weight in Kilograms (Height in Meters) X (Height in Meters)

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

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

15 Outline Boolean expression Boolean expression if statement if statement nested if statement nested if statement switch case statement switch case statement

16 Nested IF Overview if#1if#2 if#3 if#1if#2 if#3 else#1

17 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 if#2

18 Nested if statement f(x) = 2x+10, x ≤ 5 x 2 +10, 5 < x ≤ 20 x 3 +10, x > 20 Range Boolean Expression x ≤ 5 (x <= 5) (x <= 5) 5 < x ≤ 20 ((5 < x) && (x <= 20)) x > 20 (x > 20)

19 Nested if statement f(x) = 2x+10, x ≤ 5 x 2 +10, 5 < x ≤ 20 x 3 +10, x > 20 double fx = 0; double x = double.Parse(Console.ReadLine()); if ( ) fx = 2*x + 10; fx = 2*x + 10; else if ( ) fx = x*x + 10; fx = x*x + 10; else else fx = x*x*x + 10; fx = x*x*x + 10; Console.WriteLine(“f(x) = {0}”, fx);

20 Outline Boolean expression Boolean expression if statement if statement nested if statement nested if statement switch case statement switch case statement

21 switch…case statement For selecting a statement where its label corresponds to the value of the switch expression. 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

22 Exercise 1: switch-case int day_num; string day_name; 1Sun 2Monday 3Tuesday 4Wednesday 5Thursday 6Friday 7Saturday How to write ”IF Statement” and ”Switch-case statement”?

23 Exercise 2 Ex1 Please input month: 5 Your month has 31 days. Input: month number (0-12) Output: #day in that month Ex2 Please input month: 2 Your month has 28 days.

24 Exercise 3 Calculate payment for Air-time usage Calculate payment for Air-time usage Operator: LEMON Input: Promotion Type & Usage time Output: Payment price Example 4 Program Promotion Type Usage time Payment price

25 Flowchart Symbols Overview Graphical representation Graphical representation Terminator Process Input/output Condition Connector Flow line

26 Program Flowchart Example START statement1 statement2 statement3 statement4 END

27 if statement flowchart START statement1 statement2 statement3 END CONDITION true false

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

29 Quiz 1. จงเติมคำลงในช่องว่าง 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)…… } 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)…… }

30 2. จงเขียนโปรแกรมเพื่อทำรับค่าอุณหภูมิองศา เซลเซียสแล้วทำการแปลงให้เป็นองศาฟาเรน ไฮท์โดยแสดงค่าออกที่หน้าจอ โปรแกรมที่เขียนขึ้นควรให้ลักษณะการใช้งาน ออกหน้าจอดังนี้ 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 Please enter the temperature in Celsius: 32 32 degrees in Celsius is equal to 95 degrees in Fahrenheit

Download ppt "2 nd Semester 2005 1 Selection Statement Computer and Programming (204111)"

Similar presentations

Ads by Google