Chapter3 Control Instructions
Chapter3 Control Instructions
Ghulam Mohyuddin
Selection Statements
1. If Statement
2. If-else statement
3. Nested If
4. If…else (LADDER)
5. Switch statement
6. Conditional operator
Page 1 of 11
Programming Fundamentals | Chapter 3 Prof. Ghulam Mohyuddin
1. If Statement
Pseudo code:
If condition is true
Statements
Example:
If Ali’s height is greater than 6 feet
Then
Ali can become a member of Basket ball team
Syntax:
If (condition)
Statements; // single statement doesn’t need parenthesis
If (condition)
{
Statement1; // Group of statements need parenthesis
Statement2;
…
…
} // if statement ends here
Relational Operators
Relational operators are used to compare values of two expressions depending
on their relation.
Page 2 of 11
Programming Fundamentals | Chapter 3 Prof. Ghulam Mohyuddin
Logical NOT is a unary operator. Reverse the result. Returns FALSE if the
result is TRUE.
Page 3 of 11
Programming Fundamentals | Chapter 3 Prof. Ghulam Mohyuddin
Logical OR gives result FALSE, if both the conditions are FALSE, otherwise
result is TRUE
2. If-else statement
Syntax:
If (condition)
{
Statement;
--
--
}
Else
{
Statement;
--
--
}
Page 4 of 11
Programming Fundamentals | Chapter 3 Prof. Ghulam Mohyuddin
Flow Chart:
Page 5 of 11
Programming Fundamentals | Chapter 3 Prof. Ghulam Mohyuddin
Programming Practice
P-6: The current year and joining year of an employee is input through the
keyboard. Write a program to give the bonus of Rs.2500 if the service is greater
than 3 years.
P-7: The person’s age is input from the keyboard in years. Write a program to
check if a person can apply for the driving license if the person’s age is equal to
or greater than 18 years.
P-8: Enter the marks of five subjects from user. Write a program to calculate the
obtained marks, and percentage of obtained marks. Display the grades according
to the following criteria “80-100=A, 60-79=B, 50-59=C, percentage less than
50=fail”.
P-9: If cost price and selling price of an item is input through the keyboard. Write
a program to determine whether the seller has made profit or loss. Also
determine how much profit he made or loss he incurred.
P-10: Any integer is input through the keyboard. Write a program to find out
whether it is an even number or odd number.
P-11: Write a program to calculate the gross salary of an employee and basic
salary is input through the keyboard. If basic salary is less than 1500 then house
rent is 10% of basic and medical allowance is 20% of basic. If the basic salary is
Page 6 of 11
Programming Fundamentals | Chapter 3 Prof. Ghulam Mohyuddin
either equal to or above 1500 then house rent is Rs.1000 and medical allowance
is Rs.1200.
3. Nested If
Example:
4. If…else LADDER
In this type of nesting, there is an if…else statement in every else part except the
last part.
Page 7 of 11
Programming Fundamentals | Chapter 3 Prof. Ghulam Mohyuddin
Syntax:
if (condition1)
Statement1;
else if (condition2)
Statement2;
else if (condition3)
Statement3;
else
Statement4;
5. Switch Statement
It is the control statement that allows us to make a decision from the number of
choices.
Syntax:
switch (expression)
{
case constant1:
statement1;
case constant2:
statement2;
case constant3:
statement3;
default:
statement4;
}
Example:
int i=5;
switch (i) // 5
{
case 1:
cout<< “Pakistan”;
break; // end of current block/body
case 2:
cout<< “China”;
break;
Page 8 of 11
Programming Fundamentals | Chapter 3 Prof. Ghulam Mohyuddin
case 3:
cout<< “England”;
cout << “second statement”;
break;
default:
cout<< “USA”;
} // end of switch
6. Conditional operator
Here exp1 is evaluated first. If it is true exp2 will be executed. If false then exp3
will be executed.
Example:
OUTPUT: Value of s = 20
Page 9 of 11
Programming Fundamentals | Chapter 3 Prof. Ghulam Mohyuddin
Programming Practice
P-12: Write a program that input number of week’s day and display the name of
the day. For example, if the user enters 1, your program should display Friday
and so on. (1.Use switch statement, 2. Use if else)
P-13: Write a program that input two numbers and an operator (+,-,*,/). In case
the operator is +, addition of two numbers is calculated, for *, multiplication is
calculated, and so on. (1. use switch statement, 2. use if else)
P-14: Write a program to check whether a triangle is valid or not, while three
angles of the triangle is input through the keyboard. Note that a Triangle is valid,
if the sum of triangle is equal to 180 degree.
P-15: Write a program to calculate the roots of a quadratic equation, while value
of a, b and c is input through the keyboard. All cases should be discussed.
Page 10 of 11
Programming Fundamentals | Chapter 3 Prof. Ghulam Mohyuddin
Page 11 of 11