Post on 17-Jan-2016
Selection StatementThanachat Thanomkulabut
Outline2
Boolean expression ifif statement nested if nested if statement switch case switch case statement Flowchart
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
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
4
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) ___________
truefalsetruefalsetrue
5
Boolean Expression
falsetrue
Outline6
Boolean expression ifif statement nested if nested if statement switch case switch case statement Flowchart
if statement
Execute the specific statement when the ”condition””condition” becomes truetrue
Syntax:
if if (condition)(condition)
statement; statement;
if if (condition) (condition) {{
statement1; statement1;
statement2; statement2;
}}if if (condition)(condition) {{
statement; statement;
}}
7
if statement
truetrue
truetrue
if statement8
conditionFalse
True
Statement
if if (condition)(condition)
statement; statement;
if if (condition)(condition) {{
statement; statement;
}}
if statement
if statement9
condition
FalseTrue
Statement1
Statement2
if if (condition)(condition) {{
statement1; statement1;
statement2; statement2;
}}
if statement
if statement
10
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)
10
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 KilogramsWeight in Kilograms
(Height in Meters) X (Height in Meters)(Height in Meters) X (Height in Meters)
11
double BMI = W /(H*H);double BMI = W /(H*H);
if(BMI<18.5)if(BMI<18.5) Console.WriteLine(“Underweight”);Console.WriteLine(“Underweight”);
if(BMI>=18.5 && BMI<=24.9)if(BMI>=18.5 && BMI<=24.9) Console.WriteLine(“Normal”);Console.WriteLine(“Normal”);
if(BMI>=25.0 && BMI<=29.9)if(BMI>=25.0 && BMI<=29.9) Console.WriteLine(“Overweight”);Console.WriteLine(“Overweight”);
if statement example
if(BMI>=30.0)if(BMI>=30.0) Console.WriteLine(“Obese”);Console.WriteLine(“Obese”);
if statement
Test I
Selfish Ratio
Ratio Output
More than 1 You are selfish
1 You break even
Less than 1 You are giver
12
Selfish Ratio =RecievingRecieving
GivingGiving
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
}}
13
if statement
if…else… statement14
conditionFalseTrue
Statement2Statement1
if if (condition)(condition)
statement1; statement1; //true//true
elseelse
statement2; statement2; ////falsefalse
if statement
if…else… statement15
if if (condition)(condition)
statement1; statement1; //true//true
elseelse { {
statement2; statement2; ////falsefalse
statement3; statement3; ////falsefalse
}}
conditionFalseTrue
Statement2
Statement1
Statement3
if statement
if…else… statement example
16
Write the program which check input number. 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 statement
if…else… statement example
17
Value in N OutputEven Number It’s even number.
Odd Number It’s odd number.
if statement
if(n%2 == 0)if(n%2 == 0)
Console.WriteLine(“It’s even number”);Console.WriteLine(“It’s even number”);
elseelse
Console.WriteLine(“It’s odd number”);Console.WriteLine(“It’s odd number”);
Test II18
Write the program which find the value of function input : number output :
f(x) x
x2+1 x<0
x+2 x≥0
if statement
Outline19
Boolean expression ifif statement nested if nested if statement switch case switch case statement FlowChart
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 Console.WriteLine(“N is zero number”);number”); elseelse Console.WriteLine(“N is positive Console.WriteLine(“N is positive number”); number”); }}else else Console.WriteLine(“N is negative Console.WriteLine(“N is negative number”);number”);
if#1if#1
20
Nested if statement
if#2if#2
Nested if statement
int N;int N;N = int.Parse(Console.ReadLine());N = int.Parse(Console.ReadLine());
if (N > 0) if (N > 0) Console.WriteLine(“N is positive Console.WriteLine(“N is positive number”); number”);
elseelse if (N==0)if (N==0) Console.WriteLine(“N is zero Console.WriteLine(“N is zero number”);number”); elseelse Console.WriteLine(“N is negative Console.WriteLine(“N is negative number”);number”);
if#1if#1if#2if#2
21
Nested if statement
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 Console.WriteLine(“N is zero number”);number”); elseelse Console.WriteLine(“N is positive Console.WriteLine(“N is positive number”); number”); }}else else Console.WriteLine(“N is negative Console.WriteLine(“N is negative number”);number”);
-5
-5 >= 0 False
N is negative number
N
-5
0
0
0 >= 0 TrueTrue
N is zero number
3
3
3 >= 0
False
N is positive number
22
Nested if statement
Nested if statement
int N;int N;N = int.Parse(Console.ReadLine());N = int.Parse(Console.ReadLine());
if (N > 0) if (N > 0) Console.WriteLine(“N is positive Console.WriteLine(“N is positive number”); number”);
elseelse if (N==0)if (N==0) Console.WriteLine(“N is zero Console.WriteLine(“N is zero number”);number”); elseelse Console.WriteLine(“N is negative Console.WriteLine(“N is negative number”);number”);
N
-4
-4
-4 > 0 FalseFalse
FalseFalse
N is negative number
0
0
0 > 0
TrueTrue
N is zero number
3
3
3 > 0 TrueTrue
N is positive number
23
Exercise 1: Separated IF (simple)
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”);}
1. Have you eaten lunch?2. Do you like noodle?
24
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”);} }
1. Do you like noodle? 2. If you don’t like noodle, do you like fried rice?
Exercise 2: Related two IF (full version)25
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”);}
1. Do you like noodle? 2. If you don’t like noodle, do you like fried rice?
Exercise 2: Related two IF (short version)26
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”);}
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?
Exercise 3: Nested two IF (short version)27
Nested if statement example28
Write the program which show student’s grade input : score of student output :
Score Grade
80 – 100 A
70 – 79 B
60 – 69 C
50 – 59 D
0 – 49 F
Nested if statement
Test III29
Write the program which calculation value of following function input : value of x output : function value of x according
with ...
Nested if statement
f(x) =f(x) =2x+10, x ≤ 52x+10, x ≤ 5
xx22+10, 5 < x ≤ 20+10, 5 < x ≤ 20xx33+10, x > 20+10, x > 20
Outline30
Boolean expression ifif statement nested if nested if statement switch case switch case statement Flowchart
switch…case statement
For selecting a statement where its label corresponds to the value of the switch expression.
switch (<expression><expression>){ case <constant-expression> <constant-expression> : <statements>;<statements>; break;break;
[default: <statements>;<statements>; break;break;]}
<expression><expression> must be int, must be int, char, stringchar, string
31
switch...case statement
32
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>
32
switch...case statement
1230
Test III33
Write the program which show numbers of day in each months input : Abbreviation of months output : numbers of day in each months
Input Output
Jan 31
Feb 28 or 29
Mar 31
Api 30
May 31
... ...
switch...case statement
34
Example: switch-case (2)string month;
Console.Write("Input Month"); month = Console.ReadLine();
switch(monthmonth) {case “Jan”:“Jan”: case “Mar”:“Mar”: case “May”:“May”: Console.Write("This month has 31days"); break; case “Apr”:“Apr”: case “Jun”:“Jun”: Console.Write("This month has 30days"); break; default :: Console.Write (“Wrong Input"); break;}
34
switch...case statement
Input Output
Jan 31
Feb 28 or 29
Mar 31
Api 30
May 31
... ...
35
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>
int versionint versionchar versionchar version
<constant-expression><constant-expression>
35
switch...case statement
36
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-expression><constant-expression>
36
switch...case statement
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
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
37
switch...case statement
Outline38
Boolean expression ifif statement nested if nested if statement switch case switch case statement Flowchart
Flowchart Symbols Overview
Graphical representation
Terminator
Process
Input/output
Condition
Connector
Flow line
39
Flowchart
Program Flowchart Example
StartStart
Statement1Statement1
Statement2Statement2
Statement3Statement3
Statement4Statement4
EndEnd
40
Flowchart
if statement flowchartstatement1;statement1;
if if (condition)(condition)
statement2; statement2; ////truetrue
elseelse { {
statement3; statement3; ////falsefalse
}}
statement4; statement4;
StartStart
Statement1Statement1
ConditionCondition
Statement2Statement2 Statement3Statement3
truetrue falsfalsee
Statement4Statement4
EndEnd
41
Flowchart
if statement flowchart42n= n= int.Parse(Console.Reaint.Parse(Console.ReadLine());dLine());
if if (n>0)(n>0)
n= 2*n+5; n= 2*n+5;
elseelse { {
Console.Write(“Go”);Console.Write(“Go”);
n = n%4;n = n%4;
}}
StartStart
n>0n>0
n=2*n+5;n=2*n+5;
truetrue falsefalse
n=n%4;n=n%4;
EndEnd
n=int.Parse(Console.ReadLine());
Console.Write(“Go”);
Test IV43
Write flow chart which accord to following programn= n=
int.Parse(Console.ReadLine());int.Parse(Console.ReadLine());
n = n%5;n = n%5;
if if (n==2)(n==2)
Console.WriteLine(“Remainder Console.WriteLine(“Remainder is 2”);is 2”);
elseelse { {
n = n*10;n = n*10;
}}
Console.WriteLine(“Program Console.WriteLine(“Program End”);End”);
Any question?