Lab 5-Control Structures (Part 2)
Lab 5-Control Structures (Part 2)
Page No.
1/2
Revision No.
Effective Date
1
10/09/2013
Conclusion (5%-10%) /
Kesimpulan(5%-10%)
TOTAL / JUMLAH
Name/Nama
Group Members/
Ahli Kumpulan
Assessment / Penilaian
1.
2.
3.
4.
1.
2.
Result (10%-30%)/
Keputusan(10%-30%)
Data Analysis and Discussion (30%-50%)/
Analisis Data dan Perbincangan(30%-50%)
Question (10%-30%)/
Soalan(10%-30%)
Conclusion (5%-10%) /
Kesimpulan(5%-10%)
TOTAL / JUMLAH
/100%
Matrix No./
No. Matrik
/ %
/ %
/ %
/100%
Page No.
Revision No.
Effective Date
1 / 12
1
25/03/2014
To understand the structure while loop, do-while loop and for loop.
To do modification of program coding and changing parameter of the loop.
To be familiar with program algorithm and applying formula for looping statement.
2. INTRODUCTION / THEORY
2.1 WHILE LOOP
The syntax of a while loop in C++ is:
while(condition)
{
statement(s);
}
Here, statement(s) may be a single statement or a block of statements. The condition may be any
expression, and true is any non-zero value. The loop iterates while the condition is true. When
the condition becomes false, program control passes to the line immediately following the loop.
Figure 1
Page No.
Revision No.
Effective Date
2 / 12
1
25/03/2014
Base on figure 1, key point of the while loop is that the loop might not ever run. When the
condition is tested and the result is false, the loop body will be skipped and the first statement
after the while loop will be executed. The while loop is an entry-condition loop. If the test
condition is FALSE to begin with, the program never executes the body of the loop.
2.2 DO-WHILE LOOP
Unlike for and while loops, which test the loop condition at the top of the loop, the do-while loop
checks its condition at the bottom of the loop. A do-while loop is similar to a while loop, except
that a do-while loop is guaranteed to execute at least one time. The syntax of a do...while loop in
C++ is:
do
{
statement(s);
}while( condition );
Notice that the conditional expression appears at the end of the loop, so the statement(s) in the
loop execute once before the condition is tested. If the condition is true, the flow of control
jumps back up to do, and the statement(s) in the loop execute again.
Figure 2
Based on Figure 2, this process repeats until the given condition becomes false.
Page No.
Revision No.
Effective Date
3 / 12
1
25/03/2014
Figure 3
Based on Figure 3, the flow of control in a for loop are:
The init step is executed first, and only once. This step allows you to declare and initialize
any loop control variables.
Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false,
the body of the loop does not execute and jumps to the next statement.
After the body of the for loop executes, the flow of control jumps back up to
the incrementstatement. This statement allows you to update any loop control variables.
This statement can be left blank, as long as a semicolon appears after the condition.
The condition is now evaluated again. If it is true, the loop executes and the process
repeats itself (body of loop, then increment step, and then again condition). After the
condition becomes false, the for loop terminates.
Page No.
Revision No.
Effective Date
4 / 12
1
25/03/2014
3.0 ASSIGNMENTS
3.1 While Loop
3.1.1 A program that count the number of while loop repetition from 1 to a specific amount:
a) Please type below code and run the program
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a;
cout<<"Enter the Number :";
cin>>a;
int counter = 1;
while (counter <= a)
{
cout<<"Execute While Loop Number = "<<counter<<endl;
counter++;
}
}
Page No.
Revision No.
Effective Date
5 / 12
1
25/03/2014
3.1.2 A program that display first display value until the last display value entered:
a) Please type below code and run the program
#include<iostream.h>
#include<conio.h>
main()
{
int a,b;
cout<<" Input the first display value :" ;
cin>>a;
cout<<endl;
cout<<"Input the last display value :" ;
cin>>b;
cout<<endl;
while(a<=b)
{
cout<<" The Display out value is :"<<a;
cout<<endl;
a++;
}
getch();
}
Page No.
Revision No.
Effective Date
6 / 12
1
25/03/2014
Page No.
Revision No.
Effective Date
7 / 12
1
25/03/2014
b) Please modify the program condition as below and run the program:
Change the selection from :
1,2,3,4 to a,b,c,d
Page No.
Revision No.
Effective Date
8 / 12
1
25/03/2014
Page No.
Revision No.
Effective Date
9 / 12
1
25/03/2014
Page No.
Revision No.
Effective Date
10 / 12
1
25/03/2014
3.3.2 A program that display 5 random numbers that have been entered:
a) Please type below code and run the program
#include<iostream.h>
#include<conio.h>
main()
{
int q,w;
int new1[5];
for(q=0;q<5;q++)
{
cout<<"Enter a random number :";
cin>>new1[q];
}
cout<<endl;
for(w=0;w<5;w++)
{
cout<< "The number you have entered : ";
cout<<new1[w];
cout<<endl;
}
getch();
}
Page No.
Revision No.
Effective Date
11 / 12
1
25/03/2014
4.0 DISCUSSION
Based on your understanding,
a) Please discuss the different ways to implement for loop:
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
Page No.
Revision No.
Effective Date
12 / 12
1
25/03/2014
5.0 CONCLUSION
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
Signature / Tandatangan :
Name / Nama : Engr. Amirul Syafiq Sadun
Date / Tarikh : 25 March 2014
Signature / Tandatangan :
Name / Nama : Dr. Jumadi Abdul Shukor
Date / Tarikh : 25 March 2014