Computer Programming 03
Computer Programming 03
PROGRAMMING
Lecture 03
Conditions
(if, if/else, nested if/else)
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(boolean_expression)
{ // statement(s) will execute if the boolean
expression is true }
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)
Pseudocode
If student’s grade is greater than or equal to 60
Print “Passed”
C++ code
if ( grade >= 60 )
cout << "Passed";
true
grade >= 60 print “Passed”
false
Input is 35 and 30
Input is 25 and 25
Input is 10 and 20
false true
Boolean expression
if (condition)
if (condition)
{
true_statement;
……
else
}
false_statement;
else
{
……
}
false true
grade >= 60
braces,
Without
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. }
Input is 100
100 > NORM
Input is 98
98<NORM
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. }
#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;
}
Sales is 1500
Pseudocode
Else
Gross pay = rate * hours
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. }
Hour<40
Hour>40
the value in the conditional can also be actions to execute for example
grade >= 60 ? cout << “Passed” : cout <<
“Failed” ;
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
Flow chart
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.