290 likes | 295 Views
1 st semester, 2012. Selection Statement. 01204111 – Selection Statement Modified from Aj . Thanachat Thanomkulabut’s slide. Outline. Boolean expression Flowchart if statement if…else… statement nested if statement switch case statement. Boolean Expression. Boolean expression.
E N D
1st semester, 2012 Selection Statement 01204111 – Selection Statement Modified from Aj. ThanachatThanomkulabut’s slide
Outline • Boolean expression • Flowchart • if statement • if…else… statement • nested if statement • switch case statement
Boolean Expression Boolean expression • Operators • Comparison • Equal == • Not equal != • Less < • Greater > • Less than or equal to <= • Greater than or equal to >= • Boolean • And && • Or || • Not ! 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
Boolean Expression Boolean expression intY; Y Is Y greater than 3? Y>3 Is Y less than 5? Y<5 (Y>3) && (Y<5) Is Y between 3 and 5?
Boolean Expression 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
Boolean Expression 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 false true
Boolean variable Boolean variable • Used for storing Boolean value • true • False • Keyword: bool • Usage example: boolMyVar = true; //Boolean variable declaration MyVare = (X>0); //Boolean expression
Outline • Boolean expression • Flowchart • if statement • if…else… statement
Flowchart Flowchart symbols overview 9 • Graphical representation Terminator Process Input/output Condition Connector Flow line
Flowchart Program flowchart example 10 Start Statement1 Statement2 Statement3 Statement4 End
Flowchart if statement flowchart Start 11 statement1; if (condition) { statement2; //true } else{ statement3; //false } statement4; Statement1 Condition true false Statement2 Statement3 Statement4 End
Flowchart 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
Outline • Boolean expression • Flowchart • if statement • if…else… statement
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; }
if statement condition False True Statement if statement if (condition) statement; if (condition){ statement; }
if statement if statement if (condition){ statement1; statement2; } condition True False Statement1 Statement2
if statement if statement with Boolean variable boolY; 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 Weight in Kilograms BMI = (Height in Meters) X (Height in Meters) if statement example 18 • BMI (Body Mass Index)
if statement Weight in Kilograms BMI = (Height in Meters) X (Height in Meters) if statement example 19 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”);
if statement Recieving Selfish Ratio = Giving Test I 20 • Selfish Ratio
Outline • Boolean expression • Flowchart • if statement • if…else… statement
if…else… statement if…else… statement 22 • If condition is true execute statement1 • If condition isfalse execute statement2 • Syntax: if (condition) statement1; //true else statement2; //false if (condition) statement1; //true else{ statement2; //false statement3; //false }
if…else… statement condition False True Statement2 Statement1 if…else… statement if (condition) statement1; //true else statement2; //false
if…else… statement if…else… statement condition False True Statement2 if (condition) statement1; //true else{ statement2; //false statement3; //false } Statement1 Statement3
if…else… 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.
if…else… statement if…else… statement example if(n%2 == 0) Console.WriteLine(“It’s even number”); else Console.WriteLine(“It’s odd number”);
if…else… statement Test II • Write the program which find the value of function • input : number x • output : f(x)
Summary • Boolean expression • FlowChart • if statement • if…else… statement • nested if statement • switch case statement