Presentation is loading. Please wait.

Presentation is loading. Please wait.

Nested if statements When one if/else structure is contained inside another if/else structure is called a nested if/else. if (grade > 60) if (grade > 70)

Similar presentations


Presentation on theme: "Nested if statements When one if/else structure is contained inside another if/else structure is called a nested if/else. if (grade > 60) if (grade > 70)"— Presentation transcript:

1 nested if statements When one if/else structure is contained inside another if/else structure is called a nested if/else. if (grade > 60) if (grade > 70) printf(“You passed”); else printf(“You passed but need a tutor”); printf(“You failed”);

2 else if Usually you try to nest within the else statement. Note the indentation. if (grade > 70) printf(“You passed”); else if (grade > 60) printf(“You passed but need a tutor”); else printf(“You failed”);

3 && - Logical AND (section 4.10)
if ((total > 50) && (status == 0)) printf(“Your shipping will be free\n”); When using the and operator (&&), both expressions must be true for the compound statement to be true. truth table

4 || - Logical OR (section 4.10)
if ((total > 50) || (status == 0)) printf(“Your shipping will be free\n”); When using the or operator (||), at least one expression must be true for the compound statement to be true. truth table

5 more on && and || Can be used to reduce nesting
&& has higher precedence evaluation stops once truth or falsehood is known - in the example below, if semesterAverage > 90 finalExam will not get tested if ((semesterAverage > 90) || (finalExam > 90)) printf(“Student gets an a\n”);

6 logical negation ! (section 4.10)
if !(grade == goal) printf(“You missed your goal\n”); ! has high precedence so you must use parenthesis In most cases you can avoid using the logical negation by expressing the condition differently with an appropriate relational operator. Note: its a unary operator

7 #define The #define preprocessor directive creates symbolic constants (it has another use we may talk about later) #define PI Would replace all subsequent occurrences of the symbolic constant PI with the numeric constant , in the code

8 #define (cont’d) Used to avoid “magic numbers” in code
Unlike variables, you should not change the value of a symbolic constant Should be used before your variable declarations Should be in ALL CAPS, except if you use Hungarian Notation e.g. #define iMY_CONSTANT 3600

9 Avoid magic numbers! Magic numbers are actual numbers you use throughout your code e.g. your divisor of 10000 If you need to change them, at worst it takes time to find all the occurrences, at worst you may miss a few If you use #define, you only need to change the value once at the top of your code E.g #define f_ACCEPTABLE_THRESHOLD

10 reading We have already covered For next class chapter 1 chapter 2
sections section 4.10, 4.11 section 13.3 For next class 4.7


Download ppt "Nested if statements When one if/else structure is contained inside another if/else structure is called a nested if/else. if (grade > 60) if (grade > 70)"

Similar presentations


Ads by Google