Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: Setting Iterations With The While Loop in C++ | While Loop
C++ Loops

bookChallenge: Setting Iterations With The While Loop in C++

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:

main.cpp

main.cpp

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
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.

Opgave

Swipe to start coding

You need to calculate the average value of transactions in a banking system. To do this, use an array, a while loop, and variables to store the sum and the iterator. All the code should be implemented inside the calculateAverage function.

  1. Initialize the variable sum. It will hold the total of all transactions.
  2. Initialize the variable i. This will act as an iterator tracking the current position in the transactions.
  3. The while loop should run as long as i is less than the number of elements in the transactions (variable size).
  4. Inside the loop, add the value of the current transaction to the variable sum.
  5. Increment the variable i by one on each iteration.
  6. After the loop finishes, calculate the average by dividing sum by the number of transactions.

Løsning

solution.cpp

solution.cpp

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 2
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

Can you show me an example of using a counter variable in a while loop?

What happens if I forget to update the counter variable inside the loop?

How does changing the increment value affect the number of loop iterations?

close

Awesome!

Completion rate improved to 9.09

bookChallenge: Setting Iterations With The While Loop in C++

Stryg for at vise menuen

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:

main.cpp

main.cpp

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
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.

Opgave

Swipe to start coding

You need to calculate the average value of transactions in a banking system. To do this, use an array, a while loop, and variables to store the sum and the iterator. All the code should be implemented inside the calculateAverage function.

  1. Initialize the variable sum. It will hold the total of all transactions.
  2. Initialize the variable i. This will act as an iterator tracking the current position in the transactions.
  3. The while loop should run as long as i is less than the number of elements in the transactions (variable size).
  4. Inside the loop, add the value of the current transaction to the variable sum.
  5. Increment the variable i by one on each iteration.
  6. After the loop finishes, calculate the average by dividing sum by the number of transactions.

Løsning

solution.cpp

solution.cpp

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 2
single

single

some-alt