Decision Structures
Decision structure statements help us to jump from one part of the program to another part of program based on result of some conditions. Sometimes decision structure statements are also known as selective structures statements, branching statements or decision making statements.
C language provides two basic types of decision structure statements: if and switch.
The if Statement
The if statement is one of the powerful decision making statement which can be used to transfer control of instruction execution. The if statement can be used in following different ways:
Simple if statement
if-else statement
nested if statement and
else-if ladder statement
Simple if statement :-
The simplest form of the decision structure statement is the if statement. It is frequently used in the program for decision making and allowing us to change the flow of program execution
Nested if...else statement
There are situations where we need to execute series of decisions in our program. We may use series of if..else statement in nested form to achieve our task. Consider the following case where one if..else statement block is used inside another if..else block.
if (test expression-1)
{
if(test expression-2)
{
Statement block-1;
else
Statement block-2;
The else-if ladder statement
There is another way of using many if statements together when we want to evaluate multiple decisions. When chain of if statements are used in else (false) block of nested if statements, it becomes else if ladder statement.
The Switch Statement
We have used the if...else statement for selection of various statement blocks during program execution. The program becomes difficult to read and interpret the logic. To simplify our program, the C language has offered the built-in facility of multiway decision statement called switch.
Syntax of the Switch Statement
switch (expression)
case value-1:
statement block-1
break; case value-2:
statement block-2 break;
...
case value-n:
statement block-n
break; default:
default statement block break;
statement-x;
Choose the correct option from the following:
(1) What is the value of variable flag after the execution of following if statement ?
int flag=0;
if(5 <8) { flag=1; }
(a) O
(b) 1
(c) 5
(d) 8
(2) What is the value of variable 's' after the execution of following switch statement ?
X = 3;
switch ( x ) {
case 1: s = 'A'; break;
case 2: s = 'B'; break;
case 3: s = 'C'; break;
default: s = 'D'; break;
(a) A
(b) B
(c) C
(d) D
(3) What is the value of variable sum after the execution of following statements ?
int number1=5, number2=10, sum=0;
if (number1 > number2)
sum = sum + numberl;
else
sum = sum + number2;
(a) O
(b) 5
(c) 10
(d) 12
(4) What will be the output when following program segment is executed ?
int numberl=10, number2=20; if ((number1+number2) > 35 || (number1 > number2))
printf("%d", numberl);
else
printf("%d", number2);
(a) 10
(b) 20
(c) 35
(d) Error
(5) What will be the output when following program segment is executed ?
char chr='A';
switch (chr)
case 'A': printf("A"); break; case 'B': printf("B"); break; case 'C': printf("C"); break;
(a) A
(b) B
(c) C
(d) Error