Presentation is loading. Please wait.

Presentation is loading. Please wait.

The if Statement Format or No Condition satisfied? Yes

Similar presentations


Presentation on theme: "The if Statement Format or No Condition satisfied? Yes"— Presentation transcript:

1 The if Statement Format or No Condition satisfied? Yes
if ( condition ) program statement; or { program statements; } Condition satisfied? No Program statement Yes

2 The if-else Statement Format No Condition satisfied? Yes
if ( condition ) program statement 1; else program statement 2; Condition satisfied? No Program statement 1 Yes Program statement 2

3 The else if Statement Format Condition 1 satisfied? No
if ( condition1 ) program statement 1; else if (condition2) program statement 2; Condition 1 satisfied? No Condition 2 satisfied? Program statement 1 Yes No Program statement 2 Yes

4 Boolean Variables Declaration Value _Bool 0 representing false
1 representing true, mostly Can use any nonzero value representing true in C programming language!

5 Logical Operators Operator Example Description ||
Why Make a decision based on multiple conditions What are they Operator Example Description || x < 0 || x > width logical OR && x >= 0 && x <= width logical AND ! !(x < 0) Logical NOT

6 Logical OR 1 Returns false only if both expressions are false Example:
(4 > 5 || 6 <= 10) (4 <= 5 || 6 == 10) (4 >= 5 || 6 > 10) (4 < 5 || 6 != 10) A B A || B True False 1

7 A More Complicated Example
5: 10: 15: 20: 25: 30: 35: 40: 45: true false #include <stdio.h> int main() { int i; for (i=5; i<50; i+=5) if((i < 15) || (i >= 35)) printf("%i: true\n", i); else printf(“%i: false\n”, i); }//for return 0; }//main

8 Logical AND 1 Returns true only if both expressions are true Examples:
(4 > 5 && 6 <= 10) (4 <= 5 && 6 == 10) (4 >= 5 && 6 > 10) (4 < 5 && 6 != 10) A B A && B True False 1

9 A More Complicated Example
5: 10: 15: 20: 25: 30: 35: 40: 45: false true #include <stdio.h> int main() { int i; for (i=5; i<50; i+=5) if((i > 15) && (i <= 35)) printf("%i: true\n", i); else printf(“%i: false\n”, i); }//for return 0; }//main

10 Logical NOT Inverts the Boolean value of an expression A !A True False
Example: _Bool a = 0; if (!a) { printf(“a is a false value (0)\n”); } a = 1; if (a) { printf(“a is a true value (1)\n”); A !A True False

11 Conditions Relational expression with ==, !=, >, >=, <, <=
The value of “relational expression” is 1 if the relation is true, and 0 if false.  Can be combined with the operators (with decreasing precedence) ! (NOT) && (AND) || (OR) Rules: && and || are evaluated left to right Short circuiting – a little advanced Example c==' ' || c=='\t' || c=='\n' x < 75 && x > 101 && x != 101 x >= 35 && x < 7 || x == (x >= 35 && x < 7) || x == 10 (F && F) || T ≠ F && (F || T)

12 Evaluate simple expression
Objective: Evaluate expression and display results with input form Number operator number Example User input: Program output: 15

13 Program Need to check v2 == 0? #include <stdio.h> int main(void)
{ float v1, v2; char operator; printf("Type in your expression. \n"); scanf("%f %c %f", &v1, &operator, &v2); if ( operator == '+' ) printf("%.2f\n", v1 + v2); else if ( operator == '-' ) printf("%.2f\n", v1 - v2); else if ( operator == '*' ) printf("%.2f\n", v1 * v2); else if ( operator == '/' ) printf("%.2f\n", v1 / v2); return 0; } Need to check v2 == 0?

14 Corrections Change else if ( operator == '/' ) to
printf("%.2f\n", v1 / v2); to { if ( v2 == 0 ) printf(“Division by zero.\n”); else }

15 The switch Statement evaluate expression == value 1 statement 1 Y N
When to use The value of a variable successively compared against different values Format switch( expression ) { case value 1: program statement 1; break; case value 2: program statement 2; ׃ case value n: program statement n; default : program statement n+1; } == value 1 statement 1 Y N == value 2 Y statement 2 N == value n Y statement n N statement n+1

16 Example switch ( operator ) { case '+': printf("%.2f\n", v1 + v2);
break; case '-': printf("%.2f\n", v1 - v2); case '*': printf("%.2f\n", v1 * v2); case '/': if ( v2 == 0 ) printf(“Division by zero.\n”); else printf("%.2f\n", v1 / v2); default: printf("Unknown operator!\n"); }

17 Another example Month = 11 November switch (month) {
case 1: printf("January"); break; case 2: printf("February"); break; case 3: printf("March"); break; case 4: printf("April"); break; case 5: printf("May"); break; case 6: printf("June"); break; case 7: printf("July"); break; case 8: printf("August"); break; case 9: printf("September"); break; case 10: printf("October"); break; case 11: printf("November"); break; case 12: printf("December"); break; default: printf("Invalid month."); break; } Month = 7 July Month = 14 Invalid month

18 More on switch Statement
case value 1: program statement 1; case value 2: program statement 2; break; == value 1 statement 1 Y N == value 2 Y statement 2 N

19 One last example switch (month) { case 1: printf("January\n");
case 2: printf("February\n"); case 3: printf("March\n"); case 4: printf("April\n"); case 5: printf("May\n"); case 6: printf("June\n"); default: printf("Invalid month.\n"); } Month = 11 Ivalid month Month = 4 April May June Invalid Month

20 The conditional operator
Format: condition ? expression1 : expression2; Same as if ( condition ) { expression1; } else { expression2;

21 Conditional example //find the absolute value of a
abs = a < 0 ? -a : a;


Download ppt "The if Statement Format or No Condition satisfied? Yes"

Similar presentations


Ads by Google