Faculty of Engineering Technology
Page No.
1/2
Department of Electrical Engineering Technology
Revision No.
Title: EXP01_BASIC USAGE OF OSCILLOSCOPE
Effective Date
1
10/09/2013
Conclusion (5%-10%) /
Kesimpulan(5%-10%)
FACULTY OF ENGINEERING TECHNOLOGY
DEPARTMENT OF ELECTRICAL ENGINEERING TECHNOLOGY
COMPUTER PROGRAMMING LABORATORY
(MAKMAL TEKNOLOGI KOMPUTER)
WORKING INSTRUCTION AND REPORT
Course Code & Name /
Kod & Nama Kursus
Code & Title of Experiment/
Kod & Tajuk Ujikaji
Date of Experiment/
Tarikh Ujikaji
Programme/Program
Group/ Kumpulan
BNR 20803 / BNJ 10802 (COMPUTER PROGRAMMING)
LAB 5- CONTROL STRUCTURES (PART 2)
TOTAL / JUMLAH
Name/Nama
Group Members/
Ahli Kumpulan
Instructor / Lecturer Name /
Nama Instruktor
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%
Faculty of Engineering Technology
Page No.
Department of Electrical Engineering Technology
Revision No.
Title: Lab 4- Control Structures (Part 2)
Effective Date
1 / 12
1
25/03/2014
1. LAB SESSION LEARNING OUTCOMES
At the end of the lab session, students should be able :
i.
ii.
iii.
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
Faculty of Engineering Technology
Page No.
Department of Electrical Engineering Technology
Revision No.
Title: Lab 4- Control Structures (Part 2)
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.
Faculty of Engineering Technology
Page No.
Department of Electrical Engineering Technology
Revision No.
Title: Lab 4- Control Structures (Part 2)
Effective Date
3 / 12
1
25/03/2014
2.3 FOR LOOP
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to
execute a specific number of times. The syntax of a for loop in C++ is:
for ( init; condition; increment )
{
statement(s);
}
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.
Faculty of Engineering Technology
Page No.
Department of Electrical Engineering Technology
Revision No.
Title: Lab 4- Control Structures (Part 2)
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++;
}
}
Please explain what the program does:
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
b) Please modify the program condition as below and run the program:
Countdown from 1000 to a specifically entered number
(step size -1)
Faculty of Engineering Technology
Page No.
Department of Electrical Engineering Technology
Revision No.
Title: Lab 4- Control Structures (Part 2)
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();
}
Please explain what the program does:
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
b) Please modify the program condition as below and run the program:
Input the last display value
Input the first display value
Countdown from the last display to the first display (step
size -1)
Faculty of Engineering Technology
Page No.
Department of Electrical Engineering Technology
Revision No.
Title: Lab 4- Control Structures (Part 2)
Effective Date
6 / 12
1
25/03/2014
3.2 Do - While Loop
3.2.1 A program that sums the first and second number and request whether to repeat:
a) Please type below code and run the program
#include<iostream.h>
#include<conio.h>
main()
{
int q,w;
char z ;
do
{
cout<<"Please enter the first number :" ;
cin>>q;
cout<<"Please enter the second number :" ;
cin>>w;
cout<<"The sum of the numbers = " ;
cout<<q+w<<endl;
cout<<endl;
cout<<"Do you want to add more numbers (y / n):" ;
cin>>z;
cout<<endl;
}
while(z=='y');
}
Please explain what the program does:
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
b) Please modify the program condition as below and run the program:
Enter the first until Fifth number,
Find the average of the number
If the average is below 100, repeat the task
Faculty of Engineering Technology
Page No.
Department of Electrical Engineering Technology
Revision No.
Title: Lab 4- Control Structures (Part 2)
Effective Date
7 / 12
1
25/03/2014
3.2.2 A program that request for selection 1 until 4 only:
a) Please type below code and run the program
#include <iostream>
int main()
{
using namespace std;
int nSelection;
do
{
cout << "Please make a selection: " << endl;
cout << "1) Addition" << endl;
cout << "2) Subtraction" << endl;
cout << "3) Multiplication" << endl;
cout << "4) Division" << endl;
cin >> nSelection;
} while (nSelection != 1 && nSelection != 2 &&
nSelection != 3 && nSelection != 4);
return 0;
}
Please explain what the program does:
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
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
Faculty of Engineering Technology
Page No.
Department of Electrical Engineering Technology
Revision No.
Title: Lab 4- Control Structures (Part 2)
Effective Date
8 / 12
1
25/03/2014
3.2.3 A program that counting to a stop value number:
a) Please type below code and run the program
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a;
cout<<"Enter the stop number value :";
cin>>a;
int counter = 1;
do
{
cout<<"Counting Number "<<counter<<endl;
counter++;
}
while (counter <= a);
getch();
return 0;
}
Please explain what the program does:
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
b) Please modify the program condition as below and run the program:
Countdown from 1000 to a specifically entered number (step
size -1)
Faculty of Engineering Technology
Page No.
Department of Electrical Engineering Technology
Revision No.
Title: Lab 4- Control Structures (Part 2)
Effective Date
9 / 12
1
25/03/2014
3.3 For Loop
3.3.1 A program that counting to a specify value:
a) Please type below code and run the program
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a;
cout<<"Enter counting number :";
cin>>a;
for (int counter = 1; counter <= a; counter++)
{
cout<<"Count Number "<<counter<<endl;
}
getch();
return 0;
}
Please explain what the program does:
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
b) Please modify the program condition as below and run the program:
Countdown from any entered number until 0 (step size -1)
Faculty of Engineering Technology
Page No.
Department of Electrical Engineering Technology
Revision No.
Title: Lab 4- Control Structures (Part 2)
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();
}
Please explain what the program does:
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
b) Please modify the program condition as below and run the program:
Display 5 random Characters that have been entered
Faculty of Engineering Technology
Page No.
Department of Electrical Engineering Technology
Revision No.
Title: Lab 4- Control Structures (Part 2)
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:
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
b) Please discuss the advantages of do-while loop and for loop:
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
Faculty of Engineering Technology
Page No.
Department of Electrical Engineering Technology
Revision No.
Title: Lab 4- Control Structures (Part 2)
Effective Date
12 / 12
1
25/03/2014
5.0 CONCLUSION
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
~Lab Report by Group (2 person per group)~
Prepared by / Disediakan oleh :
Engr Amirul Syafiq Sadun
Approved by / Disahkan oleh :
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