This document provides instructions and examples for a lab on program control in C++ using looping statements like for, while, and do-while. It includes 5 activities with sample code to illustrate each loop type, and 2 exercises asking students to write programs to print patterns using loops and calculate parking fees using a while loop. The goal is for students to understand and be able to use different looping statements in C++ programs.
A Beginners Guide to Building a RAG App Using Open Source Milvus
Labsheet 5
1. FP 201 – PROGRAMMING FUNDAMENTALS
LAB 5 : PROGRAM CONTROL
Learning Outcome
By the end of this lab, students should be able to :
• Describe the structure of for, while and do..while statements in C+
+ program
• Write program using looping statements
Theory/ Topics
• Relational Operator (<, <=, >, >=)
• Logical Operator (&&, ||)
• Operator Precedence
• Expression
• Looping statements – for, while, do..while statements
Procedure :
Step 1: Type the programs given below
Step 2: Save the program as _________________
Step 3: Compile and run the program. Write the output
Activity 5A
#include<iostream>
using namespace std;
void main()
{
int x;
for(x=1; x<=5; x++)
{ if (x==3)
continue;
cout<<x;
}
return 0;
}
1
2. FP 201 – PROGRAMMING FUNDAMENTALS
Activity 5B
#include<iostream>
using namespace std;
void main()
{
int x;
for(x=1; x<=5; x++)
{ if (x==3)
break;
cout<<x;
}
return 0;
}
Activity 5C
//Program that add an integer to itself for n of times.
#include <iostream>
using namespace std;
void main()
{
int number1, number2;
int total = 0;
cout << "Please enter 2 numbers: ";
cin >> number1 >> number2;
for(int i = 0; i < number2; ++i)
{
total += number1;
}
cout << "Total addition: " << total;
}
2
3. FP 201 – PROGRAMMING FUNDAMENTALS
Activity 5D
The following program illustrate the syntax of do..while
statements.
#include <iostream>
using namespace std;
void main()
{
char ch;
int n1, n2;
do
{
cout << “Enter number 1 : “;
cin >> n1;
cout << “Enter number 2 : “;
cout << “number 1 divide by number 2 : “ << n1/n2
<< “, remainder is : “ << n1%n2 << endl;
cout << “nDo another ? (y/n) => “;
cin >> ch;
} while (ch != ‘n’);
}
Activity 5E
The following program illustrate the syntax of while statements.
#include <iostream>
using namespace std;
void main()
{
char ch = ‘y’;
int n1, n2;
while (ch != ‘n’)
{
cout << “Enter number 1 : “;
cin >> n1;
cout << “Enter number 2 : “;
3
4. FP 201 – PROGRAMMING FUNDAMENTALS
cout << “number 1 divide by number 2 : “ << n1/n2
<< “, remainder is : “ << n1%n2 << endl;
cout << “nDo another ? (y/n) => “;
cin >> ch;
}
}
Exercise 5.1
Write programs to print the following patterns:
1
12
123
1234
Exercise 5.2
Write a program by using while loops to calculate the parking fee based
on parking rate RM2.00 per hour for every customer. When there are
no more customers, your program should be able to sum the total
parking collection.
Example of output:
Enter parking hours: 2
Parking fee: RM 4
Continue? (y/n) => y
Enter parking hours: 5
Parking fee: RM 10
Continue? (y/n) => n
Total collection: 14
4