Friday, October 10, 2008

Submitted by:

Joyce Niko D. Perez

IV- RIZAL

CONDITIONAL STATEMENTS

Conditional Statements

l Are statements that check an expression then may or may not execute a statement or group of statement depending on the result of the condition.

Types of Conditional Statements:

l The If Statement

Expression is relational or Boolean expression that evaluates to a TRUE (1) or False (0) value.

l The If-Else Statement

The general form of the if-else statement is:

If (expression)

statement_1;

else

statement_2;

l The Nested-If Statement

l One of the most confusing aspects of the if statement in any programming language is nested ifs. A nested if is an if statement that is the object of either an if or else.

l This is sometimes referred to as “an if within an if.”

l The reason that nested ifs are so confusing is that it can be difficult to know what else associates with what if.

l The If-Else-If Ladder

l A common programming construct in C is the if-else- if ladder.

l The general form of the if-else-if ladder statement is:

if ( expression_1)

statement_1;

else if (expression_2)

statement_2;

else if (expression_3;

statement_3;

:

:

else

statement_else;

l The Switch Statement

l The switch statement is a multiple-branch decision statement.

l The general form of the switch statement is:

switch (variable)

{

case constant1:

statement sequence_1;

break;

case constant2:

statement_sequence_2;

break;

:

:

default:

statement_sequence_default;

}

l The Nested Switch Statement

l The general form of the nested switch statement is:

switch (variable)

{

case constant:{

switch (variable)

{

case constant1:

statement sequence_1;

break;

case constant2:

statement_sequence_2;

break;

}

break;

}

case constant2:

statement sequence;

break;

default:

statement sequence;

}

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home