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

Computer Programming 03

cout << "Hello"; if (condition) { cout << "Hello"; cout << "World"; } Compound statements: if (condition) { cout << "Hello"; cout << "World"; } Single statements are placed directly after the if or else keywords. Compound statements use curly braces to group multiple statements.

Uploaded by

Salman shakeel
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Computer Programming 03

cout << "Hello"; if (condition) { cout << "Hello"; cout << "World"; } Compound statements: if (condition) { cout << "Hello"; cout << "World"; } Single statements are placed directly after the if or else keywords. Compound statements use curly braces to group multiple statements.

Uploaded by

Salman shakeel
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

COMPUTER

PROGRAMMING

Lecture 03
Conditions
(if, if/else, nested if/else)

Dr. Muniba Ashfaq


TODAY’S AGENDA
 if statements
 Syntax
 Flowchart
 Examples
 If/else statements
 Syntax
 Flowchart
 Examples
 Nested if/else statements
 Syntax
 Flowchart
 Examples

Dr. Muniba Ashfaq 2


FLOW CHART
 Graphical representation of an algorithm or a portion of algorithm.

ovals or rounded rectangles, indicate "Start, begin”


or "End“.

Rectangles are action symbols to indicate any type


of action, including a calculation or an input
output operation.

Diamonds are conditional or decision symbols. It


indicates a decision to be made.

Parallelogram are the input/output symbols

Arrows, showing what's called “flow of control”.


Arrows are also called flow lines.
Dr. Muniba Ashfaq 3
CONTROL STRUCTURES
 Control structure
 A control statement is any statement that
alters the linear flow of control of a program.
C++ examples include: if-else, while, for,
break, continue and return statement
 Sequential execution
 Statements executed in order
 Transfer of control
 Next statement to be executed may be other
than the next one in sequence.

Dr. Muniba Ashfaq 4


CONTROL STRUCTURES
 Three control structures

1.Sequence structures
oBuilt into C++. Programs executed
sequentially by default.

2.Selection structures
oC++ has three types - if, if/else, and switch
3.Repetition structures.
oC++ has three types while, do/while, for
Dr. Muniba Ashfaq 5
CONTROL STRUCTURES
 if selection structure
. Perform an action if condition is true.
. Skip the action if condition is false.

 If/else selection structure


. Perform an action if condition is true.
. Performs a different action if the condition is false.

 switch selection structure


. Perform one of many different actions depending on
the value of an integer expression.

Dr. Muniba Ashfaq 6


C++ IF STATMENT
 An if statement consists of a boolean
expression followed by one or more
statements.
 Syntax

if(boolean_expression)
{ // statement(s) will execute if the boolean
expression is true }

Dr. Muniba Ashfaq 7


IF SELECTION STRUCTURE
FLOW CHART

Dr. Muniba Ashfaq 8


QUICK EXAMPLE
#include <iostream>
using namespace std;
int main ()
{
int a = 10; // local variable declaration:

if( a < 20 ) // check the boolean condition


{
cout << "a is less than 20;" << endl; // if condition is true
then print
}
cout << "value of a is : " << a << endl;
return 0;
}

Dr. Muniba Ashfaq 9


LOGIC OPERATORS IN ‘IF’

 Equal to if (x==10)
 Not equal to if (x!=10)
 Less than if (x<10)
 Greater than if (x>10)
 Less than / equal to if (x<=10)
 Greater than / equal to if (x>=10)

Dr. Muniba Ashfaq 10


COMPOUND OPERATORS
 Logical AND if (x==1 && y==2)
 Logical OR if (x==1 || y==2)
 Logical NOT if (!x) …

Dr. Muniba Ashfaq 11


IF SELECTION STRUCTURE
example:

Pseudocode
If student’s grade is greater than or equal to 60
Print “Passed”

C++ code
if ( grade >= 60 )
cout << "Passed";

Dr. Muniba Ashfaq 12


IF SELECTION STRUCTURE
 Flow chart for the pseudocode statement.

A decision can be made on


any expression.

true
grade >= 60 print “Passed”

false

Dr. Muniba Ashfaq 13


