Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 st Semester 2005 1 Module3 Condition Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.

Similar presentations


Presentation on theme: "1 st Semester 2005 1 Module3 Condition Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering."— Presentation transcript:

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/39 Outline  C# Overview Again  Boolean Expression  Condition Statement  More Example

3 1 st Semester 2005 3/39 Simple C# Structure  Namespace Class  Main()Namespace Class Main() Variable & Constant Location Statements C# Structure

4 1 st Semester 2005 4/39 C# Program Example Variable & Constant

5 1 st Semester 2005 5/39 C# Statements  Assignment Statement Ex. x = 10;  Input Statement Ex. st = Console.ReadLine();  Output Statement Ex. Console.WriteLine(”Hello World!”);Statement

6 1 st Semester 2005 6/39 More C# Operators  ++  ++ (Increment its operand by 1) y++; // y = y+1; ++y; // y = y+1;  --  -- (Decrease its operand by 1) y--; // y = y-1; --y; // y = y-1;  +=, -=, /=, %=, *= y += 50; // y = y+50; y /= 2; // y = y/2; y -= 7; // y = y-7;

7 1 st Semester 2005 7/39 Flowchart Symbols  Graphical representation Terminator Process Input/output Condition Connector Flow line

8 1 st Semester 2005 8/39 Simple C# Program Start statement1 statement2 statement3 statement4 End C# Overview Quote from The Matrix Revolutions “EVERYTHING THAT HAS A BEGINNING, HAS AN END”

9 1 st Semester 2005 9/39 C# Program with Condition Start statement1 statement2 statement3statement4 End Condition true false C# Overview

10 1 st Semester 2005 10/39 Outline  C# Overview  Boolean Expression  Condition Statement  More Example

11 1 st Semester 2005 11/39 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

12 1 st Semester 2005 12/39 Example: Boolean Expression double x = 4.0; Expression Value x < 5.0 ___________ x > 5.0 ___________ x <= 5.0 ___________ 5.0 == x ___________ x != 5.0 ___________ Boolean Expression

13 1 st Semester 2005 13/39 Example: More Boolean Expression double n1 = 78.0; double n2 = 80.0; n1 < n2 __________ n1 >= n2 __________ (n1 + 35) > n2 __________ Math.Abs(n1-n2) <= 0.001 __________ n1 == n2 __________ n1 != n2 __________ Boolean Expression

14 1 st Semester 2005 14/39 Compound Boolean Expression  Logical Operators && - AND && - AND || - OR || - OR ! – NOT ! – NOT  Example (a=20; b=12) (a > 12) && (a 12) && (a < 20) !(’F’ != ’M’) || (19 > 12) && (19 12) && (19 < 18) Boolean Expression

15 1 st Semester 2005 15/39 Precedence rules for Operators 1.( ) parentheses 2.*, /, % 3.+ – 4., = 5.==, != 6.! 7.&& 8. ││ 9.If equal precedence, left to right Boolean Expression

16 1 st Semester 2005 16/39 Break 5 minutes “You will know (the good from the bad) when you are calm, at peace. Passive...” A Jedi uses the Force for Knowledge and Defense, never for attack.

17 1 st Semester 2005 17/39 Outline  C# Overview  Boolean Expression  Condition Statement  More Example

18 1 st Semester 2005 18/39 Condition Statement  if...else  switch…case

19 1 st Semester 2005 19/39 if statement syntax condition if (condition) statement; condition Statement; true false if statement

20 1 st Semester 2005 20/39 Example: if statement if statement i % 2 == 1 Ood i % 2 == 0 Even false false true true

21 1 st Semester 2005 21/39 if statement syntax with multiple statements condition if (condition){ statement1; statement2;} condition Statement1; true false Statement2; if statement

22 1 st Semester 2005 22/39 if…else… statement syntax condition if (condition) statement1; else statement2; condition Statement2; truefalse Statement1; if statement

23 1 st Semester 2005 23/39 Example: if…else… statement i % 2 == 1 Ood Even true false if statement

24 1 st Semester 2005 24/39 Nested IF condtion1 statement2b true false condtion2 condtion3 statement2a statement3a true true false false if statement

25 1 st Semester 2005 25/39 Nested IF Example condtion1 true false statement1statement2 statement2b condtion2 statement2a true false condtion3 statement3a true false

26 1 st Semester 2005 26/39 Example: Nested IF if statement

27 1 st Semester 2005 27/39 Condition Statement  if...else  switch…case

28 1 st Semester 2005 28/39 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

29 1 st Semester 2005 29/39 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;

30 1 st Semester 2005 30/39 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; }

31 1 st Semester 2005 31/39 switch-case  Syntax:  must be int, char, string switch ( ) { case : ; break; default: ; break; }

32 1 st Semester 2005 32/39 Example: switch-case int day_num; string day_name; 1Sun 2Monday 3Tuesday 4Wednesday 5Thursday 6Friday 7Saturday How to write ”IF Statement” and ”Switch-case statement”?

33 1 st Semester 2005 33/39 Break 3 Minutes “With great power comes with great responsibility” *From Spiderman

34 1 st Semester 2005 34/39 Outline  C# Overview  Boolean Expression  Condition Statement  More Example

35 1 st Semester 2005 35/39 Example1 Write a program which, given 1 exam mark (between 0 - 100) determines the corresponding result, given the following :- ScoreGrade 100 - 80A 79- 70B 69 - 60C 59 - 50D 49 - 0F

36 1 st Semester 2005 36/39 Example2 Ex1 Please input month: 5 Your month has 31 days. Input: number of month Output: #day in that month Ex2 Please input month: 2 Your month has 28 days.

37 1 st Semester 2005 37/39 Example3 Ex1 Please input month: 5 May has 31 days. Input: number of month Output: #day in that month Ex2 Please input month: 2 Feburuary month has 28 days.

38 1 st Semester 2005 38/39 Example4  Calculate payment for Air-time usage Operator: O-LEANG Input: Promotion Type & Usage time Output: Payment price Example 4 Program Promotion Type Usage time Payment price

39 1 st Semester 2005 39/39 Summary  C# Overview  Boolean Expression  Condition Statements if...else... Statement switch-case Statement if…else… Condition Problems switch


Download ppt "1 st Semester 2005 1 Module3 Condition Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering."

Similar presentations


Ads by Google