1 st semester 2012 Selection Statement 01204111 Selection




























- Slides: 28
1 st semester, 2012 Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide
Outline 2 Boolean expression Flowchart if statement if…else…statement nested ifstatement switch casestatement
Boolean Expression Boolean expression 3 Operators Comparison Boolean Equal == Not equal != Less < Greater > Less than or equal to <= Greater than or equal to >= 0 0 1 1 and and 0 1 = = 0 0 0 1 1 And && Or || Not ! or or 0 1 = = 0 1 1 1 not 0 = 1 not 1 = 0
Boolean Expression 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>3)&&(Y<5)
Boolean Expression Example 5 From the equation: X 2+9 X+10 = 0 How can we check that value of X is the answer for above equation? ((X*X +9*X +10) == 0) //true if X is the answer Condition: Is Y an even number? (Y%2 == 0) //true if Y is even OR (Y%2 != 1) //true if Y is even
Boolean Expression Example: Boolean Expressions 6 double x = 4. 0; Expression x < 5. 0 x > 5. 0 x <= 5. 0 == x x != 5. 0 Value (3!=4)&&(7<5) (4>4)||(5<=10) ___________ true ___________ false ______ true
Booleanvariable Boolean variable 7 Used for storing Boolean value true false Keyword: bool Usage example: int X = 0; bool My. Var = true; //Boolean variable declaration My. Var = (X>0); //Boolean expression
Outline 8 Boolean expression Flowchart if statement if…else…statement
Flowchart symbols overview 9 Graphical representation Terminator Process Input/output Condition Connector Flow line
Flowchart Program flowchart example 10 Start Statement 1 Statement 2 Statement 3 Statement 4 End
Outline 11 Boolean expression Flowchart if statement if…else…statement
if statement 12 Execute the specific statement when the ”condition” becomes true Syntax: true if (condition) statement; true if (condition) { statement 1; statement 2; if (condition) { statement; } }
if statement 13 if (condition) statement; if (condition) { statement; } condition True Statement False
if statement 14 if (condition) { } statement 1; condition statement 2; True Statement 1 Statement 2 False
if statement with Boolean variable 15 bool Y; int X = int. Parse(Console. Read. Line()); 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 example 16 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) 16
if statement example BMI 17 BMI = Weight in Kilograms (Height in Meters) X (Height in Meters) double BMI = W /(H*H); Weight Status Below 18. 5 Underweight 18. 5 – 24. 9 Normal 25. 0 – 29. 9 Overweight 30. 0 and Above Obese (Extremely Fat) if(BMI<18. 5) Console. Write. Line(“Underweight”); if(BMI>=18. 5 && BMI<=24. 9) Console. Write. Line(“Normal”); if(BMI>=25. 0 && BMI<=29. 9) Console. Write. Line(“Overweight”); if(BMI>=30. 0) Console. Write. Line(“Obese”);
Outline 19 Boolean expression Flowchart if statement if…else…statement
Flowchart if…else… statement flowchart Start 20 statement 1; if (condition) { Statement 1 statement 2; // true } else { statement 3; // false } true Condition false Statement 2 Statement 3 statement 4 ; Statement 4 End
if…else…statement if…else… statement 21 If conditionis true execute statement 1 If conditionis false execute statement 2 Syntax: if (condition) statement 1; //true else { statement 2; // false statement 3; // false }
if…else…statement if…else… statement 22 True condition Statement 1 if (condition) statement 1; //true else statement 2; // false False Statement 2
if…else…statement if…else… statement 23 True if (condition) statement 1; //true else { statement 2; // false statement 3; // false } condition False Statement 2 Statement 1 Statement 3
Flowchart if… else… statement flowchart Start 24 n= int. Parse(Console. Read. Line()); n=int. Parse(Console. Read. Line()); if (n>0) n= 2*n+5; else { Console. Write(“Go”); true n>0 false Console. Write(“Go”); n=2*n+5; n = n%4; n=n%4; } End
if…else…statement 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 N Even Number Odd Number Output It’s even number. It’s odd number.
if…else…statement if…else… statement example 26 Value of n Output Even Number It’s even number. Odd Number It’s odd number. if(n%2 == 0) Console. Write. Line(“It’s even number”); else Console. Write. Line(“It’s odd number”);
if…else…statement 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) x 2+1 x+2 x x<0 x≥ 0
Summary 28 Boolean expression Flowchart if statement if…else…statement nested ifstatement switch casestatement
Any question?




























