Professional Documents
Culture Documents
Table of Contents
1. Introduction ............................................................................................................................................... 3
2. Objectives................................................................................................................................................... 3
3. Concept Map .............................................................................................................................................. 3
3.1. One way Selection.............................................................................................................................. 3
3.2. Two way Selection ............................................................................................................................. 3
3.3. Compound Statements ...................................................................................................................... 4
3.4. Nested-if statement ........................................................................................................................... 4
3.5. Similarities and Differences for the use of nested-if and logical-and ................................................ 5
4. Procedure & Tools...................................................................................................................................... 7
4.1. Tools ................................................................................................................................................... 7
4.2. Setting-up Visual Studio 2012 ............................................................................................................ 7
5. Walk-through Task ..................................................................................................................................... 7
5.1. Writing Code ...................................................................................................................................... 7
5.2. Compilation ........................................................................................................................................ 7
5.3. Executing the Program ....................................................................................................................... 8
7. Practice Tasks ................................................................................................................................................. 9
7.1 Practice Task 1 .......................................................................................................................................... 9
7.2 Practice Task 2 .......................................................................................................................................... 9
7.3 Practice Task 3.......................................................................................................................................... 9
7.4 Practice Task 4 .......................................................................................................................................... 9
7.7 Out comes ................................................................................................................................................ 9
7.8 Testing .................................................................................................................................................... 10
8. Evaluation Task............................................................................................................................................ 10
9. Further Readings .......................................................................................................................................... 10
10. Slides .......................................................................................................................................................... 11
Page 2 of 11
Lab 4: Selection Structure and nested If-else
2. Objectives
• To get basic understanding of if else conditions
• Where and when to use ‘nested-if statements.
• To practice how the selection statements and nested-if are used in a C++ program.
3. Concept Map
We will review some important concepts regarding selections (conditions) in this lab manual. 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. We will also introduce the nested-if condition and furthermore, we will study
where and when to use these concepts in C++ program.
if (number<0)
number= -1 * number;
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”;
Page 3 of 11
Lab 4: Selection Structure and nested If-else
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.
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:
Figure 1: Output
However, suppose if the marks are less than 90, then guess what you will get, Nothing will happen. Because
the second part “Congratulations, you have performed excellent” is not part of if conditions so to include
it in if statement block we have to write code in the following way.
Figure 2: Code
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.
Page 4 of 11
Lab 4: Selection Structure and nested If-else
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.
3.5. Similarities and Differences for the use of nested-if and logical-and
The example used in the section Error! Reference source not found. can be represented using logical o
perators following is an example of this.
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.
Page 5 of 11
Lab 4: Selection Structure and nested If-else
if (percentage>=70 )
{
if (age <16)
{
cout<<”Congratulations! You are eligible for the scholarship”;
}
cout<<”Your percentage is more than 70”; }
However, you probably, will not like to write the above code 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”;
Page 6 of 11
Lab 4: Selection Structure and nested If-else
}
} else {
cout<<”Sorry, you did not get the scholarship”;
}
5. Walk-through Task
5.1. Writing Code
Open the Visual Studio and in new project write the following code as shown in the Figure 3 Write the code
in the intended form as shown. .
5.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 4
Page 7 of 11
Lab 4: Selection Structure and nested If-else
..
Figure 4: 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 5
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”.
Page 8 of 11
Lab 4: Selection Structure and nested If-else
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
Marks Grades
Greater than 90 A
Greater than or equal to 86 and less than 90 A-
Greater than or equal to 81 and less than 86 B+
Greater than or equal to 77 and less than 81 B
Greater than or equal to 72 and less than 77 B-
Greater than or equal to 68 and less than 72 C+
Greater than or equal to 63 and less than 68 C
Greater than or equal to 58 and less than 63 C-
Greater than or equal to 54 and less than 58 D+
Greater than or equal to 50 and less than 54 D
Below 50 F
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.
Page 9 of 11
Lab 4: Selection Structure and nested If-else
7.8 Testing
For the above tasks following are the test cases. The instructor will check the outputs of the practice tasks
accordingly.
Table 1: Testing of Practice Tasks
9. Further Readings
9.1. Books
Text Book:
Computer Programming by D.S Malik, second edition
Reference Books:
Page 10 of 11
Lab 4: Selection Structure and nested If-else
10. Slides
The slides are available on Moellim ( http://moellim.riphah.edu.pk/ )
Page 11 of 11