Sie sind auf Seite 1von 14

Mohammad Ali Jinnah University Islamabad

Department of Computer Science,


Faculty of Computing

Lab Manual for Computer Programming


Lab 6: Control Structures (Selection) and Relational Operators

Lab 6: Control Structures (Selection) and Relational Operators

Table of Contents
1.

Introduction

29

2.

Activity Time-boxing

29

3.

Objective of the Experiment

30

4.

Concept Map

30

4.1
4.2
4.3

30
30
31

5.

6.

7.

One way Selection


Two way Selection
Compound Statements

Home work before Lab

31

5.1
5.2

31
32

Problem solution modeling


Practices from home

Procedure & Tools

32

6.1
6.1
6.2

32
32
36

Tools
Setting-up Visual Studio 2008
Walk-through Task

Practice Tasks

38

7.1
7.2
7.3
7.4
7.5
7.6

38
38
38
38
38
39

Practice Task 1
Practice Task 2
Practice Task 3
Out comes
Testing
Evaluation Task (Unseen)

8.

Evaluation Criteria

39

9.

Further Readings

40

9.1
9.2

40
40

Books
Slides

Appendix A Relational Operators

40

Department of Computer Science,


MAJU, 2013

P a g e | 28

Lab 6: Control Structures (Selection) and Relational Operators

Lab 6: Control Structures (Selection) and Relational


Operators
1. Introduction
This lab will introduce you the concepts of how to use the Control Structures (selections) such as: if, ifelse, if-else-if. Furthermore, you will also learn the relational operators such as >, >=, <= etc. The
relational operators are frequently used in conditions to make number of decisions. There is another
important type of operators (logical operators) which are often used with conditions. However, the
current lab will not focus on this. The logical operators will be covered in the next lab.
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) Revise Lecture No. 5 and 6
b) Text Book: Computer Programming by D. S. Malik, Third edition
1. Read pages: 167-174 and 185 - 202
2. Read and understand the solved example starting at page 214.
3. Quick Review starting at page 219.

2. Activity Time-boxing
Task No.
5.1
6.2
6.3
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 with 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 | 29

Lab 6: Control Structures (Selection) and Relational Operators

3. Objective of the Experiment

To get basic understanding of selection control structures (if, if-else, if-else-if).


To practice different programming task using selections.
To get knowledge and to use the relational operators.

4. Concept Map
We will review some important concepts regarding selections (conditions) in this section. The selection
is used to control the execution of your program and to make number of decisions. You will learn along
with examples about this.
4.1 One way Selection

Sometimes, we are only interested to make a decision for example, acquiring absolute value of a
number. In this case we just need to convert the negative number to the positive. However, if the
number is already positive then we do not need to do anything.
if (number<0)
number= -1 * number;

4.2 Two way Selection

Apart from the one-way selection, we need to tackle both the cases. For example if the marks of a
student are greater than 50, then the student is passed otherwise the student is failed.
if (marks>50)
cout<<Passed;
else
cout<< failed;

You can use as many if-statements after the else part, for example, consider the following code:
if (marks>90)
cout<<Grade is A;
else if (marks>85)
cout<< Grade is B;
else
cout<< Fail;

The above statement will output the grade A for all students scoring more than 90 marks. However,
the grade of the students will be B who have scored more than 85 marks and less than 90, similarly, the
remaining students will get F grade. The statement will be executed in a way that first statement will be
checked, if the students marks are greater than 90, then the grade will be shown as A and the
remaining parts will not be executed (The else part). If the students marks are less than 90, then the first
statement would be evaluated as false, and the control will be passed to the else if (marks >85)
statement.

Department of Computer Science,


MAJU, 2013

P a g e | 30

Lab 6: Control Structures (Selection) and Relational Operators

4.3 Compound Statements

In the above statements, we have used only one statement that would be executed after the true of
false condition. However, most of the times, we need to execute multiple statements after the condition
evaluated as true or false. For example, consider the following statement:

if (marks>90)
cout<<Grade is A;
cout<<congratulations, you have performed excellent;

If the marks are more than 90, the program will display the following output:
Grade is A
Congratulations, you have performed excellent

