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

Course Content

C++ Introduction

While LoopWhile Loop

We used if...else, switch-case to compare our variables with other values. But what if we need to do something a hundred times? A thousand times? A million times?


Loops are designed for just such cases! It allows you to loop your program under certain conditions. Structure of while loop:

cpp

main.cpp

We have summed up (x+=1) 992 times in this case. The loop ran while x + y was not equal to result (1000).

As soon as the expression x + y became equal to result, the loop ended, and we got the root of the equation (х).

Note

The loop may not start if the condition is not satisfied.

It is crucial to make sure that the loop has an exit condition, that is, that the loop will not be infinite. The infinite loop example:

cpp

main.cpp

Choose the correct version of the while loop:

Select the correct answer

Everything was clear?

Section 4. Chapter 4
course content

Course Content

C++ Introduction

While LoopWhile Loop

We used if...else, switch-case to compare our variables with other values. But what if we need to do something a hundred times? A thousand times? A million times?


Loops are designed for just such cases! It allows you to loop your program under certain conditions. Structure of while loop:

cpp

main.cpp

We have summed up (x+=1) 992 times in this case. The loop ran while x + y was not equal to result (1000).

As soon as the expression x + y became equal to result, the loop ended, and we got the root of the equation (х).

Note

The loop may not start if the condition is not satisfied.

It is crucial to make sure that the loop has an exit condition, that is, that the loop will not be infinite. The infinite loop example:

cpp

main.cpp

Choose the correct version of the while loop:

Select the correct answer

Everything was clear?

Section 4. Chapter 4
some-alt