Presentation is loading. Please wait.

Presentation is loading. Please wait.

Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012.

Similar presentations


Presentation on theme: "Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012."— Presentation transcript:

1 Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012

2 Outline 2  Boolean expression  Flowchart  if  if statement  if…else…  if…else… statement  nested if  nested if statement  switch case  switch case statement

3 Boolean expression  Operators  Comparison == Equal == != Not equal != < Less < > Greater > <= Less than or equal to <= >= Greater than or equal to >=  Boolean && And && || Or || ! Not ! 3 Boolean Expression 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 4 int Y; Y Is Y greater than 3? Y>3 Is Y less than 5? Y<5 Is Y between 3 and 5? (Y<5)(Y>3)&& Boolean Expression

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

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

7 Boolean variable  Used for storing Boolean value  true  false  Keyword: bool  Usage example: int X = 0; bool MyVar = true; //Boolean variable declaration MyVar = (X>0); //Boolean expression 7 Boolean variable

8 Outline 8  Boolean expression  Flowchart  if  if statement  if…else…  if…else… statement

9 Flowchart symbols overview  Graphical representation Terminator Process Input/output Condition Connector Flow line 9 Flowchart

10 Program flowchart example Start Statement1 Statement2 Statement3 Statement4 End 10 Flowchart

11 Outline 11  Boolean expression  Flowchart  if  if statement  if…else…  if…else… statement

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

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

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

15 if statement with Boolean variable 15 bool Y; int X = int.Parse(Console.ReadLine()); Y = (X > 10); if (Y) console.Write(“X is greater than 10”); else console.Write(“X is less than or equal to 10”); if statement

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

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

18 if statement Test I  Selfish Ratio RatioOutput More than 1You are selfish 1You break even Less than 1You are giver 18 Selfish Ratio =RecievingGiving

19 Outline 19  Boolean expression  Flowchart  if  if statement  if…else…  if…else… statement

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

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

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

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

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

25 if…else… statement example 25  Write a program that checks if the input number is odd or even.  input : integer number  output : message to inform that number is odd or even. Value in NOutput Even NumberIt’s even number. Odd NumberIt’s odd number. if…else… statement

26 if…else… statement example 26 Value of nOutput Even NumberIt’s even number. Odd NumberIt’s odd number. if(n%2 == 0) Console.WriteLine(“It’s even number”); Console.WriteLine(“It’s even number”); else Console.WriteLine(“It’s odd number”); Console.WriteLine(“It’s odd number”); if…else… statement

27 Test I 27  Write a program that computes the value of a f(x) based on the value of x  input : number x  output : f(x) f(x)x x 2 +1x<0 x+2x≥0 if…else… statement

28 Summary 28  Boolean expression  Flowchart  if  if statement  if…else…  if…else… statement  nested if  nested if statement  switch case  switch case statement

29 Any question?


Download ppt "Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012."

Similar presentations


Ads by Google