However, suppose if the marks are less than 90, then guess what you will get:
Nothing .
Surprisingly. This will not happen; however, for the marks less than 90, the following would be printed
on screen:
Congratulations, you have performed excellent

This is due to the fact that c++ considers only one statement as associated with the condition, and the
statement
cout<<congratulations, you have performed excellent

is not part of the if-statement, and as the if has been evaluated as false therefore the above statement
will be executed
To tackle this situation, we need to do it in the following way.
if (marks>90)
{
cout<<Grade is A;
cout<<congratulations, you have performed excellent;
}

Now both statements will be associated with the if-statement. In case the if-statement is false, none of
the above statements will be executed.

5. Home work before Lab


5.1 Problem solution modeling

After reading the reference material mentioned in the introduction, now you are ready to
Department of Computer Science,
MAJU, 2013

P a g e | 31

Lab 6: Control Structures (Selection) and Relational Operators

design the solution of the following problems.


Follow your text book, and solve the problem no. 1 to 8 mentioned on the page number 226 and
227. Bring the solution with you and submit this to lab instructor.
Furthermore, solve Write the pseudo-code for the following problems:
1. Write a program that takes a number from the user and tells what is the equivalent month of
this entered number e.g. if the user enters 6 then system should display it is JUNE.

2. Write a program to input five number from user and check the number are divisible by 3 then
print the message on the screen that the number is divisible by three. Otherwise print The
number is not divisible by three.
5.2 Practices from home

1. Write a program that asks the user for two numbers and then print the maximum and minimum
number to the user.
2. Write a program that displays a menu to the user. Press 1 to find a sum of the numbers given by
the user Press 2 to find whether the number is even or odd

6. Procedure & Tools


6.1 Tools

Visual Studio 2008.


6.1 Setting-up Visual Studio 2008

[Expected time = 5 mins]

6.1.1 Open visual studio 2008.

1) Go to start menu.
2) Click on Microsoft Visual Studio Folder.
3) Now select Microsoft Visual Studio as shown in the Figure 1.

Figure 1: Selecting Microsoft Visual Studio


6.1.2

Open File Menu.

1) Go to File menu.
2) Select New, a sub-window will open as shown in the Figure 2.
Department of Computer Science,
MAJU, 2013

P a g e | 32

Lab 6: Control Structures (Selection) and Relational Operators

3) Select Project from the sub-window.

Figure 2: Opening File Menu

6.1.3 Select New Project.

1) Click on Project.
2) When you click on Project, a dialog box will open. On the dialog box, there are various options
like Windows Application, Console Application, DLL etc as shown in the Figure 3 (a).
3) From Application Type, Select Console Application as Figure 4 (b).
4) From additional options, select empty project as shown in the Figure 3 (b).

Figure 3 (a): Selecting New Project

Department of Computer Science,


MAJU, 2013

P a g e | 33

Lab 6: Control Structures (Selection) and Relational Operators

Figure 3 (b): Selecting New Project


6.1.4

Selection of source folder.

After the step 6.2.3, you will see a screen just like shown in the Figure 4.
There would be different options like Add, Cut, Copy etc. Click on Add and a submenu will open.

Figure 4: Selecting Source File Folder


6.1.5

Selection of Working File.

Various options would appear. Click on add New Item to select a working file as show in the Figure 5.

Department of Computer Science,


MAJU, 2013

P a g e | 34

Lab 6: Control Structures (Selection) and Relational Operators

Figure 5: Selecting New Item


6.1.6 Selection of cpp file.

From the various options, click on C++ File (.cpp) as shown in the Figure 6.

.
Figure 6: Select CPP file

6.1.7 Give the name to the file.

Write the name of your file in the Name field as shown in the Figure 7.

Department of Computer Science,


MAJU, 2013

P a g e | 35

Lab 6: Control Structures (Selection) and Relational Operators

Figure 7: Writing Name of the file

When you click Add button, A new file would be added to your existing project as shown in the Figure7.
6.2

Walk-through Task

[Expected time = 15 mins]

After completing 6.2 task, now you are ready for the specialized task. You need to practice the following
task to get basic understanding of developing a small program using selections and relational operators.
6.2.1 Writing Code

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

