Decision making statement is used for making decisions based on conditions. Depending on the condition the statement is executed.
Decision making statement types
The if statement
The general form of if statement is as follows.
The test expression should always be enclosed in parentheses. If the test_expression is non zero then, the statement statement1 is executed otherwise statemnent1 is skipped and control passes to the next statement. The statement1 can be a single or a compound statement. The statement 1 part of the if statement is the body of the if statement.
The if-else statement
The if else statement is constructed to form some action even when the test expression is not true. Looking at if statement it does nothing when the statement is false. The if else statement executes one if the statement is true and another if the statement is false.
The general form of if else is as follows.
When the test expression test_expression is true then the body of if is executed and the control passes to the next statement immediately following the if-else construct. But if the test expression test_expression is not true then the body of the else is executed.
Nested if-else statement
The actual meaning of nesting is to have the same construct with itself. The way of having multiple if or if-else conditions is called nested conditional statement. The general format of the nested if-else statement as follows.
The statements statement1 or statement2 or other statements can be compound statements id a group of statement is to be executed. Furthermore, these statements can be other conditional statements and there is no limit on the depth of nesting.
A switch statement allows a variable to be tested for equality against a list of values. The switch statement is also called the constant multiway conditional statement. When each of the test in a multiway conditional if statement checks for different values of the same expression the switch statement is used.
The format of switch statement as follows.