0% found this document useful (0 votes)
2 views

Lab Manual # 2. Selection Statements

This lab manual from Rajshahi University of Engineering & Technology covers selection statements in C programming, including if, if-else, switch statements, and the conditional operator. It provides objectives, background information, examples, and exercises for students to practice these concepts. The document includes sample code for various programs demonstrating the use of selection structures and conditional operators.

Uploaded by

ohijannatul572
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab Manual # 2. Selection Statements

This lab manual from Rajshahi University of Engineering & Technology covers selection statements in C programming, including if, if-else, switch statements, and the conditional operator. It provides objectives, background information, examples, and exercises for students to practice these concepts. The document includes sample code for various programs demonstrating the use of selection structures and conditional operators.

Uploaded by

ohijannatul572
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

“Heaven’s Light is Our Guide”

Department of Computer Science & Engineering

RAJSHAHI UNIVERSITY OF ENGINEERING & TECHNOLOGY

Programming in C

Lab Manual : 2

Selection Statements (if, if-else & switch) and Conditional Operator

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

The if-else statement:


Often your program will want to take one branch if your condition is true, another if it is false. In
such cases if-else statement is used.
The simplest form of if-else statement is:

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.

The switch statement:


Switch is a built-in multiple branch selection statement in C programming language, which
successively tests the value of an expression against a list of integer or character constants. When
a match is found, the statements associated with that constant are executed. There must be break
at the end of the statements of each case otherwise all the preceding cases will be executed
including the default condition. The general form of the switch statement is:

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:

(expression1) ? (expression2) : (expression3)


This line is read as "If expression1 is true, return the value of expression2; otherwise, return the
value of expression3." Typically, this value would be assigned to a variable.

Look at the following if else structure:


if( a > b ){
c = a;
}
else{
c = b;
}

This if else structure can be replaced by


c = (a > b)? a : b

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

Operation to be performed (+, -, *, /): * Operation to be performed (+, -, *, /): +

Sample Output: Sample Output:


The Result = 990 The Result = 19

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

Sample Output: Sample Output:


Spelling: Zero Spelling: Seven

You might also like