Module3 Condition Statement - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Module3 Condition Statement

Description:

In Main() method. Variable & Constant Declaration. Statements ... const data type name = value ; Example: const int radius = 15; const double area=1.5; ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 28
Provided by: APJ9
Category:

less

Transcript and Presenter's Notes

Title: Module3 Condition Statement


1
Module3 Condition Statement
???????? ???????????Aphirak Jansang aphirak.j_at_ku.
ac.th http//www.cpe.ku.ac.th/aphirak Computer
Engineering Department Kasetsart University,
Bangkok THAILAND
2
Outline
  • C Overview Again
  • Boolean Expression
  • Condition Statement

3
Simple C Structure
C Structure
  • Namespace
  • Class
  • Main()

Variable Constant Location
Statements
4
In Main() method
Main() method
  • Variable Constant Declaration
  • Statements

5
C 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
6
C 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
7
C Program Example
Variable Constant
8
In Main() method
  • Variable Constant Declaration
  • Statements

9
C Statements
Statement
  • Assignment Statement
  • Ex. x 10
  • Input Statement
  • Ex. st Console.ReadLine()
  • Output Statement
  • Ex. Console.WriteLine(Hello World!)

10
Simple C Program
C Overview
11
C Program with Condition
C Overview
12
Outline
  • C Overview
  • Boolean Expression
  • Condition Statement

13
Boolean Expression
Boolean Expression
  • Operators

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?
14
Example 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 ___________

15
Example 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 __________

16
Compound Boolean Expression
Boolean Expression
  • Logical Operators
  • - AND
  • - OR
  • ! NOT
  • Example (a20 b12)
  • (a gt 12) (a lt 20)
  • (a 2 0) (b 2 0)

17
Precedence rules for Operators
Boolean Expression
  1. ( ) parentheses
  2. , / ,
  3. lt, gt, lt, gt
  4. , !
  5. If equal precedence, left to right

18
Outline
  • C Overview
  • Boolean Expression
  • Condition Statement

19
Condition Statement
  • if...else
  • switchcase

20
if statement syntax
if statement
  • if (condition)
  • statement

21
Example if statement
if statement
22
if statement syntax with multiple statements
if statement
  • if (condition)
  • statement1
  • statement2

23
ifelse statement syntax
if statement
  • if (condition)
  • statement1
  • else
  • statement2

24
Example ifelse statement
if statement
25
Nested IF
if statement
26
Example Nested IF
if statement
27
Summary
  • C Overview
  • Boolean Expression
  • Condition Statement
Write a Comment
User Comments (0)
About PowerShow.com