Decision making statement

Decision making statement

Introduction

Decision making statement is used for making decisions based on conditions. Depending on the condition the statement is executed.

If the condition is true, the statement is executed and if the condition is false then the statement will no execute. Decision making statement is almost used in all type of high-level programming. Such as Python, Java, JavaScript, Dart, and many others. In this section, we will discuss about decision making statements.

Decision making statement types

The if statement

The general form of if statement is as follows.

decision making statement - if

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.

decision making statement - if-else

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.

decision making statement - nested if

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.


Switch statement

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.

decision making statement - switch

Spread the love