Chapter 3 - Loops

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

YUAN ZE UNIVERSITY – Fundamental Computer Programming- C++ Lab(I)

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++;
}

The Do/While Loop


The do/while loop is a variant of the while loop. This loop will execute the code block once, before
checking if the condition is true, then it will repeat the loop as long as the condition is true.

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;

for (i = 0; i < 5; i++) {


cout << i << endl;
}

for (i = 0; i <= 10; i = i + 2) {


cout << i << endl;
}

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.

The break statement can also be used to jump out of a loop.

This example jumps out of the loop when i is equal to 4

int i;

for (i = 0; i < 10; i++) {


if (i == 4) {
break;
}
cout << i << endl;
}

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.

This example skips the value of 4

int i;

for (i = 0; i < 10; i++) {


if (i == 4) {
continue;
}
cout << i << endl;
}

Break and Continue in While Loop


You can also use break and continue in while loops:

Break Example Continue Example

int i = 0; int i = 0;

while (i < 10) { while (i < 10) {


if (i == 4) { if (i == 4) {
break; i++;
} continue;
cout << i << endl; }
i++; cout << i << endl;
} i++;
}

Lecturer: Quang-Thai Ho
YUAN ZE UNIVERSITY – Fundamental Computer Programming- C++ Lab(I)

Problem 1: Write a program to calculate the following sums:

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:

a. What is the team's total cycling time?


b. Average cycling time of the whole team.

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:

a. The average score of that student


b. The highest score of that student. Which subject is that?
c. The lowest score of that student. Which subject is that?

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.

Problem 15: Write a program to calculate the following sums:

S0 = n! = 1*2*…*n
1 1
S1 = 1 + 2
+ ⋯+ 𝑛
1 1
S2 = 1 + + ⋯+
2! 𝑛!

̅̅̅̅̅ such that 𝑎𝑏𝑐


Problem 16: Write a program to find 3-digit numbers 𝑎𝑏𝑐 ̅̅̅̅̅ = 𝑎3 + 𝑏 3 + 𝑐 3

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.

Suggestion: using do…while

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

Output: Number of digits: 5 Sum of digits: 15

Problem 19: Write a program that prints to the screen all the prime numbers from 2 to N. With N
entered from the keyboard.

Problem 20: Write a program to factor a number into prime factors.

Input: 100

Output: 100 = 2 * 2 * 5 * 5

Lecturer: Quang-Thai Ho

You might also like