Key Concepts

Review core concepts you need to learn to master this subject

while loop in C++.

#include <iostream> using namespace std; int main() { int i; i = 1; while(i <= 10) { // execute while i is less than or equal to 10 cout << i << " "; i++; } }

In C++, while loop is executed repeatedly while the condition is true. The moment condition becomes false, the loop is exited.

for loop in C++.

#include <iostream> using namespace std; int main() { int i; i = 1; while(i <= 10) { // execute while i is less than or equal to 10 cout << i << " "; i++; } }

A for loop in C++ executes a specific number of times. It has three parts: initialization to set the value, condition to test if the loop should continue, and increment/decrement to modify the loop variable.

This example prints 1 to 10 on screen.

Logical operators in C++.

#include <iostream> using namespace std; int main() { int i; i = 1; while(i <= 10) { // execute while i is less than or equal to 10 cout << i << " "; i++; } }

In C++, we use logical operators to combine two different conditions. Each condition is evaluated independently and the final result is decided by logical operators.

Following are the logical operators:

  • && Logical and operator
  • || Logical or operator
  • ! Logical not opertor

&& logical operator requires both conditions to be true. || logical operator requires either of the condition to be true. ! Logical operator negates the result.

Loops
Lesson 1 of 1
  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 […] loop! Before we dive deep into the syntax of the […] loop, let’s do a demo. Inside enter_pin.cpp, we have a program that asks and checks for a password. It uses…

  3. 3

    So now that we got a demo of loops, let’s write one! The […] loop looks very similar to an […] statement. And just like an […] statement, it executes the code inside of it if the condi…

  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 […] loop so here we are going to show you how to write a […] loop where the counter goes down. When we know exactly how many times…

  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 […] loops with a continue condi…

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