Experiment#04 To Study and Implement Selections Using If, If - Else and Switch Statements
Experiment#04 To Study and Implement Selections Using If, If - Else and Switch Statements
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
}
➢ 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
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.
#include <iostream>
Using namespace std;
int main ()
{
int number;
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
}
3
1st Semester Mannual#04 Programming Fundamentals
// 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 (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
Output:
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
#include <iostream>
Using namespace std;
int main()
{
char op;
8
1st Semester Mannual#04 Programming Fundamentals
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;
return 0;
}
goto Statement:
9
1st Semester Mannual#04 Programming Fundamentals
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()
{
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.)
12