Decision Control Statement

Decision Control Statement

C has three major decision making instructions— the if statement, the if-else statement, and the switch statement.

The if Statement

It is used to execute an instruction or sequence or block of instructions only if a conditioned is fulfilled.

The syntax is as follows:

if (condition)

statement;

Here condition is the expression that is to be evaluated. If this condition is true, statement is executed. If it is false, statement is ignored and the program continues on the next instruction after the conditional statement.

If we want more than one statement to be executed, then we can specify a block of statements within the curly bracets { }. The syntax is as follows:

if (condition)

{

block of statements;

}

 

If … else statement

 

If…else statement is used when a different sequence of instructions is to be executed depending on the logical value (True / False) of the condition evaluated. Its form used in conjunction with if and the syntax is as follows:

if (condition)

{

Statement _1;

}

else

{

Statement_ 2;

}

statement_3;

If the condition is true, then the Statements_1 executes; otherwise the Statements_2 will get executed. In both the cases, the control is then transferred to Statements_3 to follow sequential execution of the program.

Nested if…else statement

 

In nested if… else statement, an entire if…else construct is written within either the body of the if statement or the body of an else statement. The syntax is as follows:

if (condition_1)

{

if (condition_2)

{

Statements_1_Block;

}

else

{Statements_2_Block;

}

}

else

{

Statements_3_Block;

}

Statement_4_Block;

Here, condition_1 is evaluated. If it is false then Statements_3_Block is executed and is followed by the execution of Statements_4_Block, otherwise if condition_1 is true, then condition_2 is evaluated. Statements_1_Block is executed when condition_2 is true otherwise Statementss_2_Block is executed and then the control is transferred to Statements_4_Block.

Else if statement

 

To show a multi-way decision based on several conditions, we use the else if statement. This works by cascading of several comparisons. As soon as one of the conditions is true, the statement or block of statements following them is executed and no further comparisons are performed. The syntax is as follows:

if (condition_1)

{

Statements_1_Block;

}

else if (condition_2)

{

Statements_2_Block;

}

------------

else if (condition_n)

{

Statements_n_Block;

}

else

Statements_x;

Here, the conditions are evaluated in order from top to bottom. As soon as any condition evaluates to true, then the statement associated with the given condition is executed and control is transferred to Statements_x skipping the rest of the conditions following it. But if all conditions evaluate false, then the statement following final else is executed followed by the execution of Statements_x.

The Switch Statement

 

Its objective is to check several possible constant values for an expression, something similar to what we had studied in the earlier sections, with the linking of several if and else if statements. When the actions to be taken depending on the value of control variable, are large in number, then the use of control structure Nested if…else makes the program complex. There switch statement can be used. Its form is the following:

switch (expression){

case expression 1:

block of instructions 1

break;

case expression 2:

block of instructions 2

break;

.

.

default:

default block of instructions

}

It works in the following way: switch evaluates expression and checks if it is equivalent to expression1. If it is, it executes block of instructions 1 until it finds the break keyword, moment at finds the control will go to the end of the switch. If expression was not equal to expression 1 it will check whether expression is equivalent to expression 2. If it is, it will execute block of instructions 2 until it finds the break keyword.

Finally, if the value of expression has not matched any of the previously specified constants (you may specify as many case statements as values you want to check), the program will execute the instructions included in the default: section, if it exists, as it is an optional statement.

Related Post

Resignation Letter and Relieving Letter Resignation Letter Sample 1 Sender’s Name, Sender’s Address, Mobile Number, MailDate:ToThe Human Resource Department Company Name Company Addre...
Dart Introduction | Dart programming language Dart Introduction Dart is a scalable language that you can use to write simple scripts or full featured apps. Whether you’re creating a mobile app, w...
Drag and Drop Table View cell in IOS 11 & swi... Drag and Drop Table View cell in IOS 11 & swift 4
Variables in Java Variables in Java A variable is something which is associated with a value can be changed. For example: int i=10; Here variable name “i” wh...
Data Types in Java Data Types in Java Data types represent the different values to be stored in the variable, for example if a variable has int data type, it ...
Introduction to Java Programming Language JAVA TUTORIALS Introduction to Java Programming Language What is JAVA Java is programming language that produces software for multiple platforms, w...

Leave a Reply

Your email address will not be published. Required fields are marked *