SlideShare a Scribd company logo
1 of 58
PROGRAM CONTROL
PROGRAM CONTROL STRUCTURE
• Program control structure controls the flow of
execution of program statement.
• C++ provides structures that will allow an
instruction or a block of instruction to be
executed, repeated and skipped.
• There are three control structure:
 sequential structure
 selection structure
 repetition structure
Sequence structure
• The sequence structure has one entry point
and one exit point.
• No choice are made and no repetition.
• Statement are executed in sequence, one
after another without leaving out any single
statement.
flowchart
Statement 1
Statement 2
Statement n
Example of sequential statement
#include<iostream>
using namespace std;
int main()
{
float payrate=8.5;
float hourWorked=25.0;
float wages;
wages= hourWorked * payrate;
cout<<“n Wages = RM:”<<wages<<endl;
return 0;
}
Output:
Wages = RM:212.5
Example of sequential statement
include<iostream>
using namespace std;
int main()
{
int num1,num2;
float total;
cout<<“NUMBER 1:”;
cin>>num1;
cout<<“NUMBER 2:”;
cin>>num2;
total=num1+num2;
cout<<“TOTAL OF TWO NUMBERS IS:”<<total<<endl;
return 0;
}
Output:
NUMBER 1:10
NUMBER 2:5
TOTAL OF TWO NUMBERS IS:15
Selection structure
• The selection structure is used to allow
choices to be made.
• This program executes particular statements
depending on some condition(s).
• C++ uses if..else, nested if and switch..case
statement for making decision.
Selection statement
If…else
• A selection of control structure is used to select
between two or more options.
• Used to make decision based on condition.
• else statement will never exist in a program
without if statement.
• If tested condition is TRUE, the entire block of
statements following the if is performed.
• If the tested condition is FALSE, the entire block
of statement following the else is performed
flowchart
false true
condition
Statement 2 Statement 1
syntax
if(condition)
{
true statement
}
else
{
false statement
}
Example of if..else statement
#include<iostream>
using namespace std;
int main()
{
int age;
cout<<“n Key in your age:”<<age;
cin>>age;
if(age>21)
{
cout<<“Qualifiied to have driving license”;
}
else
{
cout<<“Not qualified to have driving license”;
}
return 0;
}
Output:
Key in your age:18
Not qualified to have driving license
Output:
Key in your age:25
Qualifiied to have driving license
Output:
Key in your age:21
Not qualified to have driving license
Nested if..else
condition1
condition2
condition3
Statement A
Statement C
Statement D
true
false
true
false
true
Statement B
false
Nested if..else
If(condition 1)
{
If(condition 2)
{
If(condition 3)
{
Statement A
}
else
{
Statement B
}
}
else
{
Statement c
}
}
else
{
Statement D
}
• In this nested form, condition 1 is evaluated. If it
is zero, statement D is executed and the entire
nested if statement is terminated.
• In not, control goes to the second if and condition
2 is evaluated. If it is zero, statement C is
executed.
• If not, control goes to the third if and condition 3
is evaluated. If it is zero, statement B is executed .
• If not , statement A executed.
Example of statement#include<iostream>
using namespace std;
int main()
{
float cgpa,salary;
int year;
cout<<“n YEAR:”;
cin>>year;
cout<<“n CGPA:”;
cin>>cgpa;
cout<<“n SALARY:”;
cin>>salary;
if(year>1)
{
if(cgpa>=3.00)
{
if(salary<=500)
{
cout<<“Your application is under consideration”;
}
else
{
cout<<“Not success because salary is more than RM500.00“;
}
}
else
{
cout<<“ Not success because cgpa is less than 3.00”;
}
}
else
{
cout<<“ Not success because yeaa of study is less than 1”;
}
return 0;
Output:
YEAR:2
CGPA:3.5
SALARY:250
Your application is under consideration
Nested if..else
condition1
condition2
condition3
Statement D
Statement B
Statement A
false
true
false
true
false
Statement C
true
Nested if..else
if(condition 1)
{
Statement A
}
else if(condition 2)
{
Statemnet B
}
else if(condition 3)
{
Statement C
}
else
{
Statement D
}
Example of if..else statement#include<iostream>
using namespace std;
int main()
{
float cgpa;
cout<<“n CGPA:”;
cin>>cgpa;
if(cgpa < 0.00 || cgpa > 4.00)
{
cout<<“Invalid data”;
{
else if(cgpa > = 3.5 && cgpa < = 4.00)
{
cout<<“Dean List”;
}
else if(cgpa > 2.00 && cgpa < 3.50.00)
{
cout<<“Pass”;
}
else if(cgpa > = 1.80 && cgpa < 2.00)
{
cout<<“ Conditional Pass;;
}
else
{
cout<<“ Fail”;
}
return 0;
}
Output:
CGPA:2.5
Pass
Usage of Logical Operator
Symbol
(pseudocode)
Symbol
(c++ language)
Example Result Description
AND && (1>3)&&(10<20) FALSE Both sides of
the condition
must be true.
OR || (1>3)&&(10<20) TRUE Either one of
the condition
must be true.
NOT ! !(10<20) FALSE Change the
operation
either from
true to false or
vise versa.
Example using of AND operator
● If condition ( num1< num2) is true AND condition ( num1 > num3) is true
then print “First number is the largest number” will be displayed.
● If one of the condition is not true then print “First number is the largest
number” will not be displayed.
If ((num1 > num2 ) && ( num1 > num3 ))
cout<<“First number is the largest number”;
Example usage of OR operator:
● if the sales are more than 5000 or working hours
are more than 81, bonus RM500 will be given. If
either condition is not fulfilled, still the amount of
RM500 will be given as bonus.
● If both condition are false, then bonus will not be
given.
If ( ( sales > 5000 ) || (hourseworked > 81 ))
Bonus = 500;
else
Switch case
• The switch..case structure consists of a series of case label
and optional default case which can be included for option
that is not listed in the case label.
• Switch which is followed by variable name in parentheses
called controlling expression.
• The break keyword must be included at the end of each
case statement.
• The break statement causes program control to proceed
with the first statement after the switch structure.
• Without break statement, all command for that case and
next case label will be executed.
• Default is an option that will only be executed if suitable
case cannot be found.
Flowchart
Case 1
Case n
default
Statement for value=1 break
break
Statement for value=1
syntax
Switch(expression)
{
case value 1:
statement if expression =value 1
break;
case value n:
statement if expression =value n
break;
default:
statement if none of the expression is matched
}
Example of program
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<“n Enter a number:”;
cin>>num;
switch(num)
{
case 1:
cout<<“Welcome”;
break;
case 2:
cout<<“Bye”;
break;
default:
cout<<“Wrong Number”;
}
return 0;
Output:
Enter a number:1
Welcome
Example of program without break
statement
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<“n Enter a number:”;
cin>>num;
switch(num)
{
case 1:
cout<<“Welcome”;
case 2:
cout<<“Bye”;
default:
cout<<“Wrong Number”;
}
return 0;
Output:
Enter a number:1
WelcomeByeWrong Number
Example of program
#include<iostream>
using namespace std;
int main()
{
char grade;
cout<<“n Enter a grade (A-D):”;
cin>>grade;
switch(grade)
{
case ‘A’:
cout<<“Minimun marks is 80”;
break;
case ‘B’:
cout<<“Minimun marks is 60”;
break;
case ‘C’:
cout<<“Minimun marks is 40”;
break;
case ‘D’:
cout<<“Minimun marks is 25”;
break;
default:
cout<<“Mark between 0-24”;
}
return 0;
}
Output:
Enter a grade (A-D):C
Minimun mark is 40
• A,B,C, and D are the possible values that can be assigned to the
variable grade.
• In each case of constant, A to D a statement or sequence of
statement would be executed.
• You would have noticed that every line has statement under it
called break.
• The break is the only thing that stop a case statement from
continuing all the way down through each case label under it.
• In fact if you do not put break in, the program will keep going down
in the case statement. Into other case labels, until it reaches the
end of break.
• However the default part is the codes that would be executed if
there were no matching case of constant’s value.
Rules for Switch statement
• The value of case statement must be an integer or a
character constant.
• Case statement arrangement is not important.
• Default can exist earlier ( but compiler will place it at
the end)
• Can’t use condition or limit.
• Why switch is the best way to write a program:
 easy to read
 It is a more spesific statement to implement multiple
alternative decisions.
 It is used to make a decision between many alternative.
Comparison of Nested if statement
and switch..case statement
Nested if statement switch.. case statement
#include<iostream>
using namespace std;
int main()
{
int code;
cout<<“Enter code (1,2 or 3)”;
cin>>code;
if(code==1)
cout<<“Your favourite flavor is vanila”;
else if(code==2)
cout<<“Your favourite flavor is chocolate”;
else if(code==3)
cout<<“Your favourite flavor is strawbery”
else
cout<<“please choose code 1,2 or 3 only”;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int code;
cout<<“Enter code (1,2 or 3)”;
cin>>code;
switch(code)
{
case1:
cout<<“Your favourite flavor is vanila”;
break;
case2:
cout<<“Your favourite flavor is chocolate”;
break;
case 3:
cout<<“Your favourite flavor is strawbery”;
default:
cout<<“please choose code 1,2 or 3 only”;
}
return 0;
}
exercise
1. If the payment RM5,000 to RM10,000, display a message that the
customer will be given a free ticket to Langkawi. Otherwise, display a
message that the customer will only be given a free lunch at Quality
Hotel. Translate the statement above to program segment.
2. You are given the following requirement:
a. Your program is able to read the amount of sales for a sale executive.
b. If the sale is more than RM10,000 then the commission will be 5% of the
sale. Otherwise, the commission is only 3% of the sales.
c. Your program is able to calculate the amount of commission by multiply the
percentage of commission with sale.
d. Your program is able to display the amount of commission to the sale
executive.
3. Write a c++ program to find a maximum value of three integers which are
a, b and c.
exercise
4. You’re given the following requirements:
a. Your program is able to read the item code from the keyboard
b. The item code determines the price for each item in the shop. The following table shows
the prices and charges of 7 items in the shop.
c. If the item code is not matched, an error message is dispayed as follows:
“Error, this item code is not in the list.”
d. For each selected item, the charge is calculated by multiplying the item price and charge rate.
e. Then the payment is a summation of item price and charge. Your program is to display this
payment.
Item Code Price (RM) Charge(%)
A 54.00 5
B 65.00 5
C 82.00 5
D 103.00 7
E 150.00 7
F 245.00 10
H 250.00 10
exercise
4. NASA Consultant Co Ltd wants to develop a program that can help the consultant
to give advice regarding the home loan. The program receives two inputs from the
consultant, which are the amount of House Cost in Ringgit Malaysia and years of
term applied. Based on these two inputs, the program should be able to calculate
the monthly repayment. The table for the interest calculation is provided as follows:
The formulae for the monthly repayment calculation are as follows:
Interest (RM) = Interest (%) x House cost (RM)
House cost with Interest = House Cost (RM) + Interest (RM)
Monthly Repayment = House Cost with Interest / ( Year of Term Applied x 12)
Year of Term Applied Interest (%)
More than or equal to 25 years 7.5
More than or equal to 20 years 6.5
More than or equal to 15 years 5.5
More than or equal to 10 years 4.5
Less than 10 years 4.0
Answer for question 1
(if else)
#include<iostream>
using namespace std;
int main()
{
float payment;
cout<<"Please enter your payment:RM";
cin>>payment;
if(payment>=5000&&payment<=10000)
{
cout<<"Free air ticket to Langkawi";
}
else
{
cout<<"Free lunch at Quality Hotel";
}
return 0;
Answer for question 1
(nested if)#include<iostream>
using namespace std;
int main()
{
float payment;
cout<<"Please enter your payment:RM";
cin>>payment;
if(payment>=5000)
{
if(payment<=10000)
cout<<"Free air ticket to Langkawi";
else
cout<<"Free lunch at Quality Hotel";
}
else
cout<<"Free lunch at Quality Hotel";
return 0;
}
Answer for question 1
if and if else#include<iostream>
using namespace std;
int main()
{
float payment;
cout<<"Please enter your payment:RM";
cin>>payment;
{
if(payment>10000)
{
cout<<"Free lunch at Quality Hotel";
cout<<"ccc";
}
}
{
if(payment>=5000)
{
cout<<"Free air ticket to Langkawi";
cout<<"hiii";
}
else
{
cout<<"Free lunch at Quality Hotel";
cout<<"bye";
}
}
return 0;
}
Answer for question 2
#include<iostream>
using namespace std;
int main()
{
float sale,commission,amount_commission;
cout<<"Please enter your sale:RM";
cin>>sale;
if(sale>10000)
{
commission=0.05;
}
else
{
commission=0.03;
}
amount_commission=sale*commission;
cout<<"Your amount of commission is:RM"<<amount_commission;
return 0;
}
Answer for question 2
#include<iostream>
using namespace std;
int main()
{
float sale,commission,amount_commission;
cout<<"Please enter your sale:RM";
cin>>sale;
if(sale>10000)
{
commission=0.05;
amount_commission=sale*commission;
cout<<"Your amount of commission is:RM"<<amount_commission;
}
else
{
commission=0.03;
amount_commission=sale*commission;
cout<<"Your amount of commission is:RM"<<amount_commission;
}
return 0;
}
Answer question 5
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"ENTER FIRST NUMBER:";
cin>>a;
cout<<"ENTER SECOND NUMBER:";
cin>>b;
cout<<"ENTER THIRD NUMBER:";
cin>>c;
cout<<"n*****************************n";
if(a>b&&a>c)
{
cout<<"MAXIMUM VALUE IS:"<<a;
}
else if(b>a&&b>c)
{
cout<<"MAXIMUM VALUE IS:"<<b;
}
else
{
cout<<"MAXIMUM VALUE IS:"<<c;
}
return 0;
}
Answer for question 6
#include<iostream>
using namespace std;
int main()
{
char code;
float price,charge_rate,charge,payment;
cout<<"ITEM CODE:";
cin>>code;
switch(code)
{
case 'A':price=54.00;
charge_rate=0.05;
charge=price*charge_rate;
payment=price+charge;
cout<<"Your payment is:RM"<<payment;
break;
case 'B':price=65.00;
charge_rate=0.05;
charge=price*charge_rate;
payment=price+charge;
cout<<"Your payment is:RM"<<payment;
break;
case 'C':price=82.00;
charge_rate=0.05;
charge=price*charge_rate;
payment=price+charge;
cout<<"Your payment is:RM"<<payment;
break;
case 'D':price=103.00;
charge_rate=0.07;
charge=price*charge_rate;
payment=price+charge;
cout<<"Your payment is:RM"<<payment;
break;
case 'E':price=150.00;
charge_rate=0.07;
charge=price*charge_rate;
payment=price+charge;
cout<<"Your payment is:RM"<<payment;
break;
case 'F':price=245.00;
charge_rate=0.1;
charge=price*charge_rate;
payment=price+charge;
cout<<"Your payment is:RM"<<payment;
break;
case 'H':price=250.00;
charge_rate=0.1;
charge=price*charge_rate;
payment=price+charge;
cout<<"Your payment is:RM"<<payment;
break;
default:
cout<<"Error, this item code is not in the listn";
}
return 0;
}
Answer for question 7
#include<iostream>
Using namespace.std;
int main()
{
float House_cost,monthly_repayment,interest,interest_pay,House_cost_ineterest;
int year;
cout<<"HOUSE COST IN RM:";
cin>>House_cost;
cout<<"YEAR OF TERM APPLIED:";
cin>>year;
if(year>=25)
{
interest=0.75;
}
else if(year>=20&&year<25)
{
interest=0.65;
}
else if(year>=15&&year<20)
{
interest=0.55;
}
else if(year>=10&&year<15)
{
interest=0.45;
}
else
{
interest=0.40;
}
interest_pay=interest*House_cost;
House_cost_ineterest=House_cost+interest_pay;
monthly_repayment=House_cost_ineterest/(year*12);
cout<<"YOUR REPAYMENT A MONTH IS:RM"<<monthly_repayment;
return 0;
}
Repetition structure
• Why repetition is needed??
• Suppose we want to add five numbers to find their
average.
• From what we have learned so far, we could proceed as
follows.
cin>>num1>>num2>>num3>>num4>>num5;
sum= num1>+num2+num3+num4+num5;
average=sum/5;
• How about we want to add 100 or 1000 numbers ??
• Therefore, repetition structure offers a better way of
performing those task.
count=0;
while(count<100)
{
cin>>num;
sum=sum+num;
count=count+1;
}
average=sum/count;
REPETITION STRUCTURE
• A programming control structure that allows a
series of instructions to be executed more
than once.
• The similar statement is repeated several
times until the condition are fulfilled.
• There are three types of loop:
 for
 while
do..while
Flowchart of for statement
expression statementt
False
For statement
• General form of for structure:
for(initialize;condition;counter)
Statement block;
• The initialize part is used to initialize any variables that may need to
be initialized. This part is performed just once at the start of the
loop.
• The condition part determines wether the loop execution should
continue.
• If the value of expression is TRUE (non zero), the statement block
will be executed otherwise the loop will be terminated.
• The counter part typically increments or decrements the loop
index.
• This performed every time through the loop iteration.
• This part is used to increment any variable that may need to be
incremented.
Example of program
#include<iostream>
using namespace std;
int main()
{
int num, i,sum;
sum=0;
for(i=0;i<5;i++)
{
cout<<“Enter number:”;
cin>>num;
sum=sum+num;
}
cout<<“Total of numbers are:”<<sum;
return 0;
}
Flowchart of While statement
statementt
True
False
condition
While statement
• General form:
while (expression)
statement block;
• The first expression evaluated,
• If it is True the statement block is executed
• If it False, the statement block is skipped
• A while structure allow the program to repeat a set of statement as long as the
starting condition remains true. “True” is used in the boolean sense snd so the
condition for loop to continue must evaluate to true or false.
• The program evaluates the condition if it true then the statement inside the braces
are executed.
• The condition is evaluated again.This will continue untill the condition is false and
the program jumps to the next line after the body of the structure.
• If only one statement is used in the while loop then the braces are not required.
Example of program
#include<iostream>
using namespace std;
int main()
{
int num, i,sum;
i=0;
sum=0;
while(i<5)
{
cout<<“Enter number:”;
cin>>num;
sum=sum+num;
i++;
}
cout<<“Total of numbers are:”<<sum;
return 0;
}
Output:
Enter number:10
Enter number:2
Enter number:12
Enter number:4
Enter number:2
Total of numbers are:30
Flowchart of do…while statement
statementt
True
False
do....while statement
• General form:
do
{
statement block;
}
while(expression);
• The do part contains the body and is executed as long as
the while condition remains true.
• The difference between these two while structure are while
loop the condition is evaluated first and if it immediately
false, the condition is tested and so guaranteed to be
executed at least once.
Example of program
#include<iostream>
using namespace std;
int main()
{
int num, i,sum;
i=0;
sum=0;
do
{
cout<<“Enter number:”;
cin>>num;
sum=sum+num;
i++;
} while(i<5);
cout<<“Total of numbers are:”<<sum;
return 0;
}
Output:
Enter number:5
Enter number:8
Enter number:12
Enter number:3
Enter number:10
Total of numbers are:38
continue and break statement
• Two commands that can be included in
looping processes are continue and break.
• When included continue statement in the
body of the loop causes the program to return
immediately to the start of the loop and
ignore any remaining part of the body.
• The break statement causes the program to
immediately exit from the loop and resume at
the next of the progarm.
Example of program
(continue)
#include<iostream>
using namespace std;
int main()
{
int num, i,sum;
i=0;
sum=0;
while(i<5)
{
i++;
if(i==3)
continue;
cout<<"Enter number "<<i<<":";
cin>>num;
sum=sum+num;
}
cout<<“Total of numbers are:”<<sum;
return 0;
}
Output:
Enter number 1:10
Enter number 2: 5
Enter number 4:12
Enter number 5:3
Total of numbers are:30
Example of program
(break)
#include<iostream>
using namespace std;
int main()
{
int num, i,sum;
i=0;
sum=0;
while(i<5)
{
cout<<“Enter number”;
cin>>num;
sum=sum+num;
i++;
if(i==3)
break;
}
cout<<“Total of numbers are:”<<sum;
return 0;
}
Output:
Enter number:5
Enter number:20
Enter number:10
Total of numbers are:35
Difference between the loops
structure
for while do..while
#include<iostream>
using namespace std;
int main()
{
for(i=0;i<3;i++)
{
cout<<“welcome!”;
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
i=0;
while(i<3)
{
cout<<“welcome!”;
i++;
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
i=0;
do
{
cout<<“welcome!”;
i++;
} while(i<3);
return 0;
}

More Related Content

What's hot

Institusi kekeluargaan dalam islam
Institusi kekeluargaan dalam islamInstitusi kekeluargaan dalam islam
Institusi kekeluargaan dalam islam
Shahirah Zafirah
 
Penggunaan Kalkulator Dlm P & P
Penggunaan Kalkulator Dlm P & PPenggunaan Kalkulator Dlm P & P
Penggunaan Kalkulator Dlm P & P
morabisma
 
Contoh peraturan perkhemahan
Contoh peraturan perkhemahanContoh peraturan perkhemahan
Contoh peraturan perkhemahan
Mieyy Rashid
 
Protokol dalam pengurusan majlis
Protokol dalam pengurusan majlis Protokol dalam pengurusan majlis
Protokol dalam pengurusan majlis
Aidil Syazwan
 
Ujian Rintangan Penebatan
Ujian Rintangan PenebatanUjian Rintangan Penebatan
Ujian Rintangan Penebatan
shrim shaharin
 
Keperluan elektrik-dalam-bangunan
Keperluan elektrik-dalam-bangunanKeperluan elektrik-dalam-bangunan
Keperluan elektrik-dalam-bangunan
az mei
 
Pekerjaan Teka Siapa Saya
Pekerjaan Teka Siapa SayaPekerjaan Teka Siapa Saya
Pekerjaan Teka Siapa Saya
cikgunoor
 
Dokumen Perniagaan
Dokumen PerniagaanDokumen Perniagaan
Dokumen Perniagaan
dipedmara4
 

What's hot (20)

Institusi kekeluargaan dalam islam
Institusi kekeluargaan dalam islamInstitusi kekeluargaan dalam islam
Institusi kekeluargaan dalam islam
 
Buku Panduan Proses Nikah, Cerai dan Ruju' di JAWI
Buku Panduan Proses Nikah, Cerai dan Ruju' di JAWIBuku Panduan Proses Nikah, Cerai dan Ruju' di JAWI
Buku Panduan Proses Nikah, Cerai dan Ruju' di JAWI
 
Penggunaan Kalkulator Dlm P & P
Penggunaan Kalkulator Dlm P & PPenggunaan Kalkulator Dlm P & P
Penggunaan Kalkulator Dlm P & P
 
Sistem nombor
Sistem nomborSistem nombor
Sistem nombor
 
Contoh peraturan perkhemahan
Contoh peraturan perkhemahanContoh peraturan perkhemahan
Contoh peraturan perkhemahan
 
Protokol dalam pengurusan majlis
Protokol dalam pengurusan majlis Protokol dalam pengurusan majlis
Protokol dalam pengurusan majlis
 
Ujian Rintangan Penebatan
Ujian Rintangan PenebatanUjian Rintangan Penebatan
Ujian Rintangan Penebatan
 
Keperluan elektrik-dalam-bangunan
Keperluan elektrik-dalam-bangunanKeperluan elektrik-dalam-bangunan
Keperluan elektrik-dalam-bangunan
 
Litar utama dan litar kawalan motor elektrik
Litar utama dan litar kawalan motor elektrikLitar utama dan litar kawalan motor elektrik
Litar utama dan litar kawalan motor elektrik
 
Pemasangan nic
Pemasangan nicPemasangan nic
Pemasangan nic
 
Talak
TalakTalak
Talak
 
Pekerjaan Teka Siapa Saya
Pekerjaan Teka Siapa SayaPekerjaan Teka Siapa Saya
Pekerjaan Teka Siapa Saya
 
Dokumen Perniagaan
Dokumen PerniagaanDokumen Perniagaan
Dokumen Perniagaan
 
2) mahar dan walimatulurus
2)  mahar dan walimatulurus2)  mahar dan walimatulurus
2) mahar dan walimatulurus
 
Definisi tahun, dekad, abad dan alaf
Definisi tahun, dekad, abad dan alafDefinisi tahun, dekad, abad dan alaf
Definisi tahun, dekad, abad dan alaf
 
Tiang tiang elektrik dan substation malaysia
Tiang  tiang elektrik dan substation malaysiaTiang  tiang elektrik dan substation malaysia
Tiang tiang elektrik dan substation malaysia
 
Nikah Cerai Rujuk dalam agama islam.
Nikah Cerai Rujuk dalam agama islam.Nikah Cerai Rujuk dalam agama islam.
Nikah Cerai Rujuk dalam agama islam.
 
Etika etika keusahawanan
Etika etika keusahawananEtika etika keusahawanan
Etika etika keusahawanan
 
09 haji
09 haji09 haji
09 haji
 
Motor Arus Terus (AT)
Motor Arus Terus (AT)Motor Arus Terus (AT)
Motor Arus Terus (AT)
 

Similar to POLITEKNIK MALAYSIA

FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3
rohassanie
 
Cs1123 5 selection_if
Cs1123 5 selection_ifCs1123 5 selection_if
Cs1123 5 selection_if
TAlha MAlik
 
Mid term sem 2 1415 sol
Mid term sem 2 1415 solMid term sem 2 1415 sol
Mid term sem 2 1415 sol
IIUM
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
Vince Vo
 

Similar to POLITEKNIK MALAYSIA (20)

Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
Lec16.pptx problem specifications computer
Lec16.pptx problem specifications computerLec16.pptx problem specifications computer
Lec16.pptx problem specifications computer
 
lesson 2.pptx
lesson 2.pptxlesson 2.pptx
lesson 2.pptx
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.
 
FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
Statement
StatementStatement
Statement
 
Cs1123 5 selection_if
Cs1123 5 selection_ifCs1123 5 selection_if
Cs1123 5 selection_if
 
Introduction to Selection control structures in C++
Introduction to Selection control structures in C++ Introduction to Selection control structures in C++
Introduction to Selection control structures in C++
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptx
 
Mid term sem 2 1415 sol
Mid term sem 2 1415 solMid term sem 2 1415 sol
Mid term sem 2 1415 sol
 
Chap 4 c++
Chap 4 c++Chap 4 c++
Chap 4 c++
 
C++
C++C++
C++
 
Elements of programming
Elements of programmingElements of programming
Elements of programming
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN CINPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
 

More from Aiman Hud

More from Aiman Hud (20)

POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 

Recently uploaded

Recently uploaded (20)

OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
 
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
 
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
 
WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - Kanchana
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
WSO2CON 2024 - Building a Digital Government in Uganda
WSO2CON 2024 - Building a Digital Government in UgandaWSO2CON 2024 - Building a Digital Government in Uganda
WSO2CON 2024 - Building a Digital Government in Uganda
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
 
WSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AIWSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AI
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of TransformationWSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...
 

POLITEKNIK MALAYSIA

  • 2. PROGRAM CONTROL STRUCTURE • Program control structure controls the flow of execution of program statement. • C++ provides structures that will allow an instruction or a block of instruction to be executed, repeated and skipped. • There are three control structure:  sequential structure  selection structure  repetition structure
  • 3. Sequence structure • The sequence structure has one entry point and one exit point. • No choice are made and no repetition. • Statement are executed in sequence, one after another without leaving out any single statement.
  • 5. Example of sequential statement #include<iostream> using namespace std; int main() { float payrate=8.5; float hourWorked=25.0; float wages; wages= hourWorked * payrate; cout<<“n Wages = RM:”<<wages<<endl; return 0; } Output: Wages = RM:212.5
  • 6. Example of sequential statement include<iostream> using namespace std; int main() { int num1,num2; float total; cout<<“NUMBER 1:”; cin>>num1; cout<<“NUMBER 2:”; cin>>num2; total=num1+num2; cout<<“TOTAL OF TWO NUMBERS IS:”<<total<<endl; return 0; } Output: NUMBER 1:10 NUMBER 2:5 TOTAL OF TWO NUMBERS IS:15
  • 7. Selection structure • The selection structure is used to allow choices to be made. • This program executes particular statements depending on some condition(s). • C++ uses if..else, nested if and switch..case statement for making decision.
  • 8. Selection statement If…else • A selection of control structure is used to select between two or more options. • Used to make decision based on condition. • else statement will never exist in a program without if statement. • If tested condition is TRUE, the entire block of statements following the if is performed. • If the tested condition is FALSE, the entire block of statement following the else is performed
  • 11. Example of if..else statement #include<iostream> using namespace std; int main() { int age; cout<<“n Key in your age:”<<age; cin>>age; if(age>21) { cout<<“Qualifiied to have driving license”; } else { cout<<“Not qualified to have driving license”; } return 0; } Output: Key in your age:18 Not qualified to have driving license Output: Key in your age:25 Qualifiied to have driving license Output: Key in your age:21 Not qualified to have driving license
  • 12. Nested if..else condition1 condition2 condition3 Statement A Statement C Statement D true false true false true Statement B false
  • 13. Nested if..else If(condition 1) { If(condition 2) { If(condition 3) { Statement A } else { Statement B } } else { Statement c } } else { Statement D }
  • 14. • In this nested form, condition 1 is evaluated. If it is zero, statement D is executed and the entire nested if statement is terminated. • In not, control goes to the second if and condition 2 is evaluated. If it is zero, statement C is executed. • If not, control goes to the third if and condition 3 is evaluated. If it is zero, statement B is executed . • If not , statement A executed.
  • 15. Example of statement#include<iostream> using namespace std; int main() { float cgpa,salary; int year; cout<<“n YEAR:”; cin>>year; cout<<“n CGPA:”; cin>>cgpa; cout<<“n SALARY:”; cin>>salary; if(year>1) { if(cgpa>=3.00) { if(salary<=500) { cout<<“Your application is under consideration”; } else { cout<<“Not success because salary is more than RM500.00“; } } else { cout<<“ Not success because cgpa is less than 3.00”; } } else { cout<<“ Not success because yeaa of study is less than 1”; } return 0; Output: YEAR:2 CGPA:3.5 SALARY:250 Your application is under consideration
  • 16. Nested if..else condition1 condition2 condition3 Statement D Statement B Statement A false true false true false Statement C true
  • 17. Nested if..else if(condition 1) { Statement A } else if(condition 2) { Statemnet B } else if(condition 3) { Statement C } else { Statement D }
  • 18. Example of if..else statement#include<iostream> using namespace std; int main() { float cgpa; cout<<“n CGPA:”; cin>>cgpa; if(cgpa < 0.00 || cgpa > 4.00) { cout<<“Invalid data”; { else if(cgpa > = 3.5 && cgpa < = 4.00) { cout<<“Dean List”; } else if(cgpa > 2.00 && cgpa < 3.50.00) { cout<<“Pass”; } else if(cgpa > = 1.80 && cgpa < 2.00) { cout<<“ Conditional Pass;; } else { cout<<“ Fail”; } return 0; } Output: CGPA:2.5 Pass
  • 19. Usage of Logical Operator Symbol (pseudocode) Symbol (c++ language) Example Result Description AND && (1>3)&&(10<20) FALSE Both sides of the condition must be true. OR || (1>3)&&(10<20) TRUE Either one of the condition must be true. NOT ! !(10<20) FALSE Change the operation either from true to false or vise versa.
  • 20. Example using of AND operator ● If condition ( num1< num2) is true AND condition ( num1 > num3) is true then print “First number is the largest number” will be displayed. ● If one of the condition is not true then print “First number is the largest number” will not be displayed. If ((num1 > num2 ) && ( num1 > num3 )) cout<<“First number is the largest number”;
  • 21. Example usage of OR operator: ● if the sales are more than 5000 or working hours are more than 81, bonus RM500 will be given. If either condition is not fulfilled, still the amount of RM500 will be given as bonus. ● If both condition are false, then bonus will not be given. If ( ( sales > 5000 ) || (hourseworked > 81 )) Bonus = 500; else
  • 22. Switch case • The switch..case structure consists of a series of case label and optional default case which can be included for option that is not listed in the case label. • Switch which is followed by variable name in parentheses called controlling expression. • The break keyword must be included at the end of each case statement. • The break statement causes program control to proceed with the first statement after the switch structure. • Without break statement, all command for that case and next case label will be executed. • Default is an option that will only be executed if suitable case cannot be found.
  • 23. Flowchart Case 1 Case n default Statement for value=1 break break Statement for value=1
  • 24. syntax Switch(expression) { case value 1: statement if expression =value 1 break; case value n: statement if expression =value n break; default: statement if none of the expression is matched }
  • 25. Example of program #include<iostream> using namespace std; int main() { int num; cout<<“n Enter a number:”; cin>>num; switch(num) { case 1: cout<<“Welcome”; break; case 2: cout<<“Bye”; break; default: cout<<“Wrong Number”; } return 0; Output: Enter a number:1 Welcome
  • 26. Example of program without break statement #include<iostream> using namespace std; int main() { int num; cout<<“n Enter a number:”; cin>>num; switch(num) { case 1: cout<<“Welcome”; case 2: cout<<“Bye”; default: cout<<“Wrong Number”; } return 0; Output: Enter a number:1 WelcomeByeWrong Number
  • 27. Example of program #include<iostream> using namespace std; int main() { char grade; cout<<“n Enter a grade (A-D):”; cin>>grade; switch(grade) { case ‘A’: cout<<“Minimun marks is 80”; break; case ‘B’: cout<<“Minimun marks is 60”; break; case ‘C’: cout<<“Minimun marks is 40”; break; case ‘D’: cout<<“Minimun marks is 25”; break; default: cout<<“Mark between 0-24”; } return 0; } Output: Enter a grade (A-D):C Minimun mark is 40
  • 28. • A,B,C, and D are the possible values that can be assigned to the variable grade. • In each case of constant, A to D a statement or sequence of statement would be executed. • You would have noticed that every line has statement under it called break. • The break is the only thing that stop a case statement from continuing all the way down through each case label under it. • In fact if you do not put break in, the program will keep going down in the case statement. Into other case labels, until it reaches the end of break. • However the default part is the codes that would be executed if there were no matching case of constant’s value.
  • 29. Rules for Switch statement • The value of case statement must be an integer or a character constant. • Case statement arrangement is not important. • Default can exist earlier ( but compiler will place it at the end) • Can’t use condition or limit. • Why switch is the best way to write a program:  easy to read  It is a more spesific statement to implement multiple alternative decisions.  It is used to make a decision between many alternative.
  • 30. Comparison of Nested if statement and switch..case statement Nested if statement switch.. case statement #include<iostream> using namespace std; int main() { int code; cout<<“Enter code (1,2 or 3)”; cin>>code; if(code==1) cout<<“Your favourite flavor is vanila”; else if(code==2) cout<<“Your favourite flavor is chocolate”; else if(code==3) cout<<“Your favourite flavor is strawbery” else cout<<“please choose code 1,2 or 3 only”; return 0; } #include<iostream> using namespace std; int main() { int code; cout<<“Enter code (1,2 or 3)”; cin>>code; switch(code) { case1: cout<<“Your favourite flavor is vanila”; break; case2: cout<<“Your favourite flavor is chocolate”; break; case 3: cout<<“Your favourite flavor is strawbery”; default: cout<<“please choose code 1,2 or 3 only”; } return 0; }
  • 31. exercise 1. If the payment RM5,000 to RM10,000, display a message that the customer will be given a free ticket to Langkawi. Otherwise, display a message that the customer will only be given a free lunch at Quality Hotel. Translate the statement above to program segment. 2. You are given the following requirement: a. Your program is able to read the amount of sales for a sale executive. b. If the sale is more than RM10,000 then the commission will be 5% of the sale. Otherwise, the commission is only 3% of the sales. c. Your program is able to calculate the amount of commission by multiply the percentage of commission with sale. d. Your program is able to display the amount of commission to the sale executive. 3. Write a c++ program to find a maximum value of three integers which are a, b and c.
  • 32. exercise 4. You’re given the following requirements: a. Your program is able to read the item code from the keyboard b. The item code determines the price for each item in the shop. The following table shows the prices and charges of 7 items in the shop. c. If the item code is not matched, an error message is dispayed as follows: “Error, this item code is not in the list.” d. For each selected item, the charge is calculated by multiplying the item price and charge rate. e. Then the payment is a summation of item price and charge. Your program is to display this payment. Item Code Price (RM) Charge(%) A 54.00 5 B 65.00 5 C 82.00 5 D 103.00 7 E 150.00 7 F 245.00 10 H 250.00 10
  • 33. exercise 4. NASA Consultant Co Ltd wants to develop a program that can help the consultant to give advice regarding the home loan. The program receives two inputs from the consultant, which are the amount of House Cost in Ringgit Malaysia and years of term applied. Based on these two inputs, the program should be able to calculate the monthly repayment. The table for the interest calculation is provided as follows: The formulae for the monthly repayment calculation are as follows: Interest (RM) = Interest (%) x House cost (RM) House cost with Interest = House Cost (RM) + Interest (RM) Monthly Repayment = House Cost with Interest / ( Year of Term Applied x 12) Year of Term Applied Interest (%) More than or equal to 25 years 7.5 More than or equal to 20 years 6.5 More than or equal to 15 years 5.5 More than or equal to 10 years 4.5 Less than 10 years 4.0
  • 34. Answer for question 1 (if else) #include<iostream> using namespace std; int main() { float payment; cout<<"Please enter your payment:RM"; cin>>payment; if(payment>=5000&&payment<=10000) { cout<<"Free air ticket to Langkawi"; } else { cout<<"Free lunch at Quality Hotel"; } return 0;
  • 35. Answer for question 1 (nested if)#include<iostream> using namespace std; int main() { float payment; cout<<"Please enter your payment:RM"; cin>>payment; if(payment>=5000) { if(payment<=10000) cout<<"Free air ticket to Langkawi"; else cout<<"Free lunch at Quality Hotel"; } else cout<<"Free lunch at Quality Hotel"; return 0; }
  • 36. Answer for question 1 if and if else#include<iostream> using namespace std; int main() { float payment; cout<<"Please enter your payment:RM"; cin>>payment; { if(payment>10000) { cout<<"Free lunch at Quality Hotel"; cout<<"ccc"; } } { if(payment>=5000) { cout<<"Free air ticket to Langkawi"; cout<<"hiii"; } else { cout<<"Free lunch at Quality Hotel"; cout<<"bye"; } } return 0; }
  • 37. Answer for question 2 #include<iostream> using namespace std; int main() { float sale,commission,amount_commission; cout<<"Please enter your sale:RM"; cin>>sale; if(sale>10000) { commission=0.05; } else { commission=0.03; } amount_commission=sale*commission; cout<<"Your amount of commission is:RM"<<amount_commission; return 0; }
  • 38. Answer for question 2 #include<iostream> using namespace std; int main() { float sale,commission,amount_commission; cout<<"Please enter your sale:RM"; cin>>sale; if(sale>10000) { commission=0.05; amount_commission=sale*commission; cout<<"Your amount of commission is:RM"<<amount_commission; } else { commission=0.03; amount_commission=sale*commission; cout<<"Your amount of commission is:RM"<<amount_commission; } return 0; }
  • 39. Answer question 5 #include<iostream> using namespace std; int main() { int a,b,c; cout<<"ENTER FIRST NUMBER:"; cin>>a; cout<<"ENTER SECOND NUMBER:"; cin>>b; cout<<"ENTER THIRD NUMBER:"; cin>>c; cout<<"n*****************************n"; if(a>b&&a>c) { cout<<"MAXIMUM VALUE IS:"<<a; } else if(b>a&&b>c) { cout<<"MAXIMUM VALUE IS:"<<b; } else { cout<<"MAXIMUM VALUE IS:"<<c; } return 0; }
  • 40. Answer for question 6 #include<iostream> using namespace std; int main() { char code; float price,charge_rate,charge,payment; cout<<"ITEM CODE:"; cin>>code; switch(code) { case 'A':price=54.00; charge_rate=0.05; charge=price*charge_rate; payment=price+charge; cout<<"Your payment is:RM"<<payment; break; case 'B':price=65.00; charge_rate=0.05; charge=price*charge_rate; payment=price+charge; cout<<"Your payment is:RM"<<payment; break; case 'C':price=82.00; charge_rate=0.05; charge=price*charge_rate; payment=price+charge; cout<<"Your payment is:RM"<<payment; break;
  • 41. case 'D':price=103.00; charge_rate=0.07; charge=price*charge_rate; payment=price+charge; cout<<"Your payment is:RM"<<payment; break; case 'E':price=150.00; charge_rate=0.07; charge=price*charge_rate; payment=price+charge; cout<<"Your payment is:RM"<<payment; break; case 'F':price=245.00; charge_rate=0.1; charge=price*charge_rate; payment=price+charge; cout<<"Your payment is:RM"<<payment; break; case 'H':price=250.00; charge_rate=0.1; charge=price*charge_rate; payment=price+charge; cout<<"Your payment is:RM"<<payment; break; default: cout<<"Error, this item code is not in the listn"; } return 0; }
  • 42. Answer for question 7 #include<iostream> Using namespace.std; int main() { float House_cost,monthly_repayment,interest,interest_pay,House_cost_ineterest; int year; cout<<"HOUSE COST IN RM:"; cin>>House_cost; cout<<"YEAR OF TERM APPLIED:"; cin>>year; if(year>=25) { interest=0.75; } else if(year>=20&&year<25) { interest=0.65; } else if(year>=15&&year<20) { interest=0.55; } else if(year>=10&&year<15) { interest=0.45; } else { interest=0.40; } interest_pay=interest*House_cost; House_cost_ineterest=House_cost+interest_pay; monthly_repayment=House_cost_ineterest/(year*12); cout<<"YOUR REPAYMENT A MONTH IS:RM"<<monthly_repayment; return 0; }
  • 43. Repetition structure • Why repetition is needed?? • Suppose we want to add five numbers to find their average. • From what we have learned so far, we could proceed as follows. cin>>num1>>num2>>num3>>num4>>num5; sum= num1>+num2+num3+num4+num5; average=sum/5; • How about we want to add 100 or 1000 numbers ?? • Therefore, repetition structure offers a better way of performing those task.
  • 45. REPETITION STRUCTURE • A programming control structure that allows a series of instructions to be executed more than once. • The similar statement is repeated several times until the condition are fulfilled. • There are three types of loop:  for  while do..while
  • 46. Flowchart of for statement expression statementt False
  • 47. For statement • General form of for structure: for(initialize;condition;counter) Statement block; • The initialize part is used to initialize any variables that may need to be initialized. This part is performed just once at the start of the loop. • The condition part determines wether the loop execution should continue. • If the value of expression is TRUE (non zero), the statement block will be executed otherwise the loop will be terminated. • The counter part typically increments or decrements the loop index. • This performed every time through the loop iteration. • This part is used to increment any variable that may need to be incremented.
  • 48. Example of program #include<iostream> using namespace std; int main() { int num, i,sum; sum=0; for(i=0;i<5;i++) { cout<<“Enter number:”; cin>>num; sum=sum+num; } cout<<“Total of numbers are:”<<sum; return 0; }
  • 49. Flowchart of While statement statementt True False condition
  • 50. While statement • General form: while (expression) statement block; • The first expression evaluated, • If it is True the statement block is executed • If it False, the statement block is skipped • A while structure allow the program to repeat a set of statement as long as the starting condition remains true. “True” is used in the boolean sense snd so the condition for loop to continue must evaluate to true or false. • The program evaluates the condition if it true then the statement inside the braces are executed. • The condition is evaluated again.This will continue untill the condition is false and the program jumps to the next line after the body of the structure. • If only one statement is used in the while loop then the braces are not required.
  • 51. Example of program #include<iostream> using namespace std; int main() { int num, i,sum; i=0; sum=0; while(i<5) { cout<<“Enter number:”; cin>>num; sum=sum+num; i++; } cout<<“Total of numbers are:”<<sum; return 0; } Output: Enter number:10 Enter number:2 Enter number:12 Enter number:4 Enter number:2 Total of numbers are:30
  • 52. Flowchart of do…while statement statementt True False
  • 53. do....while statement • General form: do { statement block; } while(expression); • The do part contains the body and is executed as long as the while condition remains true. • The difference between these two while structure are while loop the condition is evaluated first and if it immediately false, the condition is tested and so guaranteed to be executed at least once.
  • 54. Example of program #include<iostream> using namespace std; int main() { int num, i,sum; i=0; sum=0; do { cout<<“Enter number:”; cin>>num; sum=sum+num; i++; } while(i<5); cout<<“Total of numbers are:”<<sum; return 0; } Output: Enter number:5 Enter number:8 Enter number:12 Enter number:3 Enter number:10 Total of numbers are:38
  • 55. continue and break statement • Two commands that can be included in looping processes are continue and break. • When included continue statement in the body of the loop causes the program to return immediately to the start of the loop and ignore any remaining part of the body. • The break statement causes the program to immediately exit from the loop and resume at the next of the progarm.
  • 56. Example of program (continue) #include<iostream> using namespace std; int main() { int num, i,sum; i=0; sum=0; while(i<5) { i++; if(i==3) continue; cout<<"Enter number "<<i<<":"; cin>>num; sum=sum+num; } cout<<“Total of numbers are:”<<sum; return 0; } Output: Enter number 1:10 Enter number 2: 5 Enter number 4:12 Enter number 5:3 Total of numbers are:30
  • 57. Example of program (break) #include<iostream> using namespace std; int main() { int num, i,sum; i=0; sum=0; while(i<5) { cout<<“Enter number”; cin>>num; sum=sum+num; i++; if(i==3) break; } cout<<“Total of numbers are:”<<sum; return 0; } Output: Enter number:5 Enter number:20 Enter number:10 Total of numbers are:35
  • 58. Difference between the loops structure for while do..while #include<iostream> using namespace std; int main() { for(i=0;i<3;i++) { cout<<“welcome!”; } return 0; } #include<iostream> using namespace std; int main() { i=0; while(i<3) { cout<<“welcome!”; i++; } return 0; } #include<iostream> using namespace std; int main() { i=0; do { cout<<“welcome!”; i++; } while(i<3); return 0; }