1 nd Semester 2007 1 Module3 Selection Statement Thanawin Rakthanmanon Email: fengtwr@ku.ac.th...

Post on 31-Dec-2015

217 views 1 download

Transcript of 1 nd Semester 2007 1 Module3 Selection Statement Thanawin Rakthanmanon Email: fengtwr@ku.ac.th...

11nd Semester 2007

ModuleModule33 Selection StatementSelection Statement

Thanawin RakthanmanonEmail: fengtwr@ku.ac.th

Create by: Aphirak JansangComputer Engineering Department

Kasetsart University, Bangkok THAILAND

21nd Semester 2007

Outline

Boolean expression ifif statement nested ifnested if statement switch caseswitch case statement

31nd Semester 2007

Boolean Expression Operators

Comparison Equal ==== Not equal !=!= Less << Greater >> Less than or equal to <=<= Greater than or equal to >=>=

Boolean And &&&& Or |||| Not !!

41nd Semester 2007

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 //true if X is the answeranswer

(Y%2 == 0) //true if Y is even//true if Y is even

OROR

(Y%2 != 1) //true if Y is even//true if Y is even

51nd Semester 2007

Example: Boolean Expressions

double x = 4.0; Expression Valuex < 5.0 ___________x > 5.0 ___________x <= 5.0 ___________5.0 == x ___________x != 5.0 ___________

truefalsetruefalsetrue

61nd Semester 2007

Outline

Boolean expression ifif statement nested ifnested if statement switch caseswitch case statement

71nd Semester 2007

if statement

Execute the specific statement when the ”condition””condition” becomes truetrue

Syntax:

if if (condition)(condition)

statement;statement; //true//true

if if (condition)(condition) {{

statement1; statement1; //true//true

statement2; statement2; //true//true

}}

81nd Semester 2007

if statement example

BMI (Body Mass Index)

BMI Weight Status

Below 18.5 Underweight

18.5 – 24.9 Normal

25.0 – 29.9 Overweight

30.0 and Above Obese (Extremely Fat)

BMI = Weight in KilogramsWeight in Kilograms

(Height in Meters) X (Height in Meters)(Height in Meters) X (Height in Meters)

91nd Semester 2007

if…else… statement

If conditioncondition is truetrue execute statement1statement1

If conditioncondition is falsefalse execute statement2statement2

Syntax:if if (condition)(condition)

statement1; statement1; //true//true

elseelse

statement2; statement2; ////falsefalse

if if (condition)(condition)

statement1; statement1; //true//true

elseelse { {

statement2; statement2; ////falsefalse

statement3; statement3; ////falsefalse

}}

101nd Semester 2007

if…else… statement example Question

Value in variable N is Odd or Even Number?

Value in N OutputEven Number It’s even number.

Odd Number It’s odd number.

if if (___________________)(___________________)

Console.WriteLine(“It’s even Console.WriteLine(“It’s even number.”);number.”);

elseelse

Console.WriteLine(“It’s odd Console.WriteLine(“It’s odd number.”);number.”);

111nd Semester 2007

Quiz Fill the following blank

What x, y, z are called ?

Rewrite this sentence

10110102 = ?

if if (___________________)(___________________)

Console.WriteLine(“It’s even Console.WriteLine(“It’s even number.”);number.”);

elseelse

Console.WriteLine(“It’s odd Console.WriteLine(“It’s odd number.”);number.”);

xx11xx22 zzyy

int Width,High;int Width,High;Width=10;Width=10;High=5;High=5;

int int ______________;;int int ______________;;

Hint: 26 + 24 + 23 + 21 = ?

X%2 == 0

121nd Semester 2007

Outline

Boolean expression ifif statement nested ifnested if statement switch caseswitch case statement

131nd Semester 2007

Nested if statement

int N;int N;N = int.Parse(Console.ReadLine());N = int.Parse(Console.ReadLine());

if (N >= 0) if (N >= 0) {{ if (N==0)if (N==0) Console.WriteLine(“N is zero number”);Console.WriteLine(“N is zero number”); elseelse Console.WriteLine(“N is positive number”); Console.WriteLine(“N is positive number”); }}else else Console.WriteLine(“N is negative number”);Console.WriteLine(“N is negative number”);

if#1if#1

if#2if#2

141nd Semester 2007

Nested IF Overview

if#1if#1if#2if#2

if#3if#3

if#1if#1

if#2if#2

if#3if#3

else#1else#1=

151nd Semester 2007

Nested if statement

f(x) =f(x) =

2x+10, x ≤ 52x+10, x ≤ 5

xx22+10, 5 < x ≤ 20+10, 5 < x ≤ 20

xx33+10, x > 20+10, x > 20

Range Boolean Expression

x ≤ 5x ≤ 5 (x (x <<= 5)= 5)

5 < x ≤ 205 < x ≤ 20 ((5 < x) && (x <= 20))((5 < x) && (x <= 20))

x > 20x > 20 (x > 20)(x > 20)

161nd Semester 2007

Nested if statement

f(x) =f(x) =2x+10, x ≤ 52x+10, x ≤ 5xx22+10, 5 < x ≤ 20+10, 5 < x ≤ 20xx33+10, x > 20+10, x > 20

double fx = 0;double fx = 0;double x = double.Parse(Console.ReadLine());double x = double.Parse(Console.ReadLine());

#1 #1 if ( )if ( )#2#2 fx = 2*x + 10; fx = 2*x + 10;#3 #3 else if ( )else if ( )#4 #4 fx = x*x + 10; fx = x*x + 10;#5#5 else else #6#6 fx = x*x*x + 10; fx = x*x*x + 10;#7#7#8 #8 Console.WriteLine(“f(x) = {0}”, fx);Console.WriteLine(“f(x) = {0}”, fx);

X <= 5

5 < x <= 20

171nd Semester 2007

Outline

Boolean expression ifif statement nested ifnested if statement switch caseswitch case statement

181nd Semester 2007

switch…case statement For selecting a statement where its label

corresponds to the value of the switch expression.

switch (<expression><expression>){ case <constant-<constant-expression>expression> : <statements>;<statements>; break;break; [default: <statements>;<statements>; break;break;]}<expression><expression> must be int, char, string must be int, char, string

191nd Semester 2007

Example: switch-case (1)

int day_num;

day name

1 Sunday

2 Monday

3 Tuesday

4 Wednesday

5 Thursday

6 Friday

7 Saturday

int day_num;

Console.Write("Input the day"); day_num = int.Parse(Console.ReadLine());

switch(day_numday_num) {case 1:1: Console.Write ("Today is Sunday"); break;

case 2:2: Console.Write("Today is Monday"); break; : default :: Console.Write ("I don’t know"); break;}

<expression><expression>

<constant-expression><constant-expression>

201nd Semester 2007

Example: switch-case (2)

int month;

month

1 January

2 February

3 March

4 April

… …..

… …..

12 December

int month;

Console.Write("Input Month"); month = int.Parse(Console.ReadLine());

switch(monthmonth) {case 1:1: case 3:3: case 5:5: Console.Write("This month has 31day"); break; case 4:4: case 6:6: Console.Write("This month has 30day"); break; default :: Console.Write ("Input again"); break;}

211nd Semester 2007

Example: switch-case (3)

char op;Console.Write("Select + - / * :");op=char.Parse(Console.ReadLine());

switch(op){ case '+': Console.Write("{0}+{1}={2}", x,y,x+y); break; case '-': Console.Write("{0}-{1}={2}", x,y,x-y); break; : default: Console.Write("Try again"); break;}

<expression><expression> must be must be int, char, stringint, char, string

int day_num; day_num=int.Parse(Console.ReadLine());

switch(day_num ) { case 1: Console.Write ("Sunday"); break;

case 2: console.Write("Monday"); break; : default : Console.Write(“Try again"); break;}

<expression><expression>

iintnt version versionchar versionchar version

<<constant-constant-expression>expression>

221nd Semester 2007

Example: switch-case (4)

string op;Console.Write("Select + - / * :");op=Console.ReadLine();

switch(op){ case “+”: Console.Write("{0}+{1}={2}", x,y,x+y); break; case “-”: Console.Write("{0}-{1}={2}", x,y,x-y); break; : default: Console.Write("Try again"); break;}

<expression><expression> must be must be int, char, stringint, char, string

string versionstring version

<expression><expression>

<<constant-constant-expression>expression>

231nd Semester 2007

Convert switch-case to if else

int a;a=int.Parse(Console.ReadLine());

switch (a) { case 1 : case 2 : Console.WriteLine("Hi"); break; case 3 : case 4 : Console.WriteLine("Hello"); break; default : Console.WriteLine("Bye"); break;}

switch version with defaultswitch version with default

<<constant-constant-expression>expression>

<expression><expression>must be must be int, char, stringint, char, string

if (a == 1 || a == 2) Console.WriteLine("Hi");

else if (a == 3 || a == 4) Console.WriteLine("Hello");

else Console.WriteLine("Bye");

if else versionif else version

switch (a) { case 1 : case 2 : Console.WriteLine("Hi"); break;}

switch version without defaultswitch version without default

241nd Semester 2007

Flowchart Symbols Overview

Graphical representation

Terminator

Process

Input/output

Condition

Connector

Flow line

251nd Semester 2007

Program Flowchart Example

START

statement1statement1

statement2statement2

statement3statement3

statement4statement4

END

261nd Semester 2007

if statement flowchart

statement1statement1

if if (condition)(condition)

statementstatement22; ; ////truetrue

elseelse { {

statementstatement33; ; ////falsefalse

}}

statementstatement44; ;

truetruefalsefalse

CONDITIONCONDITION

START

statement1statement1

statement2statement2statement3statement3

statement4statement4

271nd Semester 2007

Quiz2

f(x) =f(x) =2x+10, x ≤ 52x+10, x ≤ 5

xx22+10, 5 < x ≤ 20+10, 5 < x ≤ 20xx33+10, x > +10, x > 20 20double fx = 0;double fx = 0;

double x = double.Parse(Console.ReadLine());double x = double.Parse(Console.ReadLine());

if ( )if ( ) fx = 2*x + 10;fx = 2*x + 10;else if ( )else if ( ) fx = x*x + 10;fx = x*x + 10; else else fx = x*x*x + 10;fx = x*x*x + 10;Console.WriteLine(“f(x) = {0}”, fx);Console.WriteLine(“f(x) = {0}”, fx);

Fill blanks in the program. Write the program flowchart.

281nd Semester 2007

Summary

Boolean Expression Selection Statements

if...else... Statement switch-case Statement

if…else…if…else…

Selection ProblemsSelection Problems

switchswitch

291nd Semester 2007

Exercise1 (Homework)

Ex1Please input month: 5Your month has 31 days.

Input: month number (0-12)

Output: #day in that month

Ex2Please input month: 2Your month has 28 days.

Try to use both if and case !