selection statement 01204111 – selection statement modified from aj. thanachat thanomkulabut’s...

28
Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012

Upload: catherine-ryan

Post on 13-Dec-2015

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012

Selection Statement

01204111 – Selection Statement

Modified from Aj. Thanachat Thanomkulabut’s slide

1st semester, 2012

Page 2: Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012

Outline2

Boolean expression Flowchart if statement if…else… statement nested if statement switch case statement

Page 3: Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012

Boolean expression

Operators Comparison

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

Boolean And && Or || Not !

3

Boolean Expression

0 and 0 = 00 and 1 = 01 and 0 = 01 and 1 = 1

0 or 0 = 00 or 1 = 11 or 0 = 11 or 1 = 1

not 0 = 1not 1 = 0

Page 4: Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012

4

Boolean expression

int Y; Y

Is Y greater than 3? Y>3

Is Y less than 5? Y<5

Is Y between 3 and 5? (Y<5)(Y>3)&&

Boolean Expression

Page 5: Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012

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 Y an 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

5

Boolean Expression

Page 6: Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012

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) ___________

truefalsetruefalsetrue

6

Boolean Expression

falsetrue

Page 7: Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012

7

Boolean variable

Used for storing Boolean value true false

Keyword: bool Usage example: int X = 0;

bool MyVar = true; //Boolean variable declaration

MyVar = (X>0); //Boolean expression

Boolean variable

Page 8: Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012

Outline8

Boolean expression Flowchart if statement if…else… statement

Page 9: Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012

Flowchart symbols overview

Graphical representation

Terminator

Process

Input/output

Condition

Connector

Flow line

9

Flowchart

Page 10: Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012

Program flowchart example

Start

Statement1

Statement2

Statement3

Statement4

End

10

Flowchart

Page 11: Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012

Outline11

Boolean expression Flowchart if statement if…else… statement

Page 12: Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012

if statement

Execute the specific statement when the ”condition” becomes true

Syntax:

if (condition)

statement;

if (condition) {

statement1;

statement2;

}if (condition) {

statement;

}

12

if statement

true

true

Page 13: Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012

if statement13

conditionFalse

True

Statement

if (condition)

statement;

if (condition) {

statement;

}

if statement

Page 14: Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012

if statement14

condition

FalseTrue

Statement1

Statement2

if (condition) {

statement1;

statement2;

}

if statement

Page 15: Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012

15

if statement with Boolean variable

bool Y;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

Page 16: Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012

if statement

16

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 Kilograms

(Height in Meters) X (Height in Meters)

16

Page 17: Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012

if statement

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 Kilograms

(Height in Meters) X (Height in Meters)

17

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 statement example

if(BMI>=30.0) Console.WriteLine(“Obese”);

Page 18: Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012

Outline19

Boolean expression Flowchart if statement if…else… statement

Page 19: Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012

if…else… statement flowchartstatement1;

if (condition) {

statement2; //true

}

else {

statement3; //false

}

statement4;

Start

Statement1

Condition

Statement2 Statement3

true false

Statement4

End

20

Flowchart

Page 20: Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012

if…else… statement

If condition is true execute statement1 If condition is false execute statement2 Syntax:if (condition)

statement1; //true

else

statement2; //false

if (condition)

statement1; //true

else {

statement2; //false

statement3; //false

}

21

if…else… statement

Page 21: Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012

if…else… statement22

conditionFalseTrue

Statement2Statement1

if (condition)

statement1; //true

else

statement2; //false

if…else… statement

Page 22: Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012

if…else… statement23

if (condition)

statement1; //true

else {

statement2; //false

statement3; //false

}

conditionFalseTrue

Statement2

Statement1

Statement3

if…else… statement

Page 23: Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012

if… else… statement flowchart

24

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

if (n>0)

n= 2*n+5;

else {

Console.Write(“Go”);

n = n%4;

}

Start

n>0

n=2*n+5;

true false

n=n%4;

End

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

Console.Write(“Go”);

Flowchart

Page 24: Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012

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 Output

Even Number It’s even number.

Odd Number It’s odd number.

if…else… statement

Page 25: Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012

if…else… statement example

26

Value of n OutputEven Number It’s even number.

Odd Number It’s odd number.

if(n%2 == 0)

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

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

if…else… statement

Page 26: Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012

Test I27

Write a program that computes the value of a f(x) based on the value of x input : number x output : f(x)

f(x) x

x2+1 x<0

x+2 x≥0

if…else… statement

Page 27: Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012

Summary28

Boolean expression Flowchart if statement if…else… statement nested if statement switch case statement

Page 28: Selection Statement 01204111 – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012

Any question?