Saturday, October 11, 2008

Learnings of the Week - October 11, 2008

CONDITIONAL STATEMENTS

Conditional statements 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

The If Statement


- the expression is relational or Boolean expression that evaluates to a TRUE (1) or FALSE (0) value

- statement may either be a single or a block of C statements

- if the expression evaluates to TRUE (1), the statement or the blockof statements that forms the target of the if statement will be executed. Otherwise, the program will ignore the statement or the block of statements


The If-Else Statement


- if and else are reserved words

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

- statement_1 and statemenr_2 may either be a single C statement or a block of C statements

- if the expression is TRUE (1), the statement or block of statement after the if statement will be
executed; otherwise, the statement or block of statement in the else statement will be executed

- only the code associated with the if or the code that is associated with the else executes, never both


The Nested-If Statement

- one of the most confusing aspects of the if statement in any programming language is nested ifs

- is an if statement that is the object of either an if or else

- this is sometimes referred to as “an if within an if”

- it can be difficult to know what else associates with what if

- C provides a very simple rule for resolving this type of situation

- in C, the else is linked to the closest preceding if that does not already have an else statement associated with it


The If-Else-If Ladder

- a common programming construct in C

- are reserved words in C

- the expression are evaluated from the top downward

- as soon as a true condition is found, the statement associated with it is executed and the rest of the ladder will not be executed

- if none of the condition is true, the final else is executed

- the final else acts as a defaults condition

- if all other conditions are false, the last else statement is performed

- if the final else is not present, then no action takes place

- note that the final else is optional, you may include this part if needed in the program or you may not include if not needed


The Switch Statement

- is a multiple-branch decision statement

- a variable is successively tested against a list or integer or character constants

- if a match is found, a statement or block of statement is executed

- the default part of the switch is executed if no matches are found

- according to Herbert Schildt, there are three important things to know about switch statements:

1. The switch differs from if statements in such a way that switch can only test for equality whereas if can evaluate a relational or logical expression

2. No two case constants in the same switch can have identical values. Of course, a switch statement enclosed by an outer switch may have case constant that are the same.

3. If character constants are used in the switch, they are automatically converted to their integer values.

- Note that the break statement is used to terminate the statement associated with each case constant. It is a C keyword which means that at that point of execution, you should jump to the end of the switch statement by the symbol }.


The Nested Switch Statement

Posted by:

RAE ANGELINE S. PALEN



0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home