Loops in C++
Loops are indented to repeat a block of codes as long as a condition is met. There are different forms of loops in C++.
while statement
The while loop repeats a block of code as long as a specified condition is true. The syntax is as follows.
For example,
| print-till-ten-with-while.cpp | |
|---|---|
do-while statement
The do-while loop is a variant of the while loop. This loop will at least execute the code block once. Then it will repeat the loop as long as the condition is true. The syntax is as follows.
For example,
| print-num.cpp | |
|---|---|
Compile and execute the program above and see how the program responses given different input.
for statement
If we know exactly how many times you want to repeat a block of code, use the for loop is a good option. The syntax is as follows.
statement1is executed (one time) before the execution of the code block.statement2defines the condition for executing the code block.statement3is executed (every time) after the code block has been executed.
For example,
| print-till-ten-with-for.cpp | |
|---|---|
break and continue
breakcan be used to have an early exit and jump out of a loop.continueonly breaks the current iteration and continues with next iteration in the loop.
For example,
| test-flow.cpp | |
|---|---|
Assignment 6
Create a C++ program named square.cpp that prompts for length of a square. The program will then display a square of the given length in the terminal.
We can use any character to represent the side of a square. A sample run would look like the following.
Sample Solution
Assignment 7
Create a C++ program named odd-or-even.cpp that would keep prompting for a number and tell if that number is odd or even. The program exists if the user enters a negative number.
A sample run looks like the following.
g++ odd-or-even.cpp -o odd-or-even
./odd-or-even
Enter a number: 3
Odd number
Enter a number: 2
Even number
Enter a number: 1
Odd number
Enter a number: 0
Even number
Enter a number: -1
See you later!
Sample Solution
| odd-or-even.cpp | |
|---|---|
Quote
Assignment 8
Create a C++ program named print-triangle.cpp that prompts for rows of a triangle. The program will then display a triangle with the given number of rows in the terminal.
A sample run looks like the following.
g++ print-triangle.cpp -o print-triangle
./print-triangle
Enter number of rows for the triangle: 7
*
**
***
****
*****
******
*******
Sample Solution
| print-triangle.cpp | |
|---|---|
Assignment 9
Create a C++ program named diamond.cpp that prompts for rows of a diamond, where the number of rows shall be odd. The program will then display a diamond with the given number of rows in the terminal.
A sample run looks like the following.
g++ diamond.cpp -o diamond
./diamond
Enter number of rows: 21
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
*******************
*****************
***************
*************
***********
*********
*******
*****
***
*
Sample Solution
Assignment 10
Create a C++ program named plus.cpp that prompts for rows of a plus sign, where the number of rows shall be odd. The program will then display a plus sign with the given number of rows in the terminal.
A sample run looks like the following.
Sample Solution
Assignment 11
We can generate a random number with the help of cstdlib lib. See the following example for a random within 1 and 1000.
| print-random-number.cpp | |
|---|---|
Create a C++ program named number-guessing-game.cpp that sets a random number (1 to 1000) for user to guess. It will display "Bingo!" if the user provides an exact match. If not, prompts the user if the number is higher or lower. Keep the game playing till the random number is provided.
A sample run looks like the following.
g++ number-guessing-game.cpp -o number-guessing-game
./number-guessing-game
Your guess is 500
It should be higher...
Your guess is 750
It should be lower...
Your guess is 625
It should be lower...
Your guess is 575
It should be lower...
Your guess is 535
It should be higher...
Your guess is 550
It should be higher...
Your guess is 560
It should be lower...
Your guess is 555
It should be lower...
Your guess is 553
It should be higher...
Your guess is 554
Bingo!
