Key Concepts

Review core concepts you need to learn to master this subject

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.

Lesson 1 of 2
  1. 1
    A 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…
  2. 2
    So 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…
  3. 3
    So 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…
  4. 4
    The 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…
  5. 5
    FoxTrot 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…
  6. 6
    In 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…
  7. 7
    Great job! Key concepts covered in this lesson: - Loops perform repetitive actions so we don’t have to code those actions manually every time. - How to write while loops with a continue conditi…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo