Unit Ii Selection Making Decisions:-: Decision Condition False True
Unit Ii Selection Making Decisions:-: Decision Condition False True
SELECTION MAKING DECISIONS:C language possesses decision making capability and supports the following statements known as control or
decision making statements.
1. if statement. 2. If else statement 3.if else if statement 4. switch statement.
5. Un Conditional goto statement. 6.loop Statement
These statements are classified into Two-way selection and Multi-way selection.
Two way selection:A two way selection chooses a decision among two alternatives. Here we use a statement whose answer can be
either true or false. If the answer is true, an action is performed and if the answer is false, a different action is
performed.
false
Decision
condition
true
True action
False action
Decision making with if statement:The if statement is a powerful decision making statement and is used to control the flow of execution of
statements. It is a two way decision statement and is used along with an expression. It takes the following form.
if(test expression)
The test expression is evaluated first and if the value of the expression is true (non zero) or false (zero), it
transfers the control to a particular statement.
Entry
test
expression
?
True
The if statement may be implemented in different forms depending on the complexity of conditions to be tested.
The different forms are:
1. Simple if statement.
2. if else statement.
3. Nested if..else statement.
4. else if ladder.
1. Simple if statement:The general form of a simple if statement is
if(test expression)
{
Statement- block;
}
Statement-x;
The statement-block may be a single statement (or) group of statements. If the test expression is true, the
statement-block will be executed; otherwise the statement-block will be skipped and the execution will jump
to the statement-x;
Entry
test
expression?
True
Statement block
False
Statement - X
Next statement
2. ifelse statement:The if..else statement is an extension of the simple if statement. The general form is
if(test expression)
{
True block statements;
}
else
{
False block statements;
}
statement-x;
If the test expression is true, then the true block statements are executed, otherwise the false block
statements are executed. In either case, either true block or false block will be executed.
Entry
True
test
expression?
False
True-block
statement
False-block
statement
S tatement
Statement - X
else
{
printf(%d is an odd number,n);
}
getch();
}
Output:Enter number 6
6 is an even number.
3. Nested if..else:When a series of decision are involved, we may have to use more than one ifelse statement in nested
form as shown below:
if(test condition1)
{
if(test condition2)
{
statement-1;
}
else
{
statement-2;
}
}
else
{
statement-3;
}
statement-x;
If the test condition-1 is false, the statement-3 will be executed, otherwise it continues to perform the second
test. If the condition-2 is true, the statement-1 will be evaluated; otherwise the statement-2 will be evaluated
and then the control is transferred to the statement-x.
Entry
False
True
test
expression 1
?
False
Statement - 3
Statement - 2
S tatement
S tatement
test
expression 2
?
Statement - X
Next Statement
X
True
Statement - 1
//Example2
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 100;
int b = 200;
if(x>z)
{
max=x;
}
else
{
max=z;
}
}
else
{
if(y>z)
{
max=y;
}
else
{
max=z;
}
}
printf(\n Maximum number is %d,max);
getch();
}
Output:Enter three numbers5
7
3
Maximum number is 7
Multiway selection:Multiway selection chooses among several alternatives. The logic for multiway selection is as shown below.
Multiway
expression
Value 1
action
Value 2
action
Value 3
action
Value 4
action
else if ladder:else-if is used when multipath decisions are involved. A multipath decision is a chain of ifs in which the
statement associated with each else is an if. It takes the following general form:
if(condition-1)
{
statement-1;
}
else if(condition-2)
{
statement-2;
}
.
.
.
else if(condition-n)
{
statement-n;
}
else
{
default-statement;
}
statement-x;
The conditions are evaluated from top to bottom. As soon as true condition is found, the statement
associated with it is executed and the control is transferred to statement X.
When all else conditions become false, then the final else containing the default-statement will be executed.
Entry
True
False
Condition
-1
True
Condition
-2
Statement - 1
False
S tatement
True
Statement - 2
Condition
-3
False
S tatement
True
Statement - 3
Condition
-n
False
S tatement
Statement - n
S tatement
Statement X
Next
Statement X
Default
Stateme
nt
Example:- Write a program to read three integers and display the largest of three numbers using else if
ladder
#include <stdio.h>#include <conio.h>
void main()
{
int a,b,c,max;
clrscr();
printf(\n Enter three numbers);
scanf(%d %d %d,&a,&b,&c);
if((a>b)&&(a>c))
{
max=a;
}
else if((b>a)&&(b>c))
{
max=b;
}
else
{
max=c;
}
printf(\nThe largest number
is%d,max);
getch();
}
Output:Enter three
numbers6
3
7
The largest number
is 7
SWITCH STATEMENT:
This switch control structure is used
for making multi-way decision or
multipath decision.
Syntax:
switch(expression){
case value1: statements;
break;
case value2: statements;
break;
.
.
.
case value n: statements;
break;
default: statements;
break; // optional.
}
1. The switch control structure is having more readability than using if control structures.
2. The switch control structure is used mainly in menu driven programs.
3. In the switch control structures if the given expression matches with value 1 then the statements in
that case are executed and control comes to the first statement after the switch control structure due to
the presence of break statement.
4. If the given expression does not matches with value1 then it goes for checking value 2 and ... value
n.
5. If no switch case is matched with the given expression then automatically statements in the default
case are executed and control comes out of the switch case.
6. The expression must be an integer expression or character expression which yields either integer value
or char value.
Ex1:Write a program to read a
Ex3:Write a program to read a
number and print its corresponding
character and prints ovel or not
week day.
main()//INPUT IS CHARACTER
main()//INPUT IS INTEGER
{
{
char ch;
int day;
printf(Enter alphabet:\n);
printf(Enter number of week day:\n);
scanf(%c,&ch);
scanf(%d,&day);
switch(ch)
switch(day)
{
{
case a: printf(ovel);break;
case 1: printf(MONDAY); break;
case e: printf(ovel);break;
case 2: printf(TUESDAY);break;
case i: printf(ovel);break;
case 3: printf(WEDNESDAY);break;
case o: printf(ovel)break;
case 4: printf(THURSDAY);break;
case u: printf(ovel);break;
case 5: printf(FRIDAY);break;
default: printf(consonent\n);
case 6: printf(SATURDAY);break;
}
case 7: printf(SUNDAY);break;
}output:Enter alphabet:z
default: printf(Invalid Week day\n);
consonent
}
}output: Enter number of the week day 3
WEDNESDAY
Example:1. Write a program to find all simple mathematical calculations using switch.
#include <stdio.h>
void main()
{
int x,y,z,ch;
printf(\nEnter x,y values);
scanf(%d %d,&x,&y);
printf(1.Addition\n
2.Subtraction
3.Multiplication \n 4.Division);
printf(\n Select a choice);
scanf(%d,&ch);
switch(ch)
{
case 1:
z=x+y;
printf(\n sum is %d,z);
break;
case 2:
z=x-y;
printf(\n difference is %d,z);
break;
case 3:
z=x*y;
printf(\n product is %d,z);
\n break;
case 4:
z=x/y;
printf(\n quotient is %d,z);
break;
default:
printf(Invalid choice);
}//end of switch
}//end of main
goto statement:The goto statement is used to alter the program execution sequence by transferring the control to some other
part of the program.
The general syntax of the goto statement is:
goto label;
where label is a valid C identifier used to label the destination such that control could be transferred. The label
must be followed by a colon and can be placed before or after the goto statement in a program.
If the label: is before the goto statement, a loop is formed and some statements will be executed repeatedly.
Such jump is called Backward jump.
label:
statement;
---------------------------------------goto label;
If the label: is after the goto statement, some statements will be skipped and the jump is called Forward jump.
goto label;
---------------------------------------label:
statement;
There are another ways of using the goto statement in a program. They are
1. conditional goto.
2. unconditional goto.
1. conditional goto:The conditional goto is used to transfer the control of the execution from one part of the program to the
other in certain conditional case.
Example:Write a program to find largest of two numbers using conditional goto statement.
SOURCE CODE:#include <stdio.h>
#include <conio.h>
void main()
{
int x,y;
clrscr();
printf(\n Enter two numbers);
scanf(%d %d,&x,&y);
if(x>y)
goto output1;
else
goto output2;
output1:
printf(\nlargest number is %d,x);
goto stop;
output2:
printf(\n largest number is %d,y);
stop:
getch();
}
2. unconditional goto:The unconditional goto statement is used just to transfer the control from one part of the program to the
other part without checking any condition. The use of unconditional goto statement is shown in the
following program.
Example:Write a program to illustrate unconditional goto.
SOURCE CODE:#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
start:
printf(\nPVP);
goto start;
getch();
}