Lab Manual # 2. Selection Statements
Lab Manual # 2. Selection Statements
Programming in C
Lab Manual : 2
INDEX
Lab Objectives
Background
Some Examples
Exercises
Lab Objectives:
• Explain the basic of different selection structures ( if, if-else & switch structure )
• Design programs by using selection structure
• Explain the conditional operator
Background:
The if statement:
The if statement enables you to test for a condition (such as whether two variables are equal) and
go to different parts of your code depending on the result of the condition.
The simplest form of an if statement is:
if (expression){
statement/s;
}
if (expression){
statement/s;
}
else{
statement/s;
}
If only one statement is to be followed by the if or else condition then there is no need of
parenthesis.
switch (expression){
case constant1: statement/s;
break;
case constant2: statement/s;
break;
.
.
.
.
.
default: statement/s;
}
Conditional Operator:
The conditional operator ( ?: ) is C’s only ternary operator; that is, it is the only operator to take
three operands.
The conditional operator takes three expressions and returns a value:
Some Examples:
1. Write a program that will read an integer and print whether it is odd or even.
Source code:
#include<stdio.h>
int main(){
int num;
printf("Enter any number: ");
scanf("%d", & num);
if(num%2==0){
printf("\n %d is an Even number.\n",num);
}
else{
printf("\n %d is an Odd number.\n",num);
}
return 0;
}
2. Write a program that will read obtained marks and display the result in division.
Source code:
#include<stdio.h>
int main(){
int mark;
printf(" Obtained Marks = ");
scanf("%d", &mark);
if ( mark >= 60 )
printf("\n --> First Division. \n ");
else if( mark >= 45 )
printf("\n --> Second Division. \n ");
else if( mark >= 33 )
printf("\n --> Third Division. \n ");
else
printf("\n --> Fail…\n ");
return 0;
}
3. Write a program that will read obtained marks and display ‘P’ (for Pass) or ‘F’ (for Fail)
using conditional operator.
Source code:
#include<stdio.h>
int main(){
int mark;
char passORfail;
printf("Obtained Marks = ");
scanf("%d", &mark);
passORfail = (mark>=40) ? 'P' : 'F';
printf("\nDecision (P/F): %c \n", passORfail);
return 0;
}
4. Write a program that will read a character and display whether it is vowel or not.
Source code:
#include<stdio.h>
int main(){
char ch;
printf("Enter a character: ");
scanf("%c",&ch);
switch(ch){
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
printf("\n %c is vowel\n",ch);
break;
default:
printf("\n %c is not vowel\n",ch);
}
return 0;
}
Exercise:
1. Write a program that will read two numbers and display the minimum using
conditional operator.
2. Write a program that will read four numbers and display the maximum.
3. Write a program that will read two numbers and perform addition (+) / subtraction
(-) / multiplication (*) / division (/) operation between them using switch statement.
Sample Input: Sample Input:
Enter two numbers: Enter two numbers:
10 10
99 9
4. Write a program that will accept a number and check whether it is divisible by 5 or
not.
5. Write a program that will accept a digit from user and display by spelling.
Sample Input: Sample Input:
Enter a digit: 0 Enter a digit: 7