1 / 30

Computer and Programming (204111)

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.

sheehana
Download Presentation

Computer and Programming (204111)

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 Computer and Programming (204111)

  2. 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();

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

  4. 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); } } }

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

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

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

  8. 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

  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 • ifstatement • nested if statement • switch case statement

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

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

  13. 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

  14. 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.”);

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

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

  17. 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

  18. 2x+10, x ≤ 5 x2+10, 5 < x ≤ 20 f(x) = x3+10, x > 20 Nested if statement

  19. 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

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

  21. 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

  22. Exercise 1: switch-case How to write ”IF Statement” and ”Switch-case statement”?

  23. Exercise 2 Input: month number (0-12) Output: #day in that month

  24. 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

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

  26. START statement1 statement2 statement3 statement4 END Program Flowchart Example

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

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

  29. 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)…… } } }

  30. จงเขียนโปรแกรมเพื่อทำรับค่าอุณหภูมิองศาเซลเซียสแล้วทำการแปลงให้เป็นองศาฟาเรนไฮท์โดยแสดงค่าออกที่หน้าจอจงเขียนโปรแกรมเพื่อทำรับค่าอุณหภูมิองศาเซลเซียสแล้วทำการแปลงให้เป็นองศาฟาเรนไฮท์โดยแสดงค่าออกที่หน้าจอ โปรแกรมที่เขียนขึ้นควรให้ลักษณะการใช้งานออกหน้าจอดังนี้ 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

More Related