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

Computer Programming Lab Manual

This document provides a lab manual for a computer programming lab on nested if-else statements and logical operators in C++. It introduces nested if-else statements, logical AND, logical OR, and logical NOT operators. It explains when to use nested if-else versus logical operators through examples. The document also provides practice tasks for students to apply these concepts and will evaluate students on additional unseen tasks.

Uploaded by

ẄâQâŗÂlï
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
183 views

Computer Programming Lab Manual

This document provides a lab manual for a computer programming lab on nested if-else statements and logical operators in C++. It introduces nested if-else statements, logical AND, logical OR, and logical NOT operators. It explains when to use nested if-else versus logical operators through examples. The document also provides practice tasks for students to apply these concepts and will evaluate students on additional unseen tasks.

Uploaded by

ẄâQâŗÂlï
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Mohammad Ali Jinnah University Islamabad

Department of Computer Science,


Faculty of Computing

Lab Manual for Computer Programming


Lab 7: Nested If-else and Logical Operators

Lab 7: Nested If-else and Logical Operators

Table of Contents
1.

Introduction

42

2.

Activity Time boxing

42

3.

Objective of the Experiment

43

4.

Concept Map

43

4.1
4.2
4.3
4.4
4.5

43
43
44
45
46

5.

6.

7.

Nested-if statement
Logical-and (&)
Similarities and Differences for the use of nested-if and logical-and
Logical-or (||)
Logical-not (!)

Home work before Lab

46

5.1
5.2

46
47

Problem solution modeling


Practices from home

Procedure & Tools

47

6.1
6.2

47
47
48
49
49

Tools
Walk-through Task
6.2.1 Writing Code
6.2.2 Compilation
6.2.3 Executing the Program

Practice Tasks

50

7.1
7.2
7.3
7.4
7.5

50
50
50
51
51

Practice Task 1
Practice Task 2
Practice Task 3
Out comes
Testing

8.

Evaluation Task (Unseen)

51

9.

Evaluation criteria

51

10.

Further Readings

52

10.1
10.2

52
52

Books
Slides

Appendix A Logical Operators

52

Department of Computer Science,


MAJU, 2013

P a g e | 41

Lab 7: Nested If-else and Logical Operators

Lab 7: Nested If-else and Logical Operators


1. Introduction
In the previous lab, you have learned how to use conditions and have learned three different types of
conditions (simple if-statement, if-else statement and if-else-if statements, and compound statements).
Those statements worked for a single condition; however, there are certain real problems when we
need to make the decision based one two or more conditions. In this case we can use nested-if or can
use logical operators. You will learn in detail about nested-if and logical operators in this lab. The
concept map presented in this lab will guide you when and where to use nested-if or logical operators.
The logical operators have been made available in the Appendix A for your reference.
The section 2 presents a table that outlines some major activities and tasks you will do as the part of this
lab. Table 1 also provides the estimated-time for each activity, which will help you to organize your tasks
well. Section 3 presents some of the learning objectives for this lab. Section 4 (Concept Map) discusses
and provides a comprehensive introduction of the topic. Section 5 lists the set of home-tasks you are
required to complete before this lab. Section 6 presents a walkthrough task that you will do as the first
practical activity during your lab. The walkthrough task has many small steps which you should follow as
directed in-order to complete the task and to get the desired output. After that, you will be ready to
work on some tasks on your own. The section 7 lists practice tasks for this purpose. As the part of
section 8, your lab instructor will give you some tasks at runtime and will evaluate those according to
the criteria mentioned in section 9. Section 10 lists some further reading links.
Note: Before coming to the lab, you are required to read Lab contents until section 5. You will
start your practical work from section 6 onward in the lab.

Relevant Lecture Readings:


a) Lecture No. 7
b) Text Book: Computer Programming by D. S. Malik, pages: 175-184, 192-198, 203-204
c) Revise Quick Review available at 219-220

2. Activity Time boxing

Task No.
5.1
6.1
6.2
7
9

Table 1: Activity Time Boxing


Activity Name
Activity time
Evaluation of Design
20 mins
Setting-up Visual Studio
5 mins
Specialized Tasks
30 mins
Practice tasks
As mentioned against each task
Evaluation Task
30 mins for each assigned task
Total Time

Department of Computer Science,


MAJU, 2013

Total Time
20 mins
5 mins
15 mins
70 mins
60 mins
170 mins

P a g e | 42

Lab 7: Nested If-else and Logical Operators

3. Objective of the Experiment

