Loops
Loops, loops, loops, loops, loops.
StartKey Concepts
Review core concepts you need to learn to master this subject
while
Loop
while
Loop
while (password != 1234) {
std::cout << "Try again: ";
std::cin >> password;
}
A while
loop statement repeatedly executes the code block within as long as the condition is true
. The moment the condition becomes false
, the program will exit the loop.
Note that the while
loop might not ever run. If the condition is false
initially, the code block will be skipped.
Loops
Lesson 1 of 2
- 1A loop is a programming tool that repeats some code or a set of instructions until a specified condition is reached. As a programmer, you’ll find that you rely on loops all the time! You’ll h…
- 2So first up… the while loop! Before we dive deep into the syntax of the while loop, let’s do a demo. Inside enter_pin.cpp, we have a program that asks and checks for a password. It uses a w…
- 3So now that we got a demo of loops, let’s write one! The while loop looks very similar to an if statement. And just like an if statement, it executes the code inside of it if the condition is true…
- 4The last one we held your hand, so let’s try one on your own. As an example of iteration, we have the first program ever to run on a stored-program computer (the EDSAC ). It was written and run b…
- 5FoxTrot Iterating over a sequence of numbers is so common that C++, like most other programming languages, has a special syntax for it. When we know exactly how man…
- 6In the last exercise, we saw an example of an incrementing for loop so here we are going to show you how to write a for loop where the counter goes down. When we know exactly how many times we want…
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory