Presentation is loading. Please wait.

Presentation is loading. Please wait.

Thanachat Thanomkulabut

Similar presentations


Presentation on theme: "Thanachat Thanomkulabut"— Presentation transcript:

1 Thanachat Thanomkulabut
Selection Statement Thanachat Thanomkulabut

2 Outline Boolean expression if statement nested if statement
switch case statement Flowchart

3 Boolean Expression Operators Comparison Boolean Equal == Not equal !=
Less < Greater > Less than or equal to <= Greater than or equal to >= Boolean And && Or || Not ! ! Exclamation mark 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

4 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

5 Example: Boolean Expressions
double x = 4.0; Expression Value x < ___________ x > ___________ x <= ___________ 5.0 == x ___________ x != ___________ (3!=4)&&(7<5) ___________ (4>4)||(5<=10) ___________ true false true false true false true

6 Outline Boolean expression if statement nested if statement
switch case statement Flowchart

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

8 if statement if (condition) statement; condition if (condition) {
False True Statement if (condition) { statement; }

9 if statement if (condition) { statement1; statement2; condition } True
False True Statement1 Statement2

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

11 if statement example double BMI = W /(H*H); if(BMI<18.5)
Weight Status Below 18.5 Underweight 18.5 – 24.9 Normal 25.0 – 29.9 Overweight 30.0 and Above Obese (Extremely Fat) 11 BMI = Weight in Kilograms (Height in Meters) X (Height in Meters) double BMI = W /(H*H); if(BMI<18.5) Console.WriteLine(“Underweight”); if(BMI>=18.5 && BMI<=24.9) Console.WriteLine(“Normal”); if(BMI>=25.0 && BMI<=29.9) Console.WriteLine(“Overweight”); if(BMI>=30.0) Console.WriteLine(“Obese”);

12 Test I Selfish Ratio Recieving Selfish Ratio = Giving Ratio Output
if statement Test I 12 Selfish Ratio Selfish Ratio = Recieving Giving Ratio Output More than 1 You are selfish 1 You break even Less than 1 You are giver

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

14 if…else… statement if (condition) statement1; //true else
if statement if…else… statement condition False True Statement2 Statement1 if (condition) statement1; //true else statement2; //false

15 if…else… statement if (condition) statement1; //true else {
if statement if…else… statement condition False True Statement2 Statement1 Statement3 if (condition) statement1; //true else { statement2; //false statement3; //false }

16 if…else… statement example
if statement if…else… statement example Write the program which check input number. input : integer number output : message to inform that number is odd or even. Value in N Output Even Number It’s even number. Odd Number It’s odd number.

17 if…else… statement example
if statement if…else… statement example Value in N Output Even Number It’s even number. Odd Number It’s odd number. if(n%2 == 0) Console.WriteLine(“It’s even number”); else Console.WriteLine(“It’s odd number”);

18 Test II Write the program which find the value of function
if statement Test II Write the program which find the value of function input : number output : f(x) x x2+1 x<0 x+2 x≥0

19 Outline Boolean expression if statement nested if statement
switch case statement FlowChart

20 Nested if statement int N; N = int.Parse(Console.ReadLine());
20 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”); } Console.WriteLine(“N is negative number”); if#1 if#2

21 Nested if statement int N; N = int.Parse(Console.ReadLine());
21 int N; N = int.Parse(Console.ReadLine()); if (N > 0) Console.WriteLine(“N is positive number”); else if (N==0) Console.WriteLine(“N is zero number”); Console.WriteLine(“N is negative number”); if#1 if#2

22 Nested if statement   int N; N = int.Parse(Console.ReadLine());
3 -5 -5 3 N is positive number N is zero number N is negative number 22 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”); } Console.WriteLine(“N is negative number”); 3 >= 0 0 >= 0 -5 >= 0 False True False True

23 Nested if statement int N; N = int.Parse(Console.ReadLine());
3 -4 3 -4 N is negative number N is zero number N is positive number 23 int N; N = int.Parse(Console.ReadLine()); if (N > 0) Console.WriteLine(“N is positive number”); else if (N==0) Console.WriteLine(“N is zero number”); Console.WriteLine(“N is negative number”); 3 > 0 0 > 0 -4 > 0 True False True False

24 Exercise 1: Separated IF (simple)
24 1. Have you eaten lunch? 2. Do you like noodle? 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”);} {Console.WriteLine(“I don’t like noodle”);}

25 Exercise 2: Related two IF (full version)
25 1. Do you like noodle? 2. If you don’t like noodle, do you like fried rice? 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”);} {Console.WriteLine(“I don’t like friedrice”);} }

26 Exercise 2: Related two IF (short version)
26 1. Do you like noodle? 2. If you don’t like noodle, do you like fried rice? 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”);}

27 Exercise 3: Nested two IF (short version)
27 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? 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”);} {Console.WriteLine(“I don’t like fried rice”);}

28 Nested if statement example
Write the program which show student’s grade input : score of student output : Score Grade 80 – 100 A 70 – 79 B 60 – 69 C 50 – 59 D 0 – 49 F

