Vous êtes sur la page 1sur 16

Nested If Statements

Start

Read Students Answer

Put (check) Right mark and add 1 point to score

Is it Right or Wrong?

Wrong

Put X (cross) mark and dont add anything to score

End

Recap- If else Statements


The general form of the if statement is:
if (expression) statement; else statement;

where statement may be either a single statement or a block of statements. The else clause is optional.

Recap- If else Statements


The general form of the if with blocks of statement is:
if(expression) {

statement sequence
} else {

statement sequence
}

Recap- If else Statements


If the expression is true, the statement or block that forms the target of the if is executed; otherwise, the statement or block that is the target of the else is executed. Remember, only the code associated with the if or the code associated with the else executes, never both.

Put (check) Right mark and add 5 1 point to score

Is it Start Right or Wrong? Read Students Answer

Wrong

Is it Put X (cross) close to mark and dont the add correct to anything score answer ?

Put (check) Right mark and add 1 point to score

End Is it Right or Wrong?

Wrong

Give 1-4 points depending on Put X (cross) the answer mark and dont add anything to score Put X (cross) mark and dont add anything to score

End

If else Statements
#include <stdio.h>

main(void) { int magic = 50; int guess; printf("Guess what 2-digit number is stored: "); scanf("%d",&guess);
if(guess == magic) printf("Good Guess!"); else printf("Sorry, try again!"); return 0; }

If else Statements
Start
#include <stdio.h>

main(void) { int magic = 50; int guess;

Get guessed number

printf("Guess what 2-digit number is stored: "); Is it = Display No Yes scanf("%d",&guess); Display Good

Guess!

if(guess == magic) printf("Good Guess!"); else printf("Sorry, try again!"); return 0; }

to 50? Y/N

Sorry, Try again!

End

Nested If Statements
One of the most confusing aspects of if statements in any programming language is nested ifs. A nested if is an if statement that is the object of either an if or else. The reason that nested ifs are so troublesome is that it can be difficult to know what else associates with what if.

Nested If Statements
For example: If(x) If(y) print (1); else print (2); To which if does the else refer?

Nested If Statements
In C, the else is linked to the closes preceding if that does not already have an else statement associated with it. In the previous case, the else is associated with the if(y) statement. To make the else associate with If(x) you must use braces to override its normal association:
If(x) { if(y) printf(1); } else printf(2);

Nested If Statements
else is now associated with the if(x) because it is no longer part of the if(y) object block. Because of Cs scope rules, the else now has no knowledge of the statement because they are no longer
If(x) { if(y) printf(1); } else printf(2);

Nested If Statements
#include <stdio.h>

main(void) { int magic = 50; int guess; printf("Guess what 2-digit number is stored: "); scanf("%d",&guess);
if(guess == magic) printf("Good Guess!"); else { printf("Sorry, try again!"); printf("Sorry, Wrong Guess, Please try again!"); if(guess > magic) printf("Try going lower"); return 0; else printf("Try going higher"); } return 0;

} }

Start Nested If Statements


#include <stdio.h>

Is it = No Display Sorry, Display Good Yes to 50? Try again! Guess! printf("Guess what 2-digit number is stored: "); Y/N
scanf("%d",&guess);

main(void) { int magic = 50; int guess;

Get guessed number

Is it < or > 50?

if(guess == magic) printf("Good Guess!"); else { Display Try printf("Sorry, Wrong Guess, Please try again!"); if(guess > magic) printf("Try going lower"); going Lower! End else printf("Try going higher"); } return 0; }

Display Try going Higher!

Thank you!

Vous aimerez peut-être aussi