Conditional Statements
Program control statements modify the order of statement execution. Statements in a C program normally executes from top to bottom, in the order that they appear in the source code. These control statements decide whether to perform a particular block of statements. These control statements decide whether to perform a particular block of statements.
The if statement if (condition) { statements; } If expression evaluates to true, the block of statements are executed, if false then the block of statements are not executed. In either case, execution then passes to whatever code follows the if statement.
The if-else statement if (condition) { statement1; } else { statement2; } If the conditional expression evaluates to true, statement1 is executed, if false then statement2 is executed
The if else if statement if (condition) { statement1; } else if (condition) { statement2; } else if (condition) { statement3; } else { statement4; }
If the first conditional expression evaluates to true, the statement 1 shall be executed, however if it is not true then the next conditional expression shall be tested for statement 2 to be executed and so on. If no conditional expression has been satisfied, then the statement after the last else shall be executed
The switch statement switch(variable) { case constant1: statement; break; case constant2: statement; break; default: statement; }
When a match is found, the statements associated with that case are executed until a break is reached. A break will cause immediate termination from the body of the switch. If there is no match with the variable and the cases, the default statement shall be executed. The default is optional and if it is not present, no action takes place if all matched fail.