Module 3 Selection Statement Thanawin Rakthanmanon Email fengtwrku

  • Slides: 29
Download presentation
Module 3 Selection Statement Thanawin Rakthanmanon Email: fengtwr@ku. ac. th Create by: Aphirak Jansang

Module 3 Selection Statement Thanawin Rakthanmanon Email: fengtwr@ku. ac. th Create by: Aphirak Jansang Computer Engineering Department Kasetsart University, Bangkok THAILAND 1 nd Semester 200 7 1

Outline Boolean expression ¡ if statement ¡ nested ifstatement ¡ switch casestatement ¡ 1

Outline Boolean expression ¡ if statement ¡ nested ifstatement ¡ switch casestatement ¡ 1 nd Semester 200 7 2

Boolean Expression ¡ Operators l Comparison Equal == ¡ Not equal != ¡ Less

Boolean Expression ¡ Operators l Comparison Equal == ¡ Not equal != ¡ Less < ¡ Greater > ¡ Less than or equal to <= ¡ Greater than or equal to >= ¡ l Boolean And && ¡ Or || ¡ Not ! ¡ 1 nd Semester 200 7 3

Boolean Expression Example ¡ From the equation: X 2+9 X+10 = 0 l How

Boolean Expression Example ¡ From the equation: X 2+9 X+10 = 0 l 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 value Y even number? (Y%2 == 0) //true if Y is even OR (Y%2 != 1) //true if Y is even 1 nd Semester 200 7 4

Example: Boolean Expressions double x = 4. 0; Expression x < 5. 0 x

Example: Boolean Expressions double x = 4. 0; Expression x < 5. 0 x > 5. 0 x <= 5. 0 == x x != 5. 0 1 nd Semester 200 7 Value true ___________ false ___________ true ______ 5

Outline Boolean expression ¡ if statement ¡ nested ifstatement ¡ switch casestatement ¡ 1

Outline Boolean expression ¡ if statement ¡ nested ifstatement ¡ switch casestatement ¡ 1 nd Semester 200 7 6

if statement Execute the specific statement when the ”condition” becomes true ¡ Syntax: ¡

