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

Chapter3 Control Instructions

The document discusses different types of control instructions in programming including selection statements like if, if-else, nested if, switch, and conditional operators. It provides examples of using these statements to make programming decisions based on different conditions. Key topics covered include relational operators, logical operators, if-else ladder, and syntax for switch statements. Programming exercises are provided to practice applying these conditional control structures.

Uploaded by

Gulzar Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Chapter3 Control Instructions

The document discusses different types of control instructions in programming including selection statements like if, if-else, nested if, switch, and conditional operators. It provides examples of using these statements to make programming decisions based on different conditions. Key topics covered include relational operators, logical operators, if-else ladder, and syntax for switch statements. Programming exercises are provided to practice applying these conditional control structures.

Uploaded by

Gulzar Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Programming Fundamentals | Chapter 3 Prof.

Ghulam Mohyuddin

Control Instructions (Part-I)


1. Control instructions are based on decisions.
2. In programming, in some situations we need to make certain decisions.
To make decisions we need control instructions.
3. Example:
o If a person’s age is equal to or greater than 18 years, then he/she
can apply for driving license.
o If the height of the player is not less than 6 feet, then he can join
basket ball team.
o Minimum marks to qualify for the next class is 50%
o Bonus is granted on minimum 5 years of service.

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

1. < Less than


2. <= Less than or equal to
3. == Equal to
4. >= Greater than or equal to
5. > Greater than
6. != Not equal to

Logical / Boolean operators

 Logical operators are used with one or more operands.


 They return either value zero (false) or one (true).
 The operands may be constants, variables or expressions.
 The expression that combines two or more expressions is termed as logical
expression.

 C++ and JAVA both has three logical operators:


o && (Logical AND)
o || (Logical OR)
o ! (Logical NOT)

 Logical NOT is a unary operator. Reverse the result. Returns FALSE if the
result is TRUE.

 Logical AND , Logical OR are binary operators


 Logical AND gives result TRUE, if both the conditions are TRUE, otherwise
result is FALSE

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

Logical AND Logical OR


TRUE TRUE TRUE TRUE TRUE TRUE
TRUE FALSE FALSE TRUE FALSE TRUE
FALSE TRUE FALSE FALSE TRUE TRUE
FALSE FALSE FALSE FALSE FALSE FALSE

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

When there is another if…else statement in if-block or else-block, then it is called


nesting of if…else statement.

Example:

if (age > 18) // 15


{
if (height > 5)
{
Statement 1;
Statement 2;
Statement 3;
} //end inner if
} // end outer if

if (age > 18) && (height > 5)


{
Statement 1;
Statement 2;
Statement 3;

4. If…else LADDER

In this type of nesting, there is an if…else statement in every else part except the
last part.

If condition is false, the control is passed to block where condition is again


checked with its if statement

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;

This process continues until there is no if statement in the last block

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

It is also called as ternary operator. Since it requires three expressions as


operand and it is represented as ( ? : )

Syntax: condition? Choice1: choice2

Here exp1 is evaluated first. If it is true exp2 will be executed. If false then exp3
will be executed.

Example:

int a =20, b=10;


int s = (a>b) ? a : b; //20
cout << “Value of s = ” << s; //20

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.

(b2-4ac) > 0 , (b2-4ac) < 0 , (b2-4ac) = 0

Page 10 of 11
Programming Fundamentals | Chapter 3 Prof. Ghulam Mohyuddin

Page 11 of 11

You might also like