1st Semester Mannual#04 Programming Fundamentals
Students’s Name: ID:
Experiment#04
To study and implement Selections using if, if –else and
switch Statements
In programming, decision making is used to specify the order in which statements are executed.
Normally, statements in a program are executed one after the other in the order in which they are
written. This is called sequential execution.
A selection structure selects a statement or set of statements to execute on the basis of condition.
These are if, if-else and switch.
if Statement: The if statement allows us to check if an expression is true or false, and execute
different code according to the result.
Syntax:
if (conditional)
{
Statement No 1
Statement No 2
Statement No 3
.
.
.
Statement No N
}
How if statement works?
The if statement evaluates the test expression inside the parenthesis.
➢ If the test expression is evaluated to true , statement(s) inside the body of if is executed.
➢ If the test expression is evaluated to false, statement(s) inside the body of if is skipped from
execution.
1
1st Semester Mannual#04 Programming Fundamentals
Fig.4.1 if Statement Working
Note:
1. More than One Conditions can be Written inside If statement.
2. Opening and Closing Braces are required only when “Code” after if statement occupies
multiple lines.
Example#1: Program to display a number if user enters negative number.
#include <iostream>
Using namespace std;
int main ()
{
int number;
cout<<"Enter an integer: ";
cin>> number;
// Test expression is true if number is less than 0
if (number < 0)
{
cout<<"You entered a negative number\n"<< number;
}
cout<<"The if statement is easy.";
return 0;
}
2
1st Semester Mannual#04 Programming Fundamentals
Output 1:
Enter an integer: -2
You entered -2.
The if statement is easy.
When user enters -2, the test expression (number < 0) is evaluated to true. Hence, you entered -
2 is displayed on the screen.
Output 2:
Enter an integer: 5
The if statement is easy.
When user enters 5, the test expression (number < 0) is evaluated to false and the statement inside
the body of if is skipped.
if-else Statement: The if-else statement may have an optional else block. The syntax of if-
else statement is:
Syntax:
if (test Expression)
{
// statement(s) inside the body of if
}
else
{
// statement(s) inside the body of else
}
How if...else statement works?
If test expression is evaluated to true,
➢ statement(s) inside the body of if statement is executed
➢ statement(s) inside the body of else statement is skipped from execution.
If test expression is evaluated to false,
➢ statement(s) inside the body of else statement is executed
3
1st Semester Mannual#04 Programming Fundamentals
➢ statement(s) inside the body of if statement is skipped.
Fig.4.2 if-else Statement Working
Example#2: Program to check whether an integer entered by the user is odd
or even
#include <iostream>
Using namespace std;
int main ()
{
int number;
cout<<"Enter an integer: ";
cin >> number;
// True if remainder is 0
if (number%2 == 0)
cout<<number<<"is an even integer.";
else
cout<<number<<"is an odd integer.";
return 0;
}
Output
Enter an integer: 7
7 is an odd integer.
4
1st Semester Mannual#04 Programming Fundamentals
When user enters 7, the test expression (number%2 == 0) is evaluated to false. Hence, the
statement inside the body of else statement cout<<"number is an odd integer"; is executed and the
statement inside the body of if is skipped.
if...else Ladder (if...else if.... else Statement)
The if...else statement executes two different codes depending upon whether the test expression is
true or false. Sometimes, a choice has to be made from more than 2 possibilities.
The if...else ladder allows you to check for multiple test expressions and execute different
statement(s).
Syntax of nested Ladder if-else statement:
if (test_Expression1)
{
// statement(s)
}
else if(test_Expression2)
{
// statement(s)
}
else if (test_Expression 3)
{
// statement(s)
}
.
.
else
{
// statement(s)
}
Nested if...else
It is possible to include if...else statement(s) inside the body of another if...else statement.
This program below relates two integers using either <, > and = similar like in if...else ladder
example. However, we will use nested if...else statement to solve this problem.
5
1st Semester Mannual#04 Programming Fundamentals
Example#4: Nested if...else ( To compare two numbers)
#include <iostream.h>
Using namespace std;
int main()
{
int number1, number2;
cout<<"Enter first integers: ";
cin>>number1;
cout<<"Enter second integer: ";
cin>>number2
Output:
if (number1 >= number2)
{
if (number1 == number2)
{
cout<<"two numbers are equal";
}
else
{
cout<< numbers1<<" is greater than "<< numbers2;
}
}
else
{
cout<< numbers1<<" is less than "<< numbers2;
return 0;
}
Note: If the body of if...else statement has only one statement, you do not need to use parenthesis {
}.
This code
if (a > b)
{
print("Hello");
}
print("Hi");
is equivalent to
if (a > b)
print("Hello");
print("Hi");
6
1st Semester Mannual#04 Programming Fundamentals
Switch Statement:
The if..else. If ladder allows you to execute a block code among many alternatives. If you are
checking on the value of a single variable in if...else...if, it is better to use switch statement.
The switch statement is often faster than nested if...else (not always). Also, the syntax of switch
statement is cleaner and easy to understand.
Syntax of switch...case
switch (n)
{
case constant1:
// code to be executed if n is equal to constant1;
break;
case constant2:
// code to be executed if n is equal to constant2;
break;
.
.
.
default:
// code to be executed if n doesn't match any constant
}
When a case constant is found that matches the switch expression, control of the program passes
to the block of code associated with that case.
Suppose, the value of n is equal to constant2. The compiler executes the statements after case
constant2: until break is encountered. When break statement is encountered, switch statement
terminates.
7
1st Semester Mannual#04 Programming Fundamentals
Fig.4.1
Example#5: switch Statement (Program to create a simple calculator)
#include <iostream>
Using namespace std;
int main()
{
char op;
double number1, number1;
8
1st Semester Mannual#04 Programming Fundamentals
cout<<"Enter an operator (+, -, *, /): ";
cin>>op;
cout<<"Enter first integers: ";
cin>>number1;
cout<<"Enter second integer: ";
cin>>number2
switch(op)
{
case '+':
printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber, firstNumber+
secondNumber);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber, firstNumber-secondNumber);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber, firstNumber*secondNumber);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber/secondNumber);
break;
// operator is doesn't match any case constant (+, -, *, /)
default:
printf("Error! operator is not correct");
}
return 0;
}
goto Statement:
9
1st Semester Mannual#04 Programming Fundamentals
The goto statement is used to alter the normal sequence of a C program.
Syntax of goto statement
goto label;
... .. ...
... .. ...
... .. ...
label:
statement;
The label is an identifier. When goto statement is encountered, control of the program jumps
to label: and starts executing the code.
Example#6: goto Statement (Program to calculate the sum and average of maximum of 5
numbers. If user enters negative number, the sum and average of previously entered positive
numbers are displayed)
# include <iostream>
Using namespace std;
int main()
{
const int maxInput = 5;
int i;
double number, average, sum=0.0;
for(i=1; i<=maxInput; ++i)
{
cout<<"Enter a number: ";
cin>>number;
// If user enters negative number, flow of program moves to label jump
if(number < 0.0)
goto jump;
sum += number; // sum = sum+number;
}
10
1st Semester Mannual#04 Programming Fundamentals
jump:
average=sum/(i-1);
cout<<"Sum = \n"<<sum;
cout<<"Average = "<<average;
return 0;
}
Lab Task:
1. Write a program that inputs two numbers and finds if second number is square of first.
2. Write a program that inputs five integers and displays the largest and smallest integer.
3. Write a program that inputs a year and finds whether it is leap year or not using if-else
structure.
4. Write a Program that inputs two integers.it determines and prints if the first integer is a
multiple of second integer.
5. Write a Program that inputs a character from the user and check whether it is vowel or
consonant.
11
1st Semester Mannual#04 Programming Fundamentals
Observations/Comments/Explanation of Results
(Please write in your own words the objectives and yours learning during the experiment. Also
explain the results and comment on it.)
Checked By: Date:
12