To get basic understanding of nested-if conditions


To get basic understanding of logical operators.
Where and when to use nested-if and logical-and.
To practice how the nested-if and logical operators are used in a C++ program.

4. Concept Map
We will introduce the nested-if condition and logical operators in this section. Furthermore, we will
study where and when to use these concepts in C++ program. When an if-statement is included within
another if-statement then we say that this is nested-if. For evaluating more than one condition, we can
use either nested-if or logical operators. There are three logical operators available in C++. For example
logical-and (&&), logical-or (||), and logical-not (!). We will study them one by one.
4.1 Nested-if statement

When we want to make decisions based on more than one conditions then one way of achieving this is
using nested-if condition. For example, consider the following example. A student is given a scholarship,
if and only if his/her percentage is more than 70% and he/she is below 16 years of age. Here, you need
to notice that there are two conditions and if both conditions are fulfilled then the student will be given
scholarship, otherwise, no scholarship will be awarded. To accomplish such task you can use the concept
of nested-if in the following way.
if (percentage>=70 )
{
if (age <16)
{
cout<<Congratulations! You are eligible for the scholarship;
}
}

The nested-if statement is executed in the following way, first, the statement (percentage>=70 ) will
be evaluated, if the statement is true then the control is passed to the first statement within this ifstatement. The first line within this if-statement is again an if-statement, now the statement (age
<16) will be evaluated. Here if this statement is also true, then the student will be given the scholarship.
We can use logical-and to solve the same problem as well. This will be discussed in the next section.
4.2 Logical-and (&)

The same problem (discussed in section 4.1) can be solved with using logical-and operator (&) between
both conditions. For example, consider the following code:
if (percentage>=70 && age <16)
{
cout<<Congratulations! You are eligible for the scholarship;
}

To understand the above statement, revise your concepts from the previous course of ITC. The logicaland is evaluated as true if both values (conditions) are true. For revision, you can have a look on the
Department of Computer Science,
MAJU, 2013

P a g e | 43

Lab 7: Nested If-else and Logical Operators

Appendix A. For the above code, if both conditions are true, then the statement will be executed. The
difference between using logical-and and nested-if has been explained in the next section.

4.3 Similarities and Differences for the use of nested-if and logical-and
For the example used in the section 4.1 and 4.2, there is no difference, one can use any of them (nestedif or logical-and)
4.3.1 Scenario 1: Nested-if and Logical-and (&&) are similar

Consider the followings two codes:


Code A:
if (percentage>=70 )
{
if (age <16)
{
cout<<Congratulations! You are eligible for the scholarship;
}
}

Code B:
if (percentage>=70 && age <16)
{
cout<<Congratulations! You are eligible for the scholarship;
}

In this scenario, both codes will output the same. However, there are certain differences in using
nested-if and logical-and (&&). There are some scenarios where use of nested-if is better, however,
there are some other scenarios where the use of logical-and(&&) is better. For example, consider the
following case.
4.3.2 Scenario 2: Nested-if is better than Logical-and (&&)

Consider the following code:


if (percentage>=70 )
{
if (age <16)
{
cout<<Congratulations! You are eligible for the scholarship;
}
cout<<Your percentage is more than 70;
}

Department of Computer Science,


MAJU, 2013

P a g e | 44

Lab 7: Nested If-else and Logical Operators

However, you probably, will not like to write the above code in the following way.
if (percentage>=70 && age <16)
{
cout<<Congratulations! You are eligible
scholarship;
}
if (percentage>=70)
{
cout<<Your percentage is more than 70;
}

for

the

4.3.3 Scenario 3: Logical-and (&&) is better than nested-if

Consider the following code.


if (percentage>=70 && age <16)
{
cout<<Congratulations! You are eligible for
scholarship;
}
else
{
cout<<Sorry, you did not get the scholarship;
}

the

You will not like to write it in the following way:


if (percentage>=70 )
{
if (age <16)
{
cout<<Congratulations! You are eligible for the scholarship;
}
else {
cout<<Sorry, you did not get the scholarship;
}
}
else
{
cout<<Sorry, you did not get the scholarship;
}

4.4 Logical-or (||)


Apart from logical-and (&&), there is another logical operator which is known as logical-or (||). You can
Department of Computer Science,
MAJU, 2013

P a g e | 45

Lab 7: Nested If-else and Logical Operators