if statement Execute the specific statement when the ”condition” becomes true ¡ Syntax: ¡ if (condition) { statement; //true statement 1; //true statement 2; //true } 1 nd Semester 200 7 7

if statement example ¡ BMI (Body Mass Index) Weight in Kilograms BMI = (Height

if statement example ¡ BMI (Body Mass Index) Weight in Kilograms BMI = (Height in Meters) X (Height in Meters) BMI Below 18. 5 Weight Status Underweight 18. 5 – 24. 9 Normal 25. 0 – 29. 9 Overweight 30. 0 and Above 1 nd Semester 200 7 Obese (Extremely Fat) 8

if…else… statement If conditionis true execute statement 1 ¡ If conditionis false execute statement

if…else… statement If conditionis true execute statement 1 ¡ If conditionis false execute statement 2 if (condition) ¡ Syntax: if (condition) statement 1; //true else { else statement 2; // false statement 3; // false } ¡ 1 nd Semester 200 7 9

if…else… statement example ¡ Question l Value in variable N is Odd or Even

if…else… statement example ¡ Question l Value in variable N is Odd or Even Number? Value in N Output Even Number It’s even number. Odd Number It’s odd number. if (__________) Console. Write. Line(“It’s even number. ”); else Console. Write. Line(“It’s odd number. ”); 1 nd Semester 200 7 10

Quiz ¡ Fill the following blank if (__________) X%2 == 0 Console. Write. Line(“It’s

Quiz ¡ Fill the following blank if (__________) X%2 == 0 Console. Write. Line(“It’s even number. ”); else Console. Write. Line(“It’s odd number. ”); ¡ What x, y, z are called ? x 1 x 2 ¡ y Rewrite this sentence int Width, High; Width=10; High=5; ¡ z 10110102 = ? 1 nd Semester 200 7 int _______; Hint: 26 + 24 + 23 + 21 = ? 11

Outline Boolean expression ¡ if statement ¡ nested ifstatement ¡ switch casestatement ¡ 1

Outline Boolean expression ¡ if statement ¡ nested ifstatement ¡ switch casestatement ¡ 1 nd Semester 200 7 12

Nested if statement int N; N = int. Parse(Console. Read. Line()); if#1 if#2 if

Nested if statement int N; N = int. Parse(Console. Read. Line()); if#1 if#2 if (N >= 0) { if (N==0) Console. Write. Line(“N is zero number”); else Console. Write. Line(“N is positive number”); } else Console. Write. Line(“N is negative number”); 1 nd Semester 200 7 13

Nested IF Overview if#1 if#2 if#3 = else#1 if#2 if#3 1 nd Semester 200

Nested IF Overview if#1 if#2 if#3 = else#1 if#2 if#3 1 nd Semester 200 7 14

Nested if statement f(x) = Range 2 x+10, x≤ 5 x 2+10, 5 <

Nested if statement f(x) = Range 2 x+10, x≤ 5 x 2+10, 5 < x ≤ 20 x 3+10, x > 20 Boolean Expression x≤ 5 5 < x ≤ 20 ((5 < x) && (x <= 20)) x > 20 (x > 20) 1 nd Semester 200 7 (x <= 5) 15

Nested if statement double fx = 0; double x = double. Parse(Console. Read. Line());

Nested if statement double fx = 0; double x = double. Parse(Console. Read. Line()); X <= 5 #1 if ( ) #2 fx = 2*x + 10; #3 else if ( ) 5 < x <= 20 #4 fx = x*x + 10; #5 else #6 fx = x*x*x + 10; #7 #8 Console. Write. Line(“f(x) = {0}”, fx); f(x) = 1 nd Semester 200 7 2 x+10, x 2+10, x 3+10, x≤ 5 5 < x ≤ 20 x > 20 16

Outline Boolean expression ¡ if statement ¡ nested ifstatement ¡ switch casestatement ¡ 1

Outline Boolean expression ¡ if statement ¡ nested ifstatement ¡ switch casestatement ¡ 1 nd Semester 200 7 17

switch…case statement ¡ For selecting a statement where its label corresponds to the value

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

Example: switch-case (1) int day_num; day name 1 Sunday 2 Monday 3 Tuesday 4

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

Example: switch-case (2) int month; month 1 January 2 February 3 March 4 April

Example: switch-case (2) int month; month 1 January 2 February 3 March 4 April … …. . 12 December 1 nd Semester 200 7 Console. Write("Input Month"); month = int. Parse(Console. Read. Line()); switch(month) month {case 1: case 3: case 5: Console. Write("This month has 31 day"); break; case 4: case 6: Console. Write("This month has 30 day"); break; default : Console. Write ("Input again"); break; } 20

Example: switch-case (3) <expression>must be int, char, string char version int version char op;

Example: switch-case (3) <expression>must be int, char, string char version int version char op; int day_num; Console. Write("Select + - / * : "); day_num= op=char. Parse(Console. Read. Line()); int. Parse(Console. Read. Line()); switch(op) switch(day_num ) { { <expression> case '+': case 1: Console. Write("{0}+{1}={2}", Console. Write ("Sunday"); x, y, x+y); break; <constant-expression> case '-': case 2: Console. Write("{0}-{1}={2}", console. Write("Monday"); x, y, x-y); break; : : default: Console. Write(“Try again"); Console. Write("Try again"); break; } 21 } nd 1 Semester 200 7

Example: switch-case (4) <expression>must be int, char, string version string op; Console. Write("Select +

Example: switch-case (4) <expression>must be int, char, string version string op; Console. Write("Select + - / * : "); op=Console. Read. Line(); switch(op) { <expression> case “+”: Console. Write("{0}+{1}={2}", x, y, x+y); break; <constant-expression> case “-”: Console. Write("{0}-{1}={2}", x, y, x-y); break; : default: Console. Write("Try again"); break; } 1 nd Semester 200 7 22

Convert switch-case to if else switch version with default int a; a= int. Parse(Console.

Convert switch-case to if else switch version with default int a; a= int. Parse(Console. Read. Line()); <expression> switch (a) { must be case 1 : int, char, string case 2 : Console. Write. Line("Hi"); break; <constantcase 3 : expression> case 4 : Console. Write. Line("Hello"); break; default : Console. Write. Line("Bye"); break; } 1 nd Semester 200 7 if else version if (a == 1 || a == 2) Console. Write. Line("Hi"); else if (a == 3 || a == 4) Console. Write. Line("Hello"); else Console. Write. Line("Bye"); switch version without default switch (a) { case 1 : case 2 : Console. Write. Line("Hi"); break; } 23

Flowchart Symbols Overview ¡ Graphical representation Terminator Process Input/output Condition Connector Flow line 1

Flowchart Symbols Overview ¡ Graphical representation Terminator Process Input/output Condition Connector Flow line 1 nd Semester 200 7 24

Program Flowchart Example START statement 1 statement 2 statement 3 statement 4 END 1

Program Flowchart Example START statement 1 statement 2 statement 3 statement 4 END 1 nd Semester 200 7 25

if statement flowchart START statement 1 CONDITION false if (condition) statement 2; // true

if statement flowchart START statement 1 CONDITION false if (condition) statement 2; // true else { true statement 3 statement 2 statement 3; // false } statement 4; statement 4 1 nd Semester 200 7 26

Quiz 2 ¡ ¡ Fill blanks in the program. Write the program flowchart. 2

Quiz 2 ¡ ¡ Fill blanks in the program. Write the program flowchart. 2 x+10, x≤ 5 f(x) = x 2+10, 5 < x ≤ 20 x 3+10, x > 20 double fx = 0; double x = double. Parse(Console. Read. Line()); if ( ) fx = 2*x + 10; else if ( ) fx = x*x + 10; else fx = x*x*x + 10; Console. Write. Line(“f(x) = {0}”, fx); 1 nd Semester 200 7 27

Summary Boolean Expression ¡ Selection Statements ¡ l l if. . . else. .

Summary Boolean Expression ¡ Selection Statements ¡ l l if. . . else. . . Statement switch-case Statement Selection Problems switch if…else… 1 nd Semester 200 7 28

Exercise 1 (Homework) Input: month number (0 -12) Output: #day in that month Try

Exercise 1 (Homework) Input: month number (0 -12) Output: #day in that month Try to use both if and case ! Ex 1 Please input month: 5 Your month has 31 days. Ex 2 Please input month: 2 Your month has 28 days. 1 nd Semester 200 7 29