1 st Semester Module3 Condition Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department Kasetsart University, Bangkok THAILAND
1 st Semester /24 Outline C# Overview Again Boolean Expression Condition Statement
1 st Semester /24 Simple C# Structure Namespace Class Main()Namespace Class Main() Variable & Constant Location Statements C# Structure
1 st Semester /24 C# Program with Condition Start statement1 statement2 statement3statement4 End Condition true false C# Overview
1 st Semester /24 Outline C# Overview Boolean Expression Condition Statement
1 st Semester /24 Boolean Expression Operators Math Notation C# Notation ExampleMeaning ===x == yx is equal to y? ≠ !=x != yx is not equal to y? >>x > yx is greater than y? ≥ >=x >= yx is greater than or equal to y? <<x < yx is less than y? ≤ <=x <= yx is less than or equal to y? Boolean Expression
1 st Semester /24 Compound Boolean Expression Conditional Operators && - AND && - AND || - OR || - OR ! – NOT ! – NOT Example (a=20; b=12) (a > 12) && (a 12) && (a < 20) (a % 2 == 0) || (b % 2 == 0) (a % 2 == 0) || (b % 2 == 0) Boolean Expression
1 st Semester /24 Precedence rules for Operators 1.( ) parentheses 2.*, /, % 3.+ – 4., = 5.==, != 6.&& 7. ││ 8.If equal precedence, left to right Boolean Expression
1 st Semester /24 Outline C# Overview Boolean Expression Condition Statement
1 st Semester /24 Condition Statement if...else switch…case
1 st Semester /24 if statement syntax condition if (condition) statement; condition Statement; true false if statement
1 st Semester /24 Example: if statement i % 2 == 1 Ood i % 2 == 0 Even false false true true if statement
1 st Semester /24 if statement syntax with multiple statements condition if (condition){ statement1; statement2;} condition Statement1; true false Statement2; if statement
1 st Semester /24 if…else… statement syntax condition if (condition) statement1; else statement2; condition Statement2; truefalse Statement1; if statement
1 st Semester /24 Example: if…else… statement i % 2 == 1 Ood Even true false if statement
1 st Semester /24 Nested IF condtion1 statement2b true false condtion2 condtion3 statement2a statement3a true true false false if statement
1 st Semester /24 Example: Nested IF if statement
1 st Semester /24 Condition Statement if...else switch…case
1 st Semester /24 Switch Case Flowchart Overview x==1 x==2 x==3 x==4 x==5 cmd1; cmd2; cmd3; cmd4; cmd5; break break break break break T T T T T F F F F F
1 st Semester /24 IF statement example (x==1) if (x==1) cmd1; else if (x==2) cmd2; else if (x==3) cmd3; else if (x==4) cmd4; else if (x==5) cmd5;
1 st Semester /24 switch-case example switch (x) { case 1: cmd1; break; case 2: cmd2; break; case 3: cmd3; break; case 4: cmd4; break; case 5: cmd5; break; }
1 st Semester /24 Convert if..else… statement to switch case if (x == 5) && (x%2 == 1) { y=y+5; } else { y=y-5; }true false ((x==5) && (x%2 ==1)) case ((x==5) && (x%2 ==1)){ true: y=y+5; break; break; false: y=y-5; break; }
1 st Semester /24 Switch Case example Minor Cineplex Theater Cost (baht) Title 1180War of the world 2180War of the world 3100Batman Begins 4100Batman Begins 5140Hell!!! 6140Hell!!!
1 st Semester /24 Summary Boolean Expression Condition Statement if statement case statement