refresh your concepts of logical operators by looking into the appendix A available at the end of this lab.
In the logical-or (||), the condition is considered as true if any of the conditions are true. For example,
re-writing the previous example with logical-or (||) would look like the following
if (percentage>=70 || age <16)
{
cout<<Congratulations!
scholarship;
}

You

are

eligible

for

the

The cout statement will be executed if any of the conditions is true. For example either the person has
achieved more than 70% marks or his/her age is less than 16, the scholarship will be awarded to that
person.

4.5 Logical-not (!)


The logical-not (!) is used before any condition and it will reverse the evaluation of the condition. For
example, if the condition is true, the logical-not (!) will make it false, if the condition is false, then the
logical-not (!) will make it true. Consider the following example. logical-not (!):
if (!(5==5))
cout<<5 is not equal to 5;

the above statement will be evaluated as false because the condition (5==5) is evaluated as true and
logical-not (!) will make it false, as the if-statement is evaluated as false, therefore, the cout-statement
will not be executed. Similarly, we have written the previously discussed example by incorporating
logical-not (!)
if (percentage>=70 && (!(age <16)))
{
cout<<Congratulations!
scholarship;
}

You

are

eligible

for

the

Now we have both logical-and and logical-not. You know that logical-and will be evaluated as true if
both conditions are true. The first condition would be true when percentage is more than or equal to 70,
however, the second condition would be true when the age is more than or equal to 16. Remember,
from previous discussion that whenever the inner condition (age<16) is false, the logical-not (!) will
make it true. Therefore, the scholarship will be given to only those students whose percentage is greater
than or equal to 70 and whose age is more than or equal to 16.

5. Home work before Lab


5.1 Problem solution modeling

Write the pseudo-code for the following problems:


1. Write a program that calculates salary and medical charges for the employees. There are two
Department of Computer Science,
MAJU, 2013

P a g e | 46

Lab 7: Nested If-else and Logical Operators

types of employees permanent and daily wages. The daily wages employee are paid 400 per
hour while permanent employees are paid 800 per hour. First ask the user for employee type
and then ask for either calculating salary or medical charges. Permanent employees are paid 5%
medical charges of their total salary while daily wages are paid 3% of medical charges. After
calculating for one employee it should ask the user for continuation.

2. Write a program which asks the user to either convert the number of days into hours or
minutes. Based on the option entered by user your program should calculate the result
accordingly.
5.2 Practices from home

1. Write a program that asks the user for three numbers and then print the maximum and
minimum number to the user.
2. Write a program that ask the users to enter three characters. Then the program should display
the following menu
1. Sort in Ascending order
2. Sort in Descending order
The program should perform sorting of these three characters in ascending or descending order
as per the user requirement.

6. Procedure & Tools


6.1 Tools

Visual Studio 2008.


To achieve this task, you need to setup a file as you did in Lab-5. Refer to the 6.2 section of lab-5 for
setting up a project.
6.2

Walk-through Task

[Expected time = 15 mins]

In this specialized task, you will learn that how the practice task 2 of third lab can be achieved with the
help of nested-if statement.
Write a program which would calculate the grades of students against their score. The grading criteria is
as follows:
>= 90
>=80
>=70
>=60
<60

A
B
C
D

grade
grade
grade
grade
F grade

Use nested if-else to achieve this task.

Department of Computer Science,


MAJU, 2013

P a g e | 47

Lab 7: Nested If-else and Logical Operators

6.2.1 Writing Code


Remember you created a file with name myprog in the task 5.1.8. now write the following code as
shown in the Figure 1. Write the code in the intended form as shown.
.

Figure 1: Writing the C++ code

Department of Computer Science,


MAJU, 2013

P a g e | 48

Lab 7: Nested If-else and Logical Operators

6.2.2 Compilation
After writing the code, now you are ready to compile it. For compilation, select Build Solution from the
Build option in the menu bar as shown in the Figure 2.

..
Figure 2: Compiling the C++ program
.

When you click on build solution, the compiler would start processing your program. The progress of
build activity would be displayed on the output window as shown in the Figure 3.

Figure 3: Build Activity

The progress would show whether the build was successful or not. It would display the errors if any.
Otherwise it would display the message Build succeeded.
6.2.3 Executing the Program

Now run the program by pressing Ctrl+F5 from keyboard or selecting play option from Debug menu
(Debug menu can be accessed from the Figure 2). When you press Ctrl+F5, the compiler would start
executing the program and would display the final output to your screen as shown in the Figure 4.

Department of Computer Science,


MAJU, 2013

P a g e | 49

Lab 7: Nested If-else and Logical Operators

