Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Introduction to While Loop | While Loop
course content

Course Content

C++ Loops

Introduction to While LoopIntroduction to While Loop

Loops are essential to programming because they let you repeat actions or tasks without writing the same code over and over again.

In this chapter, we'll dive into the while loop. To help illustrate the idea behind the while loop, imagine you love coffee so much that you go to the coffee shop everyday.

You keep going there as long as it's open and your routine remains unchanged, repeating the same actions with each visit. However, once the shop closes, you stop visiting.

A while loop works exactly the same, it performs a series of actions over and over again as long as a particular condition remains true, and it stops executing when that condition becomes false.

In C++ to create this loop, we use the while keyword. Following the keyword, we specify the condition within parentheses, and then within curly braces, we provide the instructions to be executed repeatedly as long as the condition remains true.

Translating the coffee shop example into code, it would resemble the following code snippet. Look at the code below:

cpp

main.cpp

Note

This is an infinite loop because the condition always remains true. We will consider infinite loops in detail in future chapters.

There also can be multiple conditions in the loop using operators && and || we learned earlier. For example, In the context of the coffee shop, we will visit it when it is open and when we have money. Both of these conditions must be met for us to continue going there. If the coffee shop is open but we don't have money, we will not go there.

Choose the correct structure of the while loop:

Select the correct answer

Everything was clear?

Section 1. Chapter 1
course content

Course Content

C++ Loops

Introduction to While LoopIntroduction to While Loop

Loops are essential to programming because they let you repeat actions or tasks without writing the same code over and over again.

In this chapter, we'll dive into the while loop. To help illustrate the idea behind the while loop, imagine you love coffee so much that you go to the coffee shop everyday.

You keep going there as long as it's open and your routine remains unchanged, repeating the same actions with each visit. However, once the shop closes, you stop visiting.

A while loop works exactly the same, it performs a series of actions over and over again as long as a particular condition remains true, and it stops executing when that condition becomes false.

In C++ to create this loop, we use the while keyword. Following the keyword, we specify the condition within parentheses, and then within curly braces, we provide the instructions to be executed repeatedly as long as the condition remains true.

Translating the coffee shop example into code, it would resemble the following code snippet. Look at the code below:

cpp

main.cpp

Note

This is an infinite loop because the condition always remains true. We will consider infinite loops in detail in future chapters.

There also can be multiple conditions in the loop using operators && and || we learned earlier. For example, In the context of the coffee shop, we will visit it when it is open and when we have money. Both of these conditions must be met for us to continue going there. If the coffee shop is open but we don't have money, we will not go there.

Choose the correct structure of the while loop:

Select the correct answer

Everything was clear?

Section 1. Chapter 1
some-alt