Introduction to Selection Statement
In most of the programs, the program statements were executed in the same order in which they are written. Each instruction was executed only once. This is not enough in programming. Sometimes we may have to execute program statements based on the given condition, sometimes we may have to execute program statements repeatedly, sometime we may have to choose an option and perform the task accordingly. To carry out all these tasks and other similar tasks, program statements must be executed in a controlled way and it can be done using control structure.Control structure is also known as control statement. It is defined as the group of statements or function that helps to control the flow of instructions in a program. It allows programmer to control the flow of program statements execution in a program. It also specifies the order of statements in program. If there are no control statements, the statements are executed in sequential manner, commonly known as sequential statements. Mainly controls structures are classified into the following three categories: - Branching Statements(Selective) - Looping Statements(Iterative or Repetitive) - Jumping Statements(Unconditional)
Branching Statements
In branching control structure, selection is made on the basis of condition. We have options to go when the given condition is true or false. The flow of program statement execution is totally directed by the result obtained from the checking condition. Hence, program statements using selective control structures are also called conditional Statements. This type of control structure is mainly used for decision making. It can mainly be categorized into two types. - Conditional Branching Statement - Switch Case StatementConditional Statement
It is the most common decision making control structure which controls the flow of program statement execution based on the condition checked. It can be used in different forms as: - if statement - if else statement - if else if statement (if-else ladder) - nested if else statementif statement
It is the simplest form of conditional statement in which statements are executed if the test expression (condition) is true. When the condition is false there is no option to go within this structure; in such a situation control must out from the structure and statements outside this structure will be executed.Syntax for if statement
if(condition) { statements; } Braces are optional for only one statement as shown below: if(condition) statements;Flowchart for if statement
if else statement
It is another form of selective control structure which can handle both expected as well as unexpected situation. In this control structure, statements written in the body part of if are executed if the condition is true otherwise statements written in body part of else are executed. This is appropriate when we must check only one condition.Syntax of if else statement:
if(condition) statement1; else statement2; In this case if the condition is true then statement1 is executed otherwise statement2 is executed. The else portion of the if-else statement is optional.Flowchart of if else statement:
if else if statement (if-else ladder)
When we have two or more conditions to be checked in a series, we can use if-else if statement. It is also known as multiple conditional statement / multipath conditional statement / if-else ladder.Syntax of if-else if statement
if(condition1) statement1; else if(condition) statement 2; ............... ............... else if(condition n-1) statement n-1; else statement n; In this statement, conditions are checked from the top of the ladder to downwards. As soon as the true condition is found, the statement associated with it are executed and the control is transferred to outside of the ladder skipping the remaining part of the ladder. When most of the conditions (condition1 to condition n-1) are evaluated false then statements associated with the else part will be executed.Nested if else statement
An entire if else statement written within the body of if part or else part of another if else statement is called nested if else statement. It is used when a condition is to be checked inside another condition at a time in the same program to make a decision.Syntax of Nested if else statement:
if(condition1) { if(condition2) statement1; else statement2; } else statement3;Switch Case Statement
C switch case statement is a multipath decision-making statement that allows selection and execution of a particular block of statements from several blocks of statement based upon the value of expression which is included within the switch statement and branches accordingly . The expression must be of an integral value or character constant. The same task can be performed using an if-else ladder as well but as the number of alternatives increases, the selection process becomes more complex (more time consuming).The main difference between if else ladder and switch statement is - In if-else ladder selection of appropriate option is made in serial fashion whereas in switch-case statement it is done in parallel fashion. So switch statement is much faster than if-else ladder.
The switch statement body consists of a series of case labels and an optional default label. The default label can appear only once and anywhere in the body of the switch statement. Syntax of switch statement switch(expression) { case constant 1: statement 1; break; case constant 2: statement 2; break; ....... case constant n: statement n; break; default: statement; } Syntax description of switch case statement - When the switch statement is executed, the value of expression is compared with the value of case constant (constant 1 , constant 2...) - Then the block of statements associated with the case whose value is matched with expression will be executed. - break statement at the end of block indicates the end of the particular case and cause an exit from switch statement and control will be transferred to statements following with switch. - If none of the case match with the value of the expression, then the block of statement under default is executed.
1. Write C program to input a number and display it is odd or even.
Algorithm to check whether the given number is odd or even
Step 1: Start
Step 2: Input any number as n
Step 3: Check is r % 2 =0 ?
3.1 If yes, display "even"
3.2 If no, display "odd"
Step 4: Stop
Flowchart to check whether the given number is odd or even
C program to check whether the given number is odd or even
#include<stdio.h>
int main()
{
int n;
printf("Enter a number : ");
scanf("%d",&n);
if(n%2==0)
printf(" %d is even ",n);
else
printf(" %d is odd ",n);
return 0;
}
Output
Enter a number : 7
7 is odd
=============
Enter a number : 4
4 is even
2. Write C program to know a candidate is eligible to caste vote or not. (age>=18)
Algorithm to know a candidate is eligible to caste vote or not. (age>=18)
Step 1: Start
Step 2: Input age
Step 3: Check is age > = 18 ?
3.1 If yes, display "can vote"
3.2 If no, display "can not vote"
Step 4: Stop
Flowchart to know a candidate is eligible to caste vote or not. (age>=18)
C program to know a candidate is eligible to caste vote or not. (age>=18)
#include<stdio.h>
int main()
{
int age;
printf("Enter age : ");
scanf("%d",&age);
if(age >=18)
printf(" can vote ");
else
printf(" Can not vote ");
return 0;
}
Output
Enter age : 20
can vote
==========
Enter age : 16
Can not vote
3. Write C program to input a number and display it is positive, negative or zero.
Algorithm to input a number and display it is positive, negative or zero
Step 1: Start
Step 2: Input any number as n
Step 3: Check is n>0?
3.1 If yes, display “Positive”
3.2 If no, goto step 4
Step 4: Check is n<0?
4.1 If yes, Print “Negative”
4.2 If no, goto step 5
Step 5: Print “Zero”
Step 6: Stop
Flowchart to input a number and display it is positive, negative or zero
C program to input a number and display it is positive, negative or zero
#include<stdio.h>
int main()
{
int n;
printf("Enter a number : ");
scanf("%d",&n);
if(n>0)
printf(" Positive ");
else if(n<0)
printf(" Negative ");
else
printf(" Zero ");
return 0;
}
Output
Enter a number : 4
Positive
==============
Enter a number : -5
Negative
==============
Enter a number : 0
Zero
4. Write C program to input two different numbers and display greater.
Algorithm to input two different numbers and display greater
Step 1: Start
Step 2: Input two numbers a and b
Step 3: Check is a>b?
3.1 If yes, print “a is greater number”
3.2 If no, print “b is greater number”
Step 4: Stop
Flowchart to input two different numbers and display greater
C program to input two different numbers and display greater
#include<stdio.h>
int main()
{
int a,b;
printf("Enter two numbers : ");
scanf("%d%d",&a,&b);
if(a>b)
printf(" %d is greater ",a);
else
printf(" %d is greater ",b);
return 0;
}
Output
Enter two numbers : 6 3
6 is greater
==============
Enter two numbers : 5 42
42 is greater
5. Write C program to input three different numbers and display smallest.
#include
int main()
{
int a,b,c;
printf("Enter three numbers : ");
scanf("%d%d%d",&a,&b,&c);
if(a<b&&a<c)
printf(" %d is smallest ",a);
else if(b<a&38;&b<c)
printf(" %d is smallest ",b);
else
printf("%d is smallest",c);
return 0;
}
Output
Enter three numbers : 24 26 11
11 is smallest
6. Write C program to input three different numbers and display middle.
7. Write C program to input marks in five subjects calculate total, percentage and division.
8. Write C program that inputs cost price (cp) and selling price (sp) and determine whether there is loss or gain.
9. A company pays its employee on hourly basis. If an employee works for 8 hrs he gets 100/hr and 120/hr for additional hours. Write a program to read working hours of an employee and calculate total salary.
#include<stdio.h>
int main()
{
float total_hrs;
float total_amount;
printf("enter total hours worked\n");
scanf("%f",&total_hrs);
if(total_hrs<=8)
{
total_amount=total_hrs*100;
printf("total amount=%f",total_amount);
}
else
{
total_amount=(120*(total_hrs-8))+8*100;
printf("total amount=%f",total_amount);
}
return 0;
}
10. The minimum charge for Nepal Electricity authority is Rs. 80 for 20 units consumed. If a person consumes more than that then s/he has to pay Rs. 7.25 per unit extra up to 100. If the person consumes more than 100 units then he has to pay 9.50 per unit. Write a 'C' program to calculate the total bill.
#include<stdio.h>
int main()
{
float total_units;
float total_amount;
printf("enter total units consumed\n");
scanf("%f",&total_units);
if(total_units<=20)
{
total_amount=80;
printf("total amount=%f",total_amount);
}
else if(total_units>20 && total_units<=100)
{
total_amount=80+(total_units-20)*7.25;
printf("total amount=%f",total_amount);
}
else
{
total_amount=total_units*9.50;
printf("total amount=%f",total_amount);
}
return 0;
}
11. Write C program to know whether three numbers are equal or not using nested if.
#include<stdio.h>
int main()
{
float a,b,c;
printf("enter three numbers\n");
scanf("%f%f%f",&a,&b,&c);
if(a==b)
{
if(b==c)
{
printf("they are same");
}
else
{
printf("%f and %f are same but %f is different",a,b,c);
}
}
else
{
printf("%f, %f and %f are different",b,c,a);
}
return 0;
}
12. Write C program to calculate sum, difference, product and division according to user choice using switch case.
#include<stdio.h>
int main()
{
int a,b,ch;
printf("\nEnter any two numbers: ");
scanf("%d%d", &a, &b);
printf("For these two numbers, we have following menu.\n");
printf("\nPress 1 for sum.");
printf("\nPress 2 for sub.");
printf("\nPress 3 for mul.");
printf("\nPress 4 for div.");
printf("\n-----------------\n");
printf("enter your choice\n");
scanf("%d", &ch);
switch(ch)
{
case 1:
printf("\nSum is: %d", a+b);
break;
case 2:
printf("\nSub is: %d", a-b);
break;
case 3:
printf("\nMul is: %d", a*b);
break;
case 4:
printf("\nDiv is: %d", a/b);
break;
default:
printf("this choice is not in the list/menu");
}
return 0;
}
13. Write C program to display name of day on the basis of entered number using switch. for example, 1 for Sunday, 2 for Monday.
#include<stdio.h>
int main()
{
int choice;
printf("Enter day number : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf(" Sunday ");
break;
case 2:
printf(" Monday ");
break;
case 3:
printf(" Tuesday ");
break;
case 4:
printf(" Wednesday ");
break;
case 5:
printf(" Thursday ");
break;
case 6:
printf(" Friday ");
break;
case 7:
printf(" Saturday ");
break;
default:
printf(" Wrong Choice ");
}
return 0;
}
14. Write C program to find the commission amount on the basis of sales amount as per following conditions:
Sales Amount Commission
0-1500 2%
1501-5000 5%
>5000 10%
#include<stdio.h>
int main()
{
int sa,c;
printf("Enter Sales Amount : ");
scanf("%d",&sa);
if(sa<=1500)
c=(sa*2)/100;
else if(sa>=1501 &&sa<=5000)
c=(sa*5)/100;
else
c=(sa*10)/100;
printf("Commission = Rs. %d",c);
return 0;
}
Case study:
Write C program to input length and breadth and height and calculate area, perimeter and volume according to user choice
*********************Menu*******************
************A-> Calculate Area******************
************P->Calculate Perimeter**************
************V->Calculate Volume****************
************E->Exit***************************
#include<stdio.h>
int main()
{
int ar,pr,vol,l,b,h;
char ch;
printf("\n***************Menu*************\n");
printf("************A-> Calculate Area**************\n");
printf("***********P->Calculate Perimeter***********\n");
printf("************V->Calculate Volume*************\n");
printf("************E->Exit***********************\n");
printf("Enter your choice : ");
scanf("%c",&ch);
printf("Enter length, breadth and height : ") ;
scanf("%d%d%d",&l,&b,&h);
switch(ch)
{
case 'a':
ar=l*b;
printf("Area=%d",ar);
break;
case 'p':
pr=2*(l+b);
printf("Perimeter=%d",pr);
break;
case 'v':
vol=l*b*h;
printf("Volum=%d",vol);
break;
case 'e':
printf("Exit");
default :
printf("Wrong Choice");
}
return 0;
}