Title: Module3 Condition Statement
1Module3 Condition Statement
???????? ???????????Aphirak Jansang aphirak.j_at_ku.
ac.th http//www.cpe.ku.ac.th/aphirak Computer
Engineering Department Kasetsart University,
Bangkok THAILAND
2Outline
- C Overview Again
- Boolean Expression
- Condition Statement
3Simple C Structure
C Structure
Variable Constant Location
Statements
4In Main() method
Main() method
- Variable Constant Declaration
- Statements
5C Variable Declaration
Variable Constant
- Syntax
- ltdata typegt ltnamegt
- Example
- We can also assign its initial value. E.g.,
int radius double area int a,b,c bool isokay
int k 200 bool done false
6C Constant Declaration
Variable Constant
- Syntax
- const ltdata typegt ltnamegt ltvaluegt
- Example
const int radius 15 const double
area1.5 const bool isokaytrue const string
movieStarWarIII
7C Program Example
Variable Constant
8In Main() method
- Variable Constant Declaration
- Statements
9C Statements
Statement
- Assignment Statement
- Ex. x 10
- Input Statement
- Ex. st Console.ReadLine()
- Output Statement
- Ex. Console.WriteLine(Hello World!)
10Simple C Program
C Overview
11C Program with Condition
C Overview
12Outline
- C Overview
- Boolean Expression
- Condition Statement
13Boolean Expression
Boolean Expression
Math Notation C Notation Example Meaning
x y x is equal to y?
? ! x ! y x is not equal to y?
gt gt x gt y x is greater than y?
gt x gt y x is greater than or equal to y?
lt lt x lt y x is less than y?
lt x lt y x is less than or equal to y?
14Example Boolean Expression
Boolean Expression
- double x 4.0
-
- Expression Value
- x lt 5.0 ___________
- x gt 5.0 ___________
- x lt 5.0 ___________
- 5.0 x ___________
- x ! 5.0 ___________
15Example More Boolean Expression
Boolean Expression
- double n1 78.0
- double n2 80.0
- n1 lt n2 __________
- n1 gt n2 __________
- (n1 35) gt n2 __________
- Math.Abs(n1-n2) lt 0.001 __________
- n1 n2 __________
- n1 ! n2 __________
16Compound Boolean Expression
Boolean Expression
- Logical Operators
- - AND
- - OR
- ! NOT
- Example (a20 b12)
- (a gt 12) (a lt 20)
- (a 2 0) (b 2 0)
17Precedence rules for Operators
Boolean Expression
- ( ) parentheses
- , / ,
-
- lt, gt, lt, gt
- , !
-
-
- If equal precedence, left to right
18Outline
- C Overview
- Boolean Expression
- Condition Statement
19Condition Statement
20if statement syntax
if statement
21Example if statement
if statement
22if statement syntax with multiple statements
if statement
- if (condition)
-
- statement1
- statement2
23ifelse statement syntax
if statement
- if (condition)
- statement1
- else
- statement2
24Example ifelse statement
if statement
25Nested IF
if statement
26Example Nested IF
if statement
27Summary
- C Overview
- Boolean Expression
- Condition Statement