1 // example
2 // Using if statements, relational
3 // operators, and equality operators
4 #include <iostream.h>
5
6
7
8
9
10 int main()
11 {
12 int num1, num2; // declare variables
13
14 cout << "Enter two integers, and I will tell you\n"
if structure compares values
15 << "the relationships they satisfy: ";
of num1 and num2 to test for If condition is true (i.e.,
16 cin >> num1 >> num2; // read two integers values are equal), execute this
17 equality.
if structure compares values statement.
18 if ( num1 == num2 )
of num1 and num2 toIf condition
test for is true (i.e.,
19 cout << num1 << " is equal to " << num2 << endl; values are not equal), execute
20 inequality.
this statement.
21 if ( num1 != num2 ) If structure compare the is true (i.e., num1
If condition
not equal to values of num1is and
less num2 to ), execute
22 cout << num1 << " is " << num2 << endl;
23
than num2
test if num1 isthis
lessstatement.
than num2
24 if ( num1 < num2 ) if structure compares
If condition values
is true (i.e., num1
25 cout << num1 << " is less than " of<<num1
num2 and num2
<< endl; to test if num2 ),
26
is greater than
num1 is compares
if structure greater than
execute num2
valuesthis statement.
27 if ( num1 > num2 )
28 cout << num1 << " is greater of num1
than " <<andnum2
num2<< to test ifIf condition is true (i.e., num1
endl;
29 num1 is less than or equal to is less than or equal to num2),
30 if ( num1 <= num2 ) num2 execute this statement.
31 cout << num1 << " is less than or equal to "
32 << num2 << endl; //continued on next slide
Dr. Muniba Ashfaq 14
33
34 if ( num1 >= num2 )
35 cout << num1 << " is greater than or equal to "
36 << num2 << endl;
37
38 return 0; // indicate that program ended successfully
39 }

Input is 35 and 30

Input is 25 and 25

Input is 10 and 20

Dr. Muniba Ashfaq 15


C++ IF ELSE STATEMENT
 An if statement can be followed by an
optional else statement, which executes
when the boolean expression is false.
Syntax:
if(boolean_expression)
{
// statement(s) will execute if the boolean expression is
true }
else
{
// statement(s) will execute if the boolean expression is
false }

Dr. Muniba Ashfaq 16


IF/ELSE SELECTION STRUCTURE
FLOW CHART

false true
Boolean expression

Action if false Action if true

Dr. Muniba Ashfaq 17


QUICK EXAMPLE
#include <iostream>
using namespace std;
int main ()
{
int a = 100; // local variable declaration:
if( a < 20 ) // check the boolean condition
{
cout << "a is less than 20;" << endl; // if condition is true then print
}
else
{cout << "a is not less than 20;" << endl; // if condition is false then
print
}
cout << "value of a is : " << a << endl;
return 0;
}

a is not less than 20;


value of a is : 100
Dr. Muniba Ashfaq 18
SINGLE AND COMPOUND
STATEMENTS
Single statements: Multiple statements:

if (condition)
if (condition)
{
true_statement;
……
else
}
false_statement;
else
{
……
}

Dr. Muniba Ashfaq 19


IF/ELSE SELECTION
STRUCTURE
Example
 Pseudocode
if student’s grade is greater than or equal to 60
print “Passed”
else
print “Failed”
 C++ code
if ( grade >= 60 )
cout << "Passed";
else
cout << "Failed";

Dr. Muniba Ashfaq 20


IF/ELSE SELECTION
STRUCTURE
Flow chart for the pseudocode

false true
grade >= 60

print “Failed” print “Passed”

Dr. Muniba Ashfaq 21


COMPOUND STATEMENT
 Compound statement
 Set of statements within a pair of braces also known as BLOCK
if ( grade >= 60 )
cout << "Passed.\n";
else {
cout << "Failed.\n";
cout << "You must take this course again.\
n";
}

braces,
 Without

cout << "You must take this course


again.\n";
always executed

Dr. Muniba Ashfaq 22


ANOTHER SIMPLE
EXAMPLE
1. //program to determine if user is ill

2. #include <iostream>
3. using namespace std;
4. int main()
5. {
6. const double NORM = 98.6; // degree Fahranheit;
7. double temperature;
8. cout<<"Enter your temperature\t";
9. cin>>temperature;

10. if (temperature>NORM) //if temperature is greater than NORM print following statement
11. cout<<"\n\nYou are sick\n"<<"take rest and drink lots of fluids";
12. else //if temperature is <= NORM print following statement
13. cout<<"\n\nYou are perfectly fine";

14. return 0;
15. }

Dr. Muniba Ashfaq 23


OUTPUT

Input is 100
100 > NORM

Input is 98
98<NORM

Dr. Muniba Ashfaq 24


EXAMPLE OF IF AND IF/ELSE SELECTION
STRUCTURE DRAWN IN FLOW CHART FORM

If selection structure(program 1) If/else selection structure(program2)

Dr. Muniba Ashfaq 25


C++ CODE FOR THE FLOW CHART OF IF SELECTION
STRUCTURE (PROGRAM 1).
1. //program 1
2. //program to find the part prices for the given part number.

3. #include<iostream>
4. using namespace std;
5. int main()
6. {
7. int part_number, ;
8. float price;
9. cout<<"enter part number"<<endl;
10. cin>>part_number;
11. cout<<"enter price"<<endl;
12. cin>>price;
13. if(part_number==203)
14. price = price*1.1;
15. cout<<"price is "<<price;
16. return 0;
17. }

Dr. Muniba Ashfaq 26


OUTPUT OF PROGRAM 1

Part_number is not equal to 203

Part_number is equal to 203

Dr. Muniba Ashfaq 27


C++ CODE FOR THE FLOW CHART OF IF/ELSE
SELECTION STRUCTURE (PROGRAM 2).

 //program to calculate the sales commission

 #include <iostream>
 using namespace std;
 int main()
 {
 float sales, commission;
 cout<<"enter the sales"<<endl;
 cin>>sales;
 if (sales>1500)
 commission = sales*0.2;
 else
 commission = sales*0.1;
 cout<<"commission is"<<commission;

 return 0;
 }

Dr. Muniba Ashfaq 28


OUTPUT OF PROGRAM 2

Sales is 1500

Sales is greater than 1500

Dr. Muniba Ashfaq 29


EXAMPLE OF IF/ELSE
 Write a program to calculate the gross pay of an employee.
 The employee is paid at his basic hourly rate for the first 40 hours worked
during a week. Any hours worked beyond 40 is considered overtime.
 overtime is paid at the 1.5 times the hourly rate.

Pseudocode

If total number of hours >40


Gross pay = rate*40 + rate*1.5(hours – 40)

Else
Gross pay = rate * hours

Dr. Muniba Ashfaq 30


C++ CODE
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int hour; //declaration
6. double gross_pay, rate;
7. cout<<"enter the hourly rate of pay\t"<<endl; //prompt
8. cin>>rate; //reading data
9. cout<<"enter the number of hours worked \t"<<endl; //prompt
10. cin>>hour;//readin data

11. if(hour>40) //condition if working hours is greater than 40


12. gross_pay = rate*40 + 1.5*rate*(hour - 40); //gross pay including extra hour
13.

14. else//if working hour is <=40 then use this formula to get gross pay
15. gross_pay = rate*hour;
16.

17. cout<<" Gross pay is \t"<<gross_pay<<endl; //printing the gross pay on screen.
18. return 0;
19. }

