1 / 29

Selection Statement

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.

Download Presentation

Selection Statement

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. 1st semester, 2012 Selection Statement 01204111 – Selection Statement Modified from Aj. ThanachatThanomkulabut’s slide

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

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

  4. 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?

  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

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

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

  8. Outline • Boolean expression • Flowchart • if statement • if…else… statement

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

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

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

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

  13. Outline • Boolean expression • Flowchart • if statement • if…else… statement

  14. 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; }

  15. if statement condition False True Statement if statement if (condition) statement; if (condition){ statement; }

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

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

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

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

  20. if statement Recieving Selfish Ratio = Giving Test I 20 • Selfish Ratio

  21. Outline • Boolean expression • Flowchart • if statement • if…else… statement

  22. 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 }

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

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

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

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

  27. if…else… statement Test II • Write the program which find the value of function • input : number x • output : f(x)

  28. Summary • Boolean expression • FlowChart • if statement • if…else… statement • nested if statement • switch case statement

  29. Any question?

More Related