Chapter 3 - Loops
Chapter 3 - Loops
Chapter 3 - Loops
Chapter 3: Loops
Loops can execute a block of code as long as a specified condition is reached.
Loops are handy because they save time, reduce errors, and they make code more readable.
While Loop
The while loop loops through a block of code if a specified condition is true:
Syntax
while (condition) {
// code block to be executed
}
Example:
int i = 0;
while (i < 5) {
cout << i << endl;
i++;
}
Syntax
do {
// code block to be executed
}
while (condition);
Example:
int i = 0;
do {
cout << i << endl ;
i++;
}
while (i < 5);
Lecturer: Quang-Thai Ho
YUAN ZE UNIVERSITY – Fundamental Computer Programming- C++ Lab(I)
For Loop
When you know exactly how many times you want to loop through a block of code, use the for loop
instead of a while loop:
Syntax
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
Statement 1 is executed one time before the execution of the code block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been executed.
Example:
int i;
Break
You have already seen the break statement used in an earlier chapter of this tutorial. It was used to
"jump out" of a switch statement.
int i;
Lecturer: Quang-Thai Ho
YUAN ZE UNIVERSITY – Fundamental Computer Programming- C++ Lab(I)
Continue
The continue statement breaks one iteration (in the loop), if a specified condition occurs, and
continues with the next iteration in the loop.
int i;
int i = 0; int i = 0;
Lecturer: Quang-Thai Ho
YUAN ZE UNIVERSITY – Fundamental Computer Programming- C++ Lab(I)
S1 = 1 + 2 + … + n
1 2 𝑛
S2 = + + ⋯+
2 3 𝑛+1
S3 = -1 + 2 -3 + … + (-1)nn
Problem 2: Write a program to calculate the number of nth in Fibonacci series according to the
following recursion formula:
1 𝑖𝑓 𝑛 = 0 𝑜𝑟 𝑛 = 1
𝐹𝑛 = {
𝐹𝑛 − 2 + 𝐹𝑛 − 1 𝑖𝑓 𝑛 ≥ 2
With input is a positive integer n, output is the number of n-th in Fibonacci series.
Problem 3: Write a program to list n + 1 first numbers in Fibonacci series. The input is a positive integer
n, the output is the Fibonacci series, each number is separated by a space.
Input: 5
Output: 1 1 2 3 5 8
Problem 4: Write a program to calculate xn by using the formula: xn = x*x*…*x (not using pow in cmath
library). The input is real number x and the integer n. The output is the result of xn which displayed with
4 decimal places.
Problem 5: Display values from 1 to m*n in a matrix of m rows, n columns from left to right, from top to
bottom. With input of 2 integers m, n; output is the result matrix.
Input: 2 3
Output:
1 2 3
4 5 6
Problem 6: Write a program to receive from keyboard two positive integers n. Print out a triangle with
height n.
Input: 5
Output:
* *
* * *
* * * *
Lecturer: Quang-Thai Ho
YUAN ZE UNIVERSITY – Fundamental Computer Programming- C++ Lab(I)
Problem 7: Write a program to receive from keyboard two positive integers m and n. Print out a
rectangle with width n and height m.
Input: 3 4
Output:
* * * *
* *
* * * *
Problem 8: Write a program to input an integer n, print a Pascal triangle with height n + 1 into the
screen.
Input: 5
Output: 1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
Problem 9: Calculate the total competition time of n members of a cycling team in the championship
cup. The program allows to enter the number of athletes in the team (n), and then enter the time of
each athlete. Then said:
Problem 10: Calculate the average grade of each semester's subjects in 1 semester. The program allows
to enter the number of subjects in the semester (n), then enter the score of each subject (the score is
from 0.0f to 10.0f). Then said:
Problem 11: Write a program to find the smallest positive integer k such that 2k > n where n is a positive
integer entered from the keyboard. The input of the problem is a positive integer n, the output is a
positive integer k satisfying the condition.
Input: 5 Output: 3
Lecturer: Quang-Thai Ho
YUAN ZE UNIVERSITY – Fundamental Computer Programming- C++ Lab(I)
1 1 1
Problem 12: Write a program to find the smallest positive integer n such that 2 + 3 + ⋯ 𝑛
< 𝑆 and 𝑆 is
a real number entered from the keyboard. The input of the problem is a real number S, the output is a
positive integer n that satisfy the conditions.
Problem 13: Write a program to print to the screen the integers from 1 to 100 such that every 10
numbers have a newline.
Problem 14: Write a program to print the multiplication table to the screen.
S0 = n! = 1*2*…*n
1 1
S1 = 1 + 2
+ ⋯+ 𝑛
1 1
S2 = 1 + + ⋯+
2! 𝑛!
Suggestion: 100 ∗ a + 10 ∗ b + c = 𝑎3 + 𝑏 3 + 𝑐 3
Problem 17: Write a program to input integers from the keyboard until a prime number is encountered,
then the input ends. Sum of even numbers and average of odd numbers.
Problem 18: Write a program to input a positive integer. Please notify on the screen how many digits
the number has and the sum of the digits of that number.
Input: 12345
Problem 19: Write a program that prints to the screen all the prime numbers from 2 to N. With N
entered from the keyboard.
Input: 100
Output: 100 = 2 * 2 * 5 * 5
Lecturer: Quang-Thai Ho