Dr. Muniba Ashfaq 31


OUTPUT

Hour<40

Hour>40

Dr. Muniba Ashfaq 32


IF/ELSE SELECTION
STRUCTURE
 Ternary conditional operator (?:)
 It is only ternary operator.
 It takes three operands together with conditional operator to form a
conditional expression
 Three arguments (condition, value if true, value if false)
 example
cout << ( grade >= 60 ? “Passed” : “Failed” );

Condition Value if true Value if false

 the value in the conditional can also be actions to execute for example
grade >= 60 ? cout << “Passed” : cout <<
“Failed” ;

Dr. Muniba Ashfaq 33


NESTED IF STATEMENT
Syntax:
if( boolean_expression 1)
{
// Executes when the boolean expression 1 is true
if(boolean_expression 2)
{
// Executes when the boolean expression 2 is
true
}
}

Dr. Muniba Ashfaq 34


QUICK EXAMPLE
#include <iostream>
using namespace std;
int main ()
{ int a = 100;
int b = 200; // local variable declaration:
if( a == 100 ) // check the boolean condition
{
if( b == 200 ) // if condition is true then check the following
{
cout << "Value of a is 100 and b is 200" << endl;
// if condition is true then print
}
}
cout << "Exact value of a is : " << a << endl;
cout << "Exact value of b is : " << b << endl;
return 0; }

Value of a is 100 and b is 200


Exact value of a is : 100
Exact value of b is : 200
Dr. Muniba Ashfaq 35
EXAMPLE
#include <iostream>
using namespace std;
int main()
{
int number;
cout<< "Enter an integer: ";
cin>> number;
if ( number > 0)
{
cout << "You entered a positive integer: "<<number<<endl;
}
else if (number < 0)
{ cout<<"You entered a negative integer: "<<number<<endl; }
else
{ cout<<"You entered 0."<<endl;
}
cout<<"This statement is always executed because it's outside nested if..else statement.";
return 0; }

Enter an integer: 0
You entered 0.
This statement is always executed because it's outside nested if..else
statement
Dr. Muniba Ashfaq 36
SIMPLE EXAMPLE TO UNDERSTAND THE NESTED
IF/ELSE
 Consider an example of a program segment that accepts a gender
code and print gender according to that code.

 Pseudocode
if the gender code is F
print female
else
if the gender code is M
print male
else
print invalid code

Dr. Muniba Ashfaq 37


SIMPLE EXAMPLE TO UNDERSTAND THE NESTED
IF/ELSE

 Flow chart

Dr. Muniba Ashfaq 38


SIMPLE EXAMPLE TO UNDERSTAND THE NESTED
IF/ELSE
 C++ code

//F and M are correct codes


//f and m are invalid codes

Dr. Muniba Ashfaq 39


NESTED IF/ELSE
STRUCTURE
 following pseudocode is example of nested if/else
 if student’s grade is greater than or equal to 90
Print “A”
 else
if student’s grade is greater than or equal to 80
Print “B”
else
if student’s grade is greater than or equal to 70
Print “C”
else
if student’s grade is greater than or equal to 60
Print “D”
else
 Print “F”

Dr. Muniba Ashfaq 40


NESTED IF/ELSE
STRUCTURE
C++ code
if ( grade >= 90 ) // 90 and above
cout << "A";
else

if ( grade >= 80 ) // 80-89


cout << "B";
else

if ( grade >= 70 ) // 70-79


cout << "C";
else

if ( grade >= 60 ) // 60-69


cout << "D";
else // less than 60
cout << "F";

 if grade>=90, first four conditions will be true. But only the cout statement after
the first test will be executed. After that cout is executed, the else part of the
outer if/else statement is skipped.

Dr. Muniba Ashfaq 41

You might also like