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

Conteúdo do Curso

C++ Loops

C++ Loops

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

book
Nested While Loops

As we already know the while loop is like a set of instructions that a computer follows repeatedly as long as a certain condition is true. It's a way to automate tasks, especially when we don't know in advance how many times we need to repeat those tasks.

Now, why do we need nested while loops? Well, sometimes, we face situations where we need to do something repeatedly, and inside that repetitive task, there's another task that needs to be repeated as well. It's like having a task within a task. Nested while loops help us handle these situations.

cpp

main

copy
123456789101112131415161718
#include <iostream> int main() { bool we_have_baskets = true; while (we_have_baskets) { bool we_have_apples_in_busket = true; while (we_have_apples_in_busket) { // check if we still have apples in busket // if not set the we_have_apples_in_busket to false std::cout << "Marking an apple" << std::endl; } // check if we still have buskets // if not set the we_have_baskets to false } }

Note

Nested loops are a powerful tool, but they should be used judiciously and with care to ensure that your code remains readable, maintainable, and efficient.

Tarefa
test

Swipe to show code editor

Write a program to ensure a password meets the following requirements.

  1. At least 8 characters long.
  2. Contains at least one uppercase letter.
  3. Includes at least one number.

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 3. Capítulo 2
toggle bottom row

book
Nested While Loops

As we already know the while loop is like a set of instructions that a computer follows repeatedly as long as a certain condition is true. It's a way to automate tasks, especially when we don't know in advance how many times we need to repeat those tasks.

Now, why do we need nested while loops? Well, sometimes, we face situations where we need to do something repeatedly, and inside that repetitive task, there's another task that needs to be repeated as well. It's like having a task within a task. Nested while loops help us handle these situations.

cpp

main

copy
123456789101112131415161718
#include <iostream> int main() { bool we_have_baskets = true; while (we_have_baskets) { bool we_have_apples_in_busket = true; while (we_have_apples_in_busket) { // check if we still have apples in busket // if not set the we_have_apples_in_busket to false std::cout << "Marking an apple" << std::endl; } // check if we still have buskets // if not set the we_have_baskets to false } }

Note

Nested loops are a powerful tool, but they should be used judiciously and with care to ensure that your code remains readable, maintainable, and efficient.

Tarefa
test

Swipe to show code editor

Write a program to ensure a password meets the following requirements.

  1. At least 8 characters long.
  2. Contains at least one uppercase letter.
  3. Includes at least one number.

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