Professional Documents
Culture Documents
Lab Manual 6 C++ Updated
Lab Manual 6 C++ Updated
PROGRAMMING
FUNDAMENTALS
Experiment 6
Decision Constructs
if, if...else and switch
1
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Purpose:
constructs “if-else and switch”. Students will run and check different programs
Objectives:
1) Know the different decision construct available in C++: how to use them in a C++
program.
2) Run and check different but related programs consisting if-else and switch
constructs.
3) Implementation of C++ program using the right decision construct according to the
required logic.
5) Code::Blocks IDE16.01
2
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
1. if statement
2. if...else statement
3. if...else if...else statement
C++ if Statement
The syntax of the if statement is:
if (condition)
{
// body of if statement
}
• If the condition evaluates to true, the code inside the body of if is executed.
• If the condition evaluates to false, the code inside the body of if is skipped.
3
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
#include <iostream>
using namespace std;
int main() {
int number;
Output 1
Enter an integer: 5
You entered a positive number: 5
This statement is always executed.
4
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
When the user enters 5, the condition number > 0 is evaluated to true and the statement inside
the body of if is executed.
Output 2
Enter a number: -5
This statement is always executed.
When the user enters -5, the condition number > 0 is evaluated to false and the statement inside
the body of if is not executed.
C++ if...else
The if statement can have an optional else clause. Its syntax is:
if (condition) {
// block of code if condition is true
}
else {
// block of code if condition is false
}
#include <iostream>
using namespace std;
int main() {
int number;
Output 1
Enter an integer: 4
You entered a positive integer: 4.
This line is always printed.
In the above program, we have the condition number >= 0. If we enter the number greater or
equal to 0, then the condition evaluates true. Here, we enter 4. So, the condition is true. Hence,
the statement inside the body of if is executed.
Output 2
Enter an integer: -4
You entered a negative integer: -4.
6
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Here, we enter -4. So, the condition is false. Hence, the statement inside the body of else is
executed.
if (condition1)
{
// code block 1
} else if (condition2)
{
// code block 2
} else
{
// code block 3
}
Here,
7
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Note: There can be more than one else if statement but only one if and else statements.
#include <iostream>
using namespace std;
int main() {
int number;
8
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Output 1
Enter an integer: 1
You entered a positive integer: 1.
This line is always printed.
Output 2
Enter an integer: -2
You entered a negative integer: -2.
This line is always printed.
Output 3
Enter an integer: 0
You entered 0.
This line is always printed.
In this program, we take a number from the user. We then use the if...else if...else ladder
to check whether the number is positive, negative, or zero.
If the number is greater than 0, the code inside the if block is executed. If the number is less than
0, the code inside the else if block is executed. Otherwise, the code inside the else block is
executed.
Think of it as multiple layers of if statements. There is a first, outer if statement, and inside it is
another, inner if statement. Its syntax is:
// outer if statement
if (condition1) {
// statements
// inner if statement
if (condition2) {
// statements
}
}
9
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Notes:
• We can add else and else if statements to the inner if statement as required.
• The inner if statement can also be inserted inside the outer else or else if statements
(if they exist).
• We can nest multiple layers of if statements.
#include <iostream>
int main() {
int num;
10
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Output 1
Enter an integer: 34
The number is Positive.
This line is always printed.
Output 2
Output 3
Enter an integer: 0
The number is 0.
This line is always printed.
int number = 5;
if (number > 0) {
cout << "The number is positive." << endl;
}
else {
cout << "The number is negative." << endl;
11
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
with
int number = 5;
if (number > 0)
cout << "The number is positive." << endl;
else
cout << "The number is negative." << endl;
Note: Although it's not necessary to use { } if the body of if...else has only one statement,
using { } makes your code more readable.
Switch Statement
A switch statement allows a variable to be tested for equality against a list of values. Each value is
called a case, and the variable being switched on is checked for each case.
Syntax
The syntax for a switch statement in C++ is as follows −
switch (expression) {
case constant1:
// code to be executed if
// expression is equal to constant1;
break;
case constant2:
// code to be executed if
// expression is equal to constant2;
break;
.
.
.
default:
// code to be executed if
// expression doesn't match any constant
}
12
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
The expression is evaluated once and compared with the values of each case label.
• If there is a match, the corresponding code after the matching label is executed. For
example, if the value of the variable is equal to constant2, the code after case
constant2: is executed until the break statement is encountered.
• If there is no match, the code after default: is executed.
Note: We can do the same thing with the if...else..if ladder. However, the syntax of the
switch statement is cleaner and much easier to read and write.
13
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
switch (oper) {
case '+':
cout << num1 << " + " << num2 << " = " << num1 + num2;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1 - num2;
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1 * num2;
break;
case '/':
cout << num1 << " / " << num2 << " = " << num1 / num2;
break;
default:
// operator is doesn't match any case constant (+, -, *, /)
cout << "Error! The operator is not correct";
break;
}return 0;
}
Output 1
Output 2
Output 3
Output 4
Output 5
In the above program, we are using the switch...case statement to perform addition,
subtraction, multiplication, and division.
Notice that the break statement is used inside each case block. This terminates the switch
statement.
If the break statement is not used, all cases after the correct case are executed.
1. We first prompt the user to enter the desired operator. This input is then stored in the char
variable named oper.
2. We then prompt the user to enter two numbers, which are stored in the float variables
num1 and num2.
3. The switch statement is then used to check the operator entered by the user:
o If the user enters +, addition is performed on the numbers.
o If the user enters -, subtraction is performed on the numbers.
o If the user enters *, multiplication is performed on the numbers.
o If the user enters /, division is performed on the numbers.
o If the user enters any other character, the default code is print
15
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Task 1:
o Write a C++ program in which user will enter Character.
o Program should check and display whether a character is Vowel or
Consonant.
Task2:
o Write C++ program to Find Largest Number Among Three Numbers
Task3:
o Write C++ program to Check Whether Number is Even or Odd using nested if
statements.
16
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
17