If-Then-Else Statement 1
Conditional statements (1) +variable scope
Based on slides by
- Asst. Prof. Kitsana Waiyamai, Ph.D.
- Dept. of Computer Engineering
Faculty of Engineering, Kasetsart University Bangkok, Thailand
http://kdl.cpe.ku.ac.th
Conditional statements (1) +variable scope Based on slides by - - PowerPoint PPT Presentation
Conditional statements (1) +variable scope Based on slides by Asst. Prof. Kitsana Waiyamai, Ph.D. Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand http://kdl.cpe.ku.ac.th If-Then-Else Statement 1
If-Then-Else Statement 1
Based on slides by
Faculty of Engineering, Kasetsart University Bangkok, Thailand
http://kdl.cpe.ku.ac.th
If-Then-Else Statement 2
Now, some statements may be executed only if certain conditions
are satisfied, otherwise, the statements will be skipped
number = int.Parse(Console.ReadLine()); if(number>=0) Console.WriteLine("The number is positive"); else Console.WriteLine("The number is negative");
If-Then-Else Statement 3
A kind of logical data type with value either true or false
Console.WriteLine(2+3 == 5); Console.WriteLine(3*4 == 7*4/2); Console.WriteLine(100>90); Console.WriteLine("Pascal" == "Pascal");
True False True True
If-Then-Else Statement 4
What can the operands be? Arithmetic expressions, e.g., 1+1*2/3 Characters, e.g. 'a' == 'b' Strings, e.g. "Pascal" == "Pascal" Variables
If-Then-Else Statement 5
Relational Operator in Pascal Meaning == Equal to != Not equal to > Greater than < Less than >= Greater than or equal to <= Less than or equal to
If-Then-Else Statement 6
Boolean constants and Boolean variables are of type bool
static void Main(string[] args) { const bool sunRiseFromEast = true; const bool oneGreaterThanTwo = false; bool boy; Console.WriteLine("The sun rises from the east is " + sunRiseFromEast); boy = false; Console.WriteLine("It is " + boy + " that Mary is a boy"); }
If-Then-Else Statement 7
Boolean Expression Value 2.5 == 5 false 4%10 > 1-3 true 5.7 >= -2 true 5/2 <= 1.1*2 false 999!=999.9 false
If-Then-Else Statement 8
ASCII – American Standard Code for
Contains 128 characters
If-Then-Else Statement 9
Boolean Expression ’\’ != ’j’ ’9’ > ’4’ ’{’ >= ’}’ ASCII of First Operand 92 57 123 ASCII of Second Operand 106 52 125 Result true true false
If-Then-Else Statement 10
Boolean Expression Result Remark "Z" > "Other" true ‘Z’ has larger ASCII value then ‘O’ "small" > "smaller" false ‘small’ does not have the sixth character "Space" < "NoSpace" true The character ‘ ’ has smaller ASCII value than the character ‘N’ "Timmy" >= "Tommy" false The character ‘i’ of the first operand has smaller ASCII value then ‘o’ of the second
If-Then-Else Statement 11
&& - conditional AND || - conditional OR ^ - logical exclusive OR (logical XOR) !
(age >= 18) && (age <= 20) (grade == 'F') || (grade == 'W')
If-Then-Else Statement 12
Truth Table for conditional AND operator (&&) Truth Table for conditional OR operator (||)
Boolean expression P Boolean expression Q P && Q TRUE TRUE TRUE TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
Boolean expression P Boolean expression Q P || Q TRUE TRUE TRUE TRUE FALSE TRUE FALSE TRUE TRUE FALSE FALSE FALSE
If-Then-Else Statement 13
Truth Table for logical exclusive OR operator Truth table for logical NOT operator
Boolean Expression P ! P
TRUE FALSE FALSE TRUE
Boolean expression P Boolean expression Q P ^ Q TRUE TRUE FALSE TRUE FALSE TRUE FALSE TRUE TRUE FALSE FALSE FALSE
If-Then-Else Statement 14
If-Then-Else Statement 15
can reduce execution time
If-Then-Else Statement 16
Operators Associativity Type () ++ -- left to right right to left parentheses unary postfix ++ -- + - ! (type) right to left unary prefix * / % left to right multiplicative + - left to right additive < <= > >= left to right relational == != left to right equality & left to right logical AND ^ left to right logical XOR | left to right logical OR && left to right conditional AND || left to right conditional OR ?: right to left conditional = += -= *= /= %= right to left assignment
If-Then-Else Statement 17
!('F'!='M')||(19 > 12)&&(19 < 18) !( true )||(19 > 12)&&(19 < 18) ( false )||(19 > 12)&&(19 < 18) ( false )||( true )&&(19 < 18) ( false )||( true )&&( false ) ( false ) || ( false ) ( false )
If-Then-Else Statement 18
!('F'=='M')||(19 > 12)&&(19 < 18) !( false )||(19 > 12)&&(19 < 18) ( true )||(19 > 12)&&(19 < 18) ( true ) (true) || ?? true
If-Then-Else Statement 19
!('F‘!='M')||(19 < 12)&&(19 > 18) !( true )||(19 < 12)&&(19 > 18) ( false )||(19 < 12)&&(19 > 18) ( false ) ( false )||( false )&&(19 > 18) ( false )||( false )
If-Then-Else Statement 20
If statements If-else statements Nested if statements Switch statements
If-Then-Else Statement 21
If-Then-Else Statement 22
If a student has score higher than 90,
Score > 90
‘very good student’ True False
If-Then-Else Statement 23
Syntax:
if (<boolean value>) statement;
statement is executed, if the boolean value is true. If more than one statements are to be executed when the boolean value is true, we need to group them with { }. e.g.,
if (<boolean value>) { statement1; statement2; }
If-Then-Else Statement 24
class MainClass { static void Main() { int num; Console.Write("Please input a number :"); num = int.Parse(Console.ReadLine()); if(num < 0) Console.WriteLine("negative"); if(num == 0) Console.WriteLine("zero"); if(num > 0) Console.WriteLine("positive"); } }
If-Then-Else Statement 25
When there is only one of two alternative statements to
If Mark variable is greater
Mark >= 50 Output ‘pass’ Output ‘fail’
True False
If-Then-Else Statement 26
condition False True Statement2 Statement1
If-Then-Else Statement 27
class PassOrFail { static void Main() { Console.Write("Enter mark: "); int mark = int.Parse(Console.ReadLine()); if(mark >= 50) Console.WriteLine("you pass the exam"); else Console.WriteLine("you fail the exam"); } }
If-Then-Else Statement 28
class MainClass { static void Main() { int sum = 10; int num; Console.Write("Please input a number :"); num = int.Parse(Console.ReadLine()); if(num < 0) num = 0; else num = 20; sum += num; Console.WriteLine("Sum = " + sum); } }
If-Then-Else Statement 29
Enter mark: 80 Congratulation! You mark is greater than 50. You pass the examination ! Enter mark: 30 Sorry! You mark is less than 50. You fail the examination ! Mark >= 50
Statement 1 Statement 1 Statement 2 Statement 3 Statement 2 Statement 3
If-Then-Else Statement 30
Compound statement
by '{' and '}' to form a compound statement class PassOrFail { static void Main() { int mark = int.Parse(Console.ReadLine()); if(mark >= 50) { Console.WriteLine("congratulations!"); Console.WriteLine("your mark is greater than 50"); Console.WriteLine(‘you pass the exam’); } else { Console.WriteLine("sorry!"); Console.WriteLine("your mark is less than 50"); Console.WriteLine("you fail the exam"); } } }
If-Then-Else Statement 31
class PassOrFail { static void Main() { int mark = int.Parse(Console.ReadLine()); if(mark >= 50) { int a; a = int.Parse(Console.ReadLine()); Console.WriteLine(a+100); } else { string line = Console.ReadLine(); Console.WriteLine("Sorry " + line); } } }
scope of "line" scope of "a"
If-Then-Else Statement 33
If statement may be inserted in the then-part or the else-part of another if statement to form a nested if statement.
Condition 2 Statement a2 Statement a1 Condition 3 Statement b2 Statement b1 Condition 1
If-Then-Else Statement 34
The program receives three different numbers and displays the smallest number
static void Main(string[] args) { int a,b,c; a = int.Parse(Console.ReadLine()); b = int.Parse(Console.ReadLine()); c = int.Parse(Console.ReadLine()); Console.Write("The smallest is "); if(a<b) if(a<c) Console.WriteLine(a); else Console.WriteLine(c); else if(b<c) Console.WriteLine(b); else Console.WriteLine(c); }
a<c c a b<c c b a < b True True True False False False