0% found this document useful (0 votes)
161 views12 pages

Unit Ii Selection Making Decisions:-: Decision Condition False True

The document discusses different types of control statements in C language that allow for decision making. It describes if, if-else, nested if-else, else-if ladder and switch statements. It provides the syntax and flowcharts to illustrate the logic of each statement. Examples are given to demonstrate how to use each statement to evaluate conditions and execute the corresponding code blocks. Key points about switch statement like expression type, break usage, order independence of cases are also mentioned.

Uploaded by

PRavi Prakash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
161 views12 pages

Unit Ii Selection Making Decisions:-: Decision Condition False True

The document discusses different types of control statements in C language that allow for decision making. It describes if, if-else, nested if-else, else-if ladder and switch statements. It provides the syntax and flowcharts to illustrate the logic of each statement. Examples are given to demonstrate how to use each statement to evaluate conditions and execute the corresponding code blocks. Key points about switch statement like expression type, break usage, order independence of cases are also mentioned.

Uploaded by

PRavi Prakash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

UNIT II

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

Flow chart of simple if control


Example:- Write a program to check whether the entered number is less than 10? If yes, display the same.
#include <stdio.h> #include <conio.h>
getch();
void main()
}
{
Output:int a;
Enter number3
clrscr();
The given number is less than 10
printf(\n Enter number);
scanf(%d,&a);
if(a<10)
printf(\n the given number is less
than 10);

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

Flowchart of if.else control


Example:- Write a program to find whether the given number is even or odd.

#include <stdio.h>#include <conio.h>


void main()
{
int n;
printf(\n Enter number);
scanf(%d,&n);
if(n%2==0)
{
printf( %d is an even number,n);
}

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

Flow chart of nested if..else statements

True

Statement - 1

Example:- Write a program to find Maximum of three numbers using nested if


//Example1
#include <stdio.h>
#include <conio.h>
void main()
{
int x,y,z,max;
clrscr();
printf(\n Enter three numbers);
scanf(%d %d %d,&x,&y,&z);
if(x>y)
{

//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

/* check the boolean condition */


if( a == 100 )
{
/* if condition is true then check the following */
if( b == 200 )
{
/* if condition is true then print the following */
printf("Value of a is 100 and b is 200\n" );
}
}
printf("Exact value of a is : %d\n", a );
printf("Exact value of b is : %d\n", b );
return 0;
Output:
Value of a is 100 and b is 200
Exact value of a is : 100
Exact value of b is : 200

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

Flow chart of else..if ladder

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

Points to be remembered when using switch control structure:


In switch control structure the expression can be an integer or a character value.
In switch control structure the order of cases is not important.
In switch statement every case must end with break statement.
In switch statement we can have cases with no executable statements even through those
cases are not having any executable statements. They are used for specific purposes.
Example:
main()
{
char ch;
printf(Enter a character a-z only);
scanf(%c,&ch);
switch(ch)
{
case a:
case e:
case i:
case o:
case u:printf(The Given character is vowel);
break;
default: printf(The character is constant\n);
}
}
5. In switch statement these can be some cases having integer type arguments, there can be some cases
having char type.
6. In switch statement if a statement is present, then it must belong to some particular case is the switch
otherwise that statement is not executed forever.
Example
main()
{
int n=1;
switch(n)
{
printf(Good Morning);//This is not executed forever it is not belongs to any case
case 1:printf(One\n);
case 2:printf(Two\n);
default: printf(Hello);
}
}
output: One Two Hello//if we place break at two cases output is One.
7.In switch statement we should not write conditions in the cases. But we can write conditions in the
expression.
8. In switch statement unlike if control structure the entire statements is a particular case are not written in cur
lee braces.{ }
9. Using switch case, we can write more structured programs than using if statement.

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();
}

You might also like