Download presentation
Presentation is loading. Please wait.
Published byArlene Strickland Modified over 8 years ago
1
1 st Semester 2005 1 Module3 Condition Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang aphirak.j@ku.ac.th http://www.cpe.ku.ac.th/~aphirak Computer Engineering Department Kasetsart University, Bangkok THAILAND
2
1 st Semester 2005 2/24 Outline C# Overview Again Boolean Expression Condition Statement
3
1 st Semester 2005 3/24 Simple C# Structure Namespace Class Main()Namespace Class Main() Variable & Constant Location Statements C# Structure
4
1 st Semester 2005 4/24 C# Program with Condition Start statement1 statement2 statement3statement4 End Condition true false C# Overview
5
1 st Semester 2005 5/24 Outline C# Overview Boolean Expression Condition Statement
6
1 st Semester 2005 6/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
7
1 st Semester 2005 7/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
8
1 st Semester 2005 8/24 Precedence rules for Operators 1.( ) parentheses 2.*, /, % 3.+ – 4., = 5.==, != 6.&& 7. ││ 8.If equal precedence, left to right Boolean Expression
9
1 st Semester 2005 9/24 Outline C# Overview Boolean Expression Condition Statement
10
1 st Semester 2005 10/24 Condition Statement if...else switch…case
11
1 st Semester 2005 11/24 if statement syntax condition if (condition) statement; condition Statement; true false if statement
12
1 st Semester 2005 12/24 Example: if statement i % 2 == 1 Ood i % 2 == 0 Even false false true true if statement
13
1 st Semester 2005 13/24 if statement syntax with multiple statements condition if (condition){ statement1; statement2;} condition Statement1; true false Statement2; if statement
14
1 st Semester 2005 14/24 if…else… statement syntax condition if (condition) statement1; else statement2; condition Statement2; truefalse Statement1; if statement
15
1 st Semester 2005 15/24 Example: if…else… statement i % 2 == 1 Ood Even true false if statement
16
1 st Semester 2005 16/24 Nested IF condtion1 statement2b true false condtion2 condtion3 statement2a statement3a true true false false if statement
17
1 st Semester 2005 17/24 Example: Nested IF if statement
18
1 st Semester 2005 18/24 Condition Statement if...else switch…case
19
1 st Semester 2005 19/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
20
1 st Semester 2005 20/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;
21
1 st Semester 2005 21/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; }
22
1 st Semester 2005 22/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; }
23
1 st Semester 2005 23/24 Switch Case example Minor Cineplex Theater Cost (baht) Title 1180War of the world 2180War of the world 3100Batman Begins 4100Batman Begins 5140Hell!!! 6140Hell!!!
24
1 st Semester 2005 24/24 Summary Boolean Expression Condition Statement if statement case statement
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.