29 Nested if statement Test III Write the program which calculation value of following function input : value of x output : function value of x according with ... f(x) = 2x+10, x ≤ 5 x2+10, < x ≤ 20 x3+10, x > 20

30 Outline Boolean expression if statement nested if statement
switch case statement Flowchart

31 เปรียบเทียบ “Read” แต่ละแบบ
Console.Read() ผู้ใช้ใส่สตริงใดๆเข้ามา รอจนกว่าผู้ใช้จะเคาะ Return แล้วค่อยทำคำสั่งต่อไป ให้ค่า int กลับมา เป็น int ที่แทนรหัส ASCII ของ character ตัวหน้าสุด Console.Read() ถูกเรียกอีกครั้ง จะให้ค่า ASCII ของ character ถัดไป เมื่อถึงตัวสุดท้าย รอผู้ใช้เคาะ Return อีกครั้งจึงจะทำคำสั่งต่อไป Console.ReadLine() ให้ค่าสตริงตามที่ผู้ใช้พิมพ์เข้ามา รอจนกว่าเคาะ Return แล้วทำคำสั่งต่อไป Console.ReadKey() ให้ค่า character ที่ผู้ใช้พิมพ์ และทำคำสั่งต่อไปทันที

32 using System;     class Program     {         public static void Main(string[] args)         {             Console.Write("Input any string: ");             char ch = (char)(Console.Read());             Console.WriteLine(ch);             ch = (char)(Console.Read());             Console.WriteLine(ch);             ch = (char)(Console.Read());             Console.WriteLine(ch);             ch = (char)(Console.Read());             Console.WriteLine(ch);             ch = (char)(Console.Read());             Console.WriteLine(ch);             Console.WriteLine("Hello World!");             Console.ReadKey(true);         }     }

33 switch…case statement
33 For selecting a statement where its label corresponds to the value of the switch expression. switch (<expression>) { case <constant-expression> : <statements>; break; [default: break;] } <expression> must be int, char, string

34 Example: switch-case (1)
switch...case statement Example: switch-case (1) 34 int day_num; Console.Write("Input the day"); day_num = int.Parse(Console.ReadLine()); switch(day_num) {case 1: Console.Write ("Today is Sunday"); break; case 2: Console.Write("Today is Monday"); : default : Console.Write ("I don’t know"); } int day_num; day name 1 Sunday 2 Monday 3 Tuesday 4 Wednesday 5 Thursday 6 Friday 7 Saturday 2 1 30 <expression> <constant-expression>

35 Test III Write the program which show numbers of day in each months
switch...case statement Test III Write the program which show numbers of day in each months input : Abbreviation of months output : numbers of day in each months Input Output Jan 31 Feb 28 or 29 Mar Api 30 May ...

36 Example: switch-case (2)
switch...case statement Example: switch-case (2) 36 string month; Console.Write("Input Month"); month = Console.ReadLine(); switch(month) {case “Jan”: case “Mar”: case “May”: Console.Write("This month has 31days"); break; case “Apr”: case “Jun”: Console.Write("This month has 30days"); default : Console.Write (“Wrong Input"); } Input Output Jan 31 Feb 28 or 29 Mar Api 30 May ...

37 Example: switch-case (3)
switch...case statement Example: switch-case (3) 37 <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); : default: Console.Write("Try again"); } int day_num; day_num= int.Parse(Console.ReadLine()); switch(day_num ) { case 1: Console.Write ("Sunday"); break; case 2: console.Write("Monday"); : default : Console.Write(“Try again"); } <expression> <constant-expression>

38 Example: switch-case (4)
switch...case statement Example: switch-case (4) 38 <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); : default: Console.Write("Try again"); } <expression> <constant-expression>

39 Convert switch-case to if else
switch...case statement Convert switch-case to if else 39 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 int a; a= int.Parse(Console.ReadLine()); switch (a) { case 1 : case 2 : Console.WriteLine("Hi"); break; case 3 : case 4 : Console.WriteLine("Hello"); default : Console.WriteLine("Bye"); } switch (a) { case 1 : case 2 : Console.WriteLine("Hi"); break; } switch version without default

40 Outline Boolean expression if statement nested if statement
switch case statement Flowchart

41 Flowchart Symbols Overview
41 Graphical representation Terminator Process Input/output Condition ข้าวหลามตัด = diamond Connector Flow line

42 Program Flowchart Example
42 Start Statement1 Statement2 Statement3 Statement4 End

43 if statement flowchart
Start 43 statement1; if (condition) statement2; //true else { statement3; //false } statement4; Statement1 Condition true false Statement2 Statement3 Statement4 End

44 if statement flowchart
Start n= int.Parse(Console.ReadLine()); if (n>0) n= 2*n+5; else { Console.Write(“Go”); n = n%4; } n=int.Parse(Console.ReadLine()); n>0 false true Console. Write(“Go”); n=2*n+5; n=n%4; End

45 Test IV Write flow chart which accord to following program
n= int.Parse(Console.ReadLine()); n = n%5; if (n==2) Console.WriteLine(“Remainder is 2”); else { n = n*10; } Console.WriteLine(“Program End”);

46 Any question?


Download ppt "Thanachat Thanomkulabut"

Similar presentations


Ads by Google