Figure 8: Writing the C++ code


Department of Computer Science,
MAJU, 2013

P a g e | 36

Lab 6: Control Structures (Selection) and Relational 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 9.

..
Figure 9: 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 10.

Figure 10: 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 10). 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 11.

Figure 11: Final output


Department of Computer Science,
MAJU, 2013

P a g e | 37

Lab 6: Control Structures (Selection) and Relational Operators

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 that asks the user to input 5 numbers and find its average. Once you find the average
then compare that average with all inputs and count the numbers which are greater than average.

7.2 Practice Task 2

[Expected time = 30 mins]

Write a program that takes the marks from the user and tells the grade corresponding to the marks. The
marks and corresponding grades are mentioned below:
Marks
Greater than 90
Greater than or equal to 86 and less than 90
Greater than or equal to 81 and less than 86
Greater than or equal to 77 and less than 81
Greater than or equal to 72 and less than 77
Greater than or equal to 68 and less than 72
Greater than or equal to 63 and less than 68
Greater than or equal to 58 and less than 63
Greater than or equal to 54 and less than 58
Greater than or equal to 50 and less than 54
Below 50

Grades
A
AB+
B
BC+
C
CD+
D
F

7.3 Practice Task 3

[Expected time = 20 mins]

Write a program which reads salaries of 10 employees of an organization. The program will tell, what
the maximum salary is and what the minimum salary is

7.4 Out comes

The outcomes of this lab were:


a) You have learnt control structures (selections) in C++
b) You have practiced different tasks how to use selections.
c) You have practiced different relational operators

7.5 Testing

Now you need to perform the following test cases for all practice tasks mentioned above. The test cases
have been made available in Table 2.

Department of Computer Science,


MAJU, 2013

P a g e | 38

Lab 6: Control Structures (Selection) and Relational Operators

Table 2: Test cases


Practice Tasks
6.3

Input
Num1 = 30
Num2= 5

Output
Line 1: a is not equal to b
Line 2: a is not less than b
Line 3: a is greater than b
Line 4: a is not equal to b

7.1

Num1=20
Num2 = 25
Num3= 63
Num4 = 80
Num5 = 95
Num6 = 85
Num7 = 79
Num8= 58
Num9=25
Num10=101
99
76
49
79
83
Salary1=50000
Salary2=45000
Salary3=39000
Salary4=99000
Salary5=12000
Salary6=58000
Salary7=54000
Salary8=25000
Salary9=10000
Salary10=10400

Average = 54
Total number greater than average = 6

7.2

7.3

7.6

Confirm

A
BF
B
B+
Maximum salary is = 99000
Minimum salary = 10000

Evaluation Task (Unseen)

[Expected time = 60 mins]

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

8. 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 | 39

Lab 6: Control Structures (Selection) and Relational Operators

Table 3: Evaluation of the Lab

Sr. No.
1
2
3
4
5
6

Task No
5.1
6
7 and 8
9

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

Marks
20
10
70
20
5
10

9. Further Readings
9.1 Books
Text Book:

C++ Programming by D.S Malik, Third Edition


Reference Books:

1. Beginning C++, the complete language, by Ivor Horton, Wrox Publishers.


2. How to Program in C++, Dietel and Dietel
9.2 Slides

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

Appendix A Relational Operators


Operator

Description

Example

==

Checks if the value of two operands is equal


or not, if yes then condition becomes true.

(A == B) is not true.

!=

Checks if the value of two operands is equal


or not, if values are not equal then condition
becomes true.

(A != B) is true.

>

Checks if the value of left operand is greater


than the value of right operand, if yes then
condition becomes true.

(A > B) is not true.

<

Checks if the value of left operand is less than


the value of right operand, if yes then
(A < B) is true.
condition becomes true.

>=

Checks if the value of left operand is greater


than or equal to the value of right operand, if (A >= B) is not true.
yes then condition becomes true.

<=

Checks if the value of left operand is less than


or equal to the value of right operand, if yes (A <= B) is true.
then condition becomes true.

Department of Computer Science,


MAJU, 2013

P a g e | 40

Das könnte Ihnen auch gefallen