Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Set Iterations with While Loop | While Loop
C++ Loops
course content

Conteúdo do Curso

C++ Loops

C++ Loops

1. While Loop
2. For loop
3. Nested loops

book
Set Iterations with While Loop

A while loop continues executing as long as a specified condition remains true. But what if we want to repeat a certain chunk of code a fixed number of times, say 3 or 5 times? In these cases, we can achieve this by using a counter variable.

Let's consider the idea in more detail. We already know that expressions like x < 5 return true if x is less than five and false if x is greater than or equal to five.

This concept aligns with what we need for a fixed number of repetitions. However, to make this work, we need to set up a counter variable x and update it inside the loop. Look at the code below step by step:

cpp

main

copy
123456789101112
#include <iostream> int main() { // create variable for loop condition int i = 0; // variables for loops are usually called i or j while (i < 5) { std::cout << "Hello!" << std::endl; i++; // incrementing the i } }

Note

The choice of the initial value for a variable and the increment used in a loop will impact the number of iterations. For instance, in this case initializing the variable with 3 will result in just 2 iterations.

Additionally, you can adjust the increment value, which will also affect the iteration count. The specific values to use for initialization and incrementing depend on the specific requirements and objectives of your task. You can try to experiment by modifying the starting value, altering the condition, or adjusting the increment value.

Tarefa
test

Swipe to show code editor

Simulate a banking system that stores the last few transactions in an array. Use a while loop to calculate the average of these transactions.

  1. Create variables to store the current index of the array (iterator) and the sum of the transaction values.
  2. Iterate through the array using the while loop, adding each transaction value to the sum.
  3. Once the loop completes, calculate the average by dividing the sum by the total number of transactions in the array.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 2
toggle bottom row

book
Set Iterations with While Loop

A while loop continues executing as long as a specified condition remains true. But what if we want to repeat a certain chunk of code a fixed number of times, say 3 or 5 times? In these cases, we can achieve this by using a counter variable.

Let's consider the idea in more detail. We already know that expressions like x < 5 return true if x is less than five and false if x is greater than or equal to five.

This concept aligns with what we need for a fixed number of repetitions. However, to make this work, we need to set up a counter variable x and update it inside the loop. Look at the code below step by step:

cpp

main

copy
123456789101112
#include <iostream> int main() { // create variable for loop condition int i = 0; // variables for loops are usually called i or j while (i < 5) { std::cout << "Hello!" << std::endl; i++; // incrementing the i } }

Note

The choice of the initial value for a variable and the increment used in a loop will impact the number of iterations. For instance, in this case initializing the variable with 3 will result in just 2 iterations.

Additionally, you can adjust the increment value, which will also affect the iteration count. The specific values to use for initialization and incrementing depend on the specific requirements and objectives of your task. You can try to experiment by modifying the starting value, altering the condition, or adjusting the increment value.

Tarefa
test

Swipe to show code editor

Simulate a banking system that stores the last few transactions in an array. Use a while loop to calculate the average of these transactions.

  1. Create variables to store the current index of the array (iterator) and the sum of the transaction values.
  2. Iterate through the array using the while loop, adding each transaction value to the sum.
  3. Once the loop completes, calculate the average by dividing the sum by the total number of transactions in the array.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 2
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt