# PowerPoint Presentation - Department of Computer Engineering

Document Sample

```					Module3
Selection Statement

Dr. Pattara Leelaprute
ดร. ภทร ลลาพฤทธ
Computer Engineering Department
Kasetsart University
pattara.l@ku.ac.th
http://www.cpe.ku.ac.th/~pattara
1
Outline
n Boolean expression
n if statement
n nested if statement
n switch case statement

2
Boolean Expression
n   Operators
¨ Comparison
n Equal ==
n Not equal !=

n Less <

n Greater >

n Less than or equal to <=

n Greater than or equal to >=

¨ Boolean
n And &&
n Or ||

n Not !                         3
Boolean Expression Example
n   From the equation: X2+9X+10 = 0
¨ 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

n   Condition: Is value Y even number?
(Y%2 == 0) //true if Y is even
OR
(Y%2 != 1) //true if Y is even
4
Example: Boolean Expressions
double x = 4.0;

Expression   Value
x < 5.0        true
___________
x > 5.0        false
___________
x <= 5.0     ___________
true
5.0 == x       false
___________
x != 5.0       true
___________

5
Outline
n Boolean expression
n if statement
n nested if statement
n switch case statement

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

if (condition)         if (condition) {
statement; //true         statement1; //true
statement2; //true
}

7
if statement example
n   BMI (Body Mass Index)
Weight in Kilograms
BMI =
(Height in Meters) X (Height in Meters)

BMI                  Weight Status
Below 18.5     Underweight
18.5 – 24.9     Normal
25.0 – 29.9     Overweight
30.0 and Above   Obese (Extremely Fat)

8
if…else… statement
n  If condition is true à execute
statement1
n If condition is false à execute
statement2           if (condition)
if (condition)
n Syntax:                statement1; //true
statement1; //true
else {
else
statement2; // false
statement2; // false
statement3; // false
}
9
if…else… statement example
n   Question
¨ 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.WriteLine(“It’s even number.”);
else
Console.WriteLine(“It’s odd number.”);
10
Exercise 1: Separated IF (simple)
1. Have you eaten lunch?
2. Do you like noodle?
if (eaten==true)
{Console.WriteLine(“ I’ve already eaten”);}
else
{Console.WriteLine(“ I’ve not eat yet”);}
if (like_noodle==true)
{Console.WriteLine(“I like noodle”);}
else
{Console.WriteLine(“I don’t like noodle”);}
11
Exercise 2: Related two IF (full version)
1. Do you like noodle?
2. If you don’t like noodle, do you like fried rice?
if (like_noodle==true)
{Console.WriteLine(“I like noodle”);}
else
{ Console.WriteLine(“I don’t like noodle”);
if (like_friedrice==true)
{Console.WriteLine(“I like friedrice”);}
else
{Console.WriteLine(“I don’t like friedrice”);}
}                                                      12
Exercise 2: Related two IF (short version)
1. Do you like noodle?
2. If you don’t like noodle, do you like fried rice?
if (like_noodle==true)
{Console.WriteLine(“I like noodle”);}
else if (like_friedrice==true)
{Console.WriteLine(“I like friedrice”);}
else
{Console.WriteLine(“I don’t like friedrice”);}

13
Quiz
n   Fill the following blank
if (___________________)
X%2 == 0
Console.WriteLine(“It’s even number.”);
else
Console.WriteLine(“It’s odd number.”);

n   What x, y, z are called ?
x1
x2            y               z

n   Rewrite this sentence
int Width,High;
Width=10;                          int   _______;
High=5;                            int   _______;

n   10110102 = ?          Hint: 26 + 24 + 23 + 21 = ?        14
Outline
n Boolean expression
n if statement
n nested if statement
n switch case statement

15
Nested if statement
int N;
N = int.Parse(Console.ReadLine());
if#1
if (N >= 0)
{                                         if#2
if (N==0)
Console.WriteLine(“N is zero number”);
else
Console.WriteLine(“N is positive number”);
}
else
Console.WriteLine(“N is negative number”);

16
Nested IF Overview
if#1         if#1
if#2
if#3
else#1
=
if#2
if#3

17
Nested if statement
2x+10,    x≤ 5

f(x) =     x2+10,    5 < x ≤ 20

x3+10,    x > 20

Range          Boolean Expression
x≤ 5                  (x <= 5)
5 < x ≤ 20        ((5 < x) && (x <= 20))
x > 20                 (x > 20)
18
Nested if statement
double fx = 0;
double x = double.Parse(Console.ReadLine());

#1 if (         X <= 5         )
#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.WriteLine(“f(x) = {0}”, fx);

2x+10,     x≤ 5
f(x) =    x2+10,     5 < x ≤ 20
x3+10,     x > 20
19
Exercise 3: Nested two IF (short version)
1. Do you like noodle?
1.1 If you like noodle, do you love Sen-Yai?
1.2 If you don’t like noodle, do you like fried rice?
if (like_noodle==true)
{Console.WriteLine(“I like noodle”);
if (love_SenYai==true)
{Console.WriteLine(“I love Sen-Yai”);}
else
{Console.WriteLine(“I don’t love Sen-Yai”);}
}
else if (like_friedrice==true)
{Console.WriteLine(“I like fried rice”);}
else
{Console.WriteLine(“I don’t like fried rice”);}     20
Outline
n Boolean expression
n if statement
n nested if statement
n switch case statement

21
switch…case statement
n   For selecting a statement where its label
corresponds to the value of the switch
expression.
switch (<expression> )
{
case <constant-expression> :
<statements>;
break;
[default:
<statements>;
break; ]
}
<expression> must be int, char, string   22
Example: switch-case (1)
int day_num;
int     day name    Console.Write("Input the day");
day_num;
day_num =
1        Sunday           int.Parse(Console.ReadLine());

2        Monday     switch(day_num)
day_num      <expression>
{case 1:
3        Tuesday       Console.Write ("Today is Sunday");
break;
4       Wednesday               <constant-expression>
case 2:
5       Thursday       Console.Write("Today is Monday");
break;
6         Friday         :
7        Saturday     default :
Console.Write ("I don’t know");
break;
}                                  23
Example: switch-case (2)
int month;
int month;    month     Console.Write("Input Month");
month = int.Parse(Console.ReadLine());
1        January
switch(month)
month
2        February   {case 1:
case 3:
3         March       case 5:
Console.Write("This month has 31days");
4          April       break;
case 4:
…           …..        case 6:
Console.Write("This month has 30days");
break;
…           …..        default :
Console.Write ("Input again");
12        December      break;
}

24
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.ReadLine()); int.Parse(Console.ReadLine());

switch(op)                          switch(day_num )
{                                   {
<expression>
case '+':                           case 1:
Console.Write("{0}+{1}={2}",       Console.Write ("Sunday");
x,y,x+y);     break;
break;        <constant-expression>
case '-':                           case 2:
Console.Write("{0}-{1}={2}",       console.Write("Monday");
x,y,x-y);     break;
break;                                    :
:                                 default :
default:                             Console.Write(“Try again");
Console.Write("Try again");         break;
break;                           }
}                                                              25
Example: switch-case (4)
<expression> must be int, char, string
string op;
string version
Console.Write("Select + - / * :");
op=Console.ReadLine();

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;
}                                         26
Convert switch-case to if else
switch version with default            if else version
int a;
a=                                    if (a == 1 || a == 2)
int.Parse(Console.ReadLine());         Console.WriteLine("Hi");

switch (a) {     <expression>         else if (a == 3 || a == 4)
case 1 :       must be               Console.WriteLine("Hello");
case 2 :       int , char, string
else
Console.WriteLine("Hi");            Console.WriteLine("Bye");
break;
<constant-
case 3 :
expression>           switch version without default
case 4 :
Console.WriteLine("Hello");
switch (a) {
break;
case 1 :
default :
case 2 :
Console.WriteLine("Bye");
Console.WriteLine("Hi");
break;
break;
}
}
27
Flowchart Symbols Overview
n   Graphical representation
Terminator

Process

Input/output

Condition

Connector
Flow line

28
Program Flowchart Example
START

statement1

statement2

statement3

statement4

END

29
if statement flowchart
START

statement1
statement1
if (condition)
statement 2; // true
CONDITION
else {
false                true       statement 3; // false
statement3     statement2   }
statement 4;

statement4

30
Quiz2
n   Fill           blanks in the program.
n   Write the program flowchart.
2x+10,   x≤ 5
f(x) =  x2+10,    5 < x ≤ 20
x3+10,    x>
double fx = 0; 20
double x = double.Parse(Console.ReadLine());

if (                      )
fx = 2*x + 10;
else if (                     )
fx = x*x + 10;
else
fx = x*x*x + 10;
Console.WriteLine(“f(x) = {0}”, fx);
31
Summary
n Boolean Expression
n Selection Statements
Statement
¨ if...else...
¨ switch-case Statement
Selection Problems

switch
if…else…

32

```
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
 views: 8 posted: 7/2/2013 language: Unknown pages: 32