Key Concepts

Review core concepts you need to learn to master this subject

while Loop in C++

while (guess != 8) { std::cout << "Try again: "; std::cin >> guess; }

In C++, a while loop statement repeatedly executes the code block within while the condition is true. The moment the condition becomes false, the loop is exited.

Note that the while loop might not ever run. If the condition is false initially, the code block will be skipped and the first statement after the while loop will be executed.

for Loop in C++

while (guess != 8) { std::cout << "Try again: "; std::cin >> guess; }

In C++, a for loop executes a code block a specific number of times. It has three parts:

  • The initialization of a counter
  • The continue condition
  • The increment/decrement of the counter

This example prints 1 to 10 on the screen.

  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 many times we want to ite…
  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…
  1. 1
    “First actual case of bug being found.” The story goes that on September 9 th , 1947, computer scientist Grace Hopper found a moth in the Harvard Mark II computer’s log book and reported the w…
  2. 2
    When we are writing C++ programs, the compiler is our first line of defense against errors. There are two types of compile-time errors: - Syntax errors: Errors that occur when we violate the …
  3. 3
    Sometimes the code compiles fine, but there is still a message because the program needs some function or library that it can’t find. This is known as a link-time error. As our program gets bigger…
  4. 4
    If our program has no compile-time errors and no link-time errors, it’ll run. This is where the fun really starts. Errors which happen during program execution (run-time) after successful compilat…
  5. 5
    Once we have removed the compile-time errors, link-time errors, and run-time errors, the program runs successfully. But sometimes, the program doesn’t do what we want it to do or no output is produ…
  6. 6
    Finding bugs is a huge part of a programmer’s life. Don’t be intimidated by them… embrace them. Errors in your code mean you’re trying to do something cool! In this lesson, we have learned about t…

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