IF-ELSE Statements

Views:
 
Category: Entertainment
     
 

Presentation Description

No description available.

Comments

Presentation Transcript

PowerPoint Presentation:

Conditional Structures

PowerPoint Presentation:

execute statements depending on some condition It is organized in such a way that there is always a condition or a comparison of two expressions that has to be evaluated first, which will decide the course of action of the program In C++, the condition will either evaluate to a boolean value true or false , or integer values 1 (for true) or a (for false) Conditional Structures …

PowerPoint Presentation:

Types of Conditional Structures

PowerPoint Presentation:

IF STATEMENT SWITCH-CASE STATEMENT

TYPES OF IF STATEMENTS::

TYPES OF IF STATEMENTS: Simple If Statement THE SIMPLE if-else STATEMENT THE NESTED if-else STATEMENT

(THE SIMPLE if STATEMENT):

( THE SIMPLE if STATEMENT) The syntax for the if statement is as follows : if (<expression>) <statement>; OR if (<expression>) { <statement1>; <statement2>; … < statementN >; }

PowerPoint Presentation:

The value of <expression> is evaluated first, if it results to a non-zero or true value, then <statement> is executed. If <expression> results to a zero or false value, then the program flow jumps to the next statement after the if structure.

THE SIMPLE if STATEMENT:

THE SIMPLE if STATEMENT There are some important things we must remember in using the if structure : The expression must always be enclosed within a pair of parentheses; forgetting the parentheses will result into a syntax error . If there is more than one statement that needs to be executed when the condition is non-zero or true, then these statements must be grouped in a pair of curly brackets . Do not place a semi-colon (;) after the <expression> for this will cause a logical error.

Conditional Structure (THE SIMPLE if-else STATEMENT) :

Conditional Structure (THE SIMPLE if-else STATEMENT) The syntax for the if-else statement is as follows: if (<expression>) <statement-1>; else <statement-2>; Like in the simple if statement, the value of <expression> is evaluated first, if it results to a non-zero or a true value, then <statement-1> is executed. Otherwise, if it is evaluated as zero or false, then the else part, i.e., <statement-2> is executed.

Conditional Structure (THE NESTED if-else STATEMENT) :

Conditional Structure (THE NESTED if-else STATEMENT) Since if-else statements are statements by themselves, they can actually be used as statement(s) inside an if-else statement. We will refer to this construction as nested if-else statements.

Conditional Structure (THE switch-case STATEMENT):

Conditional Structure (THE switch-case STATEMENT) The switch-case is a good alternative to cascading if-else statements. The syntax for the switch-case statement is as follows: switch(<expression>) { case <label-1> : <statement-1>; [break;] case <label-2> : <statement-2>; [break;] … case <label-n>: <statement-n>; [break] [default : <statement-d>; ] }

PowerPoint Presentation:

The expression may be an integer or character variable or, as the name suggests, an expression that evaluates to an integer or a character value the use of float and double data type values will result into an error. The <expression> is evaluated and the value compared is with each of the case labels . The case labels must have the same type as the <expression> and they must all be different. If a match is found between the selector and one of the case labels, say <label-1> , then the statement from <statement-1> will be executed. The same applies to other cases . The [break] statement is optional. If it is present, it will cause the program to “break” or “jump” out of the switch-case, and to execute the next statement following switch-case. If the break is not present, it will cause the program to execute the statement in the following case, i.e., <statement- 2> above, causing a waterfall effect , the same behavior applies to the other cases . If the value of the expression does not match with any of the case labels then the statement <statement-d> associated with [default] is executed. The [default] is optional but it should only be left out if it is certain that the expression will always take the value of one of the case labels . Note that the statement associated with a case label can be a single statement or a sequence of statements (without being enclosed in curly brackets).

authorStream Live Help