Figure 4: Final output

7. Practice Tasks
This section will provide more practice exercises which you need to finish during the lab. You need to
finish the tasks in the required time. When you finish them, put these tasks in the folder specified by
your lab instructor

7.1 Practice Task 1

[Expected time = 20 mins]

Write a program which would calculate the sales tax on two products by the name of Toothpaste and
Shampoo. First the user would be asked for the product type. Once the user selects the product then ask
him/her about the price of the product. For the product Toothpaste, If the price is greater than 50 and
less than 500 then calculate 3% sales tax, and for the product Shampoo, , If the price is greater than 100
and less than 999 then calculate 5% sales tax on the product. Then display the sales tax to the user.
Note: Use nested-if to achieve this task.

7.2 Practice Task 2

[Expected time = 30 mins]

Write a program which asks the user to open a bank account either Current or Savings. Your program
will display the following menu at the start:
Press 1 for Current Account
Press 2 for Savings Account

On the selection of account type, the user will open an account by providing an initial amount. In current
account the initial amount for opening an account would be 2000 and for savings it would be 5000. After
choosing the account type and opening the account with initial amount, your program will display the
following menu:
Press a for Deposit the money
Press b for withdrawing the money

In current account the user cannot withdraw more than 10000 rupees. In Savings account the user
cannot withdraw more than 25000. When the user withdraws amount make sure that amount should
not be more than the balance in account and at the end of withdrawal transaction, there should be
minimum of 500 rupees for each account.

7.3 Practice Task 3

[Expected time = 20 mins]

Write a program in which a father wants to know the grade of his two sons. Ask the user for the marks.
If the marks are greater than 90 then he/she receives A grade. If his first son gets an A grade then he will
be give a treat by his father. If the second son gets an A grade then he would be given a treat by his
Department of Computer Science,
MAJU, 2013

P a g e | 50

Lab 7: Nested If-else and Logical Operators

father. If both sons get A grade then both of them will get a treat from their father. Use nested if-else to
do this task.

7.4 Out comes


The outcomes of this lab were:
a) You have learnt nested-if control structures in C++
b) You have practiced different tasks how to use selections.
c) You have practiced logical operators

7.5 Testing
For the tasks mentioned in Section 7, following are the test cases. The instructor will check the outputs
of the practice tasks accordingly.
Table 2: Testing of Practice Tasks

Practice
Tasks
6.2

Sample Input

Sample output

Enter your marks


Marks:85
Marks:65
Marks:40
Marks: 60

Grade is: B
Grade is: D
Grade is: F
Grade is: D

7.1

Enter product type:


1 (for toothpaste)
2 (for shampo)
1
Enter price = 499

Sales tax = 14.97

7.2.1

Account Type: 1
Amount: 2000
Deposit Amount: 1000
Withdraw amount: 1500
Account Type: 2
Amount: 5000
Deposit Amount: 500
Withdraw amount: 5400
Withdraw amount : 4500

Available Balance: 1500

7.2.2

8. Evaluation Task (Unseen)

Confirmation

Rs. 500 must be available


after withdraw transaction,
please enter the amount
again.
Available balance = 1000

[Expected time = 60 mins]

The lab instructor will give you unseen task depending upon the progress of the class.

9. Evaluation criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each task is
assigned the marks percentage which will be evaluated by the instructor in the lab whether the student
has finished the complete/partial task(s).

Department of Computer Science,


MAJU, 2013

P a g e | 51

Lab 7: Nested If-else and Logical Operators

Table 3: Evaluation of the Lab

Sr. No.
1
2
3
4
5
6

Task No
5.1
5.3
7
8

Description
Problem Modeling
Home work
Practice tasks and Testing
Evaluation Tasks (Unseen)
Comments
Good Programming Practices

Marks
20
10
35
20
5
10

10. Further Readings


10.1 Books
Text Book:
Computer Programming by D.S Malik, second edition

Reference Books:
1. Beginning C++, the complete language, by Ivor Horton, Wrox Publishers.
2. How to Program in C++, Dietel and Dietel

10.2 Slides
The slides and reading material can be accessed from the folder of the class instructor available at
\\dataserver\jinnah$

Appendix A Logical Operators


&& OPERATOR

|| OPERATOR

! OPERATOR

a && b

a || b

!a

true true true

true true true

true false false

true False

true false true

false true False

False True

false true true

false false False

false false false

Department of Computer Science,


MAJU, 2013

P a g e | 52

You might also like