Computer Programming Lab Manual
Computer Programming Lab Manual
Table of Contents
1.
Introduction
42
2.
42
3.
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 (!)
46
5.1
5.2
46
47
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.
51
9.
Evaluation criteria
51
10.
Further Readings
52
10.1
10.2
52
52
Books
Slides
52
P a g e | 41
Task No.
5.1
6.1
6.2
7
9
Total Time
20 mins
5 mins
15 mins
70 mins
60 mins
170 mins
P a g e | 42
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
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
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 (&&)
P a g e | 44
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
the
P a g e | 45
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.
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.
P a g e | 46
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.
Walk-through Task
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
P a g e | 47
P a g e | 48
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.
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.
P a g e | 49
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
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.
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.
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
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.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
Grade is: B
Grade is: D
Grade is: F
Grade is: D
7.1
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
7.2.2
Confirmation
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).
P a g e | 51
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
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$
|| OPERATOR
! OPERATOR
a && b
a || b
!a
true False
False True
P a g e | 52