You are on page 1of 2

Lab 7: Looping (for loop)

for loop is an entry controlled looping statement. It is used to repeat set of statements
until some condition is met.

Syntax:

for (initialization; condition; update statement)


{
statement;
}

1. Initialization: contain loop counter variable initialization statements. It defines


starting point of the repetition (where to start loop)
2. Condition: contain Boolean expression. If Boolean expression is true, then execute
body of loop otherwise terminate the loop
3. Statement: specify what to repeat. It contains set of statement to repeat.
4. Update statement: contains loop counter update (increment/decrement) statements

Example:

#include <iostream>
using namespace std;
int main()
{
int i;

for (i = 1; i<=5; i++)


{
cout<< i << endl;

}
}
Task 1: Write a complete C++ program that allows the user to print the following message
10 times. The message is “I can do it!”

Task 2: Write a complete C++ program that allows the user to enter 10 numbers. Your
program is able to calculate the total and average of these numbers. Display the total and
average to the user.

Task 3: Write a complete C++ program that allows the user to enter 10 numbers. Your
program is able to do the following tasks:
a. Calculate the number of even numbers;
b. Calculate the number of odd numbers;

This is the sample of output:

Task 4: Input 10 integer numbers using a for loop, check if the number is positive, add the number
to a variable posNum, otherwise, add the number to a variable negNum.

This is the sample of output:

You might also like