Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
For Loop | Loops
Introduction to C++
course content

Contenido del Curso

Introduction to C++

Introduction to C++

1. Basics
2. Variables
3. Conditional Statements
4. Loops
5. Intro to Arrays

For Loop

If you want to execute the code a certain number of times use the for loop.

Syntax:

It looks a bit difficult but let’s understand what is actually going on:

  • The statement#1 is an initial statement. It is executed at the beginning of for loop and doesn’t repeat later.
  • The statement#2 is the condition. The code block in the loop is executed if the condition is true (for example x < 4).
  • The statement#3 is executed every time after the code block execution (in most cases it’s this statement increment/decremant the control variable of the loop).

The loop body stops execution if the condition is false.

Let’s look at the example:

123
for (int x = 1; x < 5; x++) { cout << x << endl; }
copy

This code prints numbers from 1 to 4. Firstly the code sets in the variable x the value 1 before the loop starts. Then we mention that the program should execute the block while the x is less than 5 (x<5) and increment it by 1 every time after the code block execution.

You can also change statements in any convenient way. For example, we want to print all even numbers descending from 10 to 0:

123
for (int x = 10; x >= 0; x-=2) { cout << x << endl; }
copy

Tarea

The office employee makes 13 calls per hour, he works 8 hours a day. Write a program that will output the number of calls the worker has made by each hour.

  1. Declare the variable calls type of int.
  2. Use for loop to calculate the hours.
  3. Calculate the number of calls by multiplying the variables i and 13.
  4. Print the variable calls in the for loop.

Please, don’t forget to type the semicolumn at the end of the lines.

Tarea

The office employee makes 13 calls per hour, he works 8 hours a day. Write a program that will output the number of calls the worker has made by each hour.

  1. Declare the variable calls type of int.
  2. Use for loop to calculate the hours.
  3. Calculate the number of calls by multiplying the variables i and 13.
  4. Print the variable calls in the for loop.

Please, don’t forget to type the semicolumn at the end of the lines.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Sección 4. Capítulo 3
toggle bottom row

For Loop

If you want to execute the code a certain number of times use the for loop.

Syntax:

It looks a bit difficult but let’s understand what is actually going on:

  • The statement#1 is an initial statement. It is executed at the beginning of for loop and doesn’t repeat later.
  • The statement#2 is the condition. The code block in the loop is executed if the condition is true (for example x < 4).
  • The statement#3 is executed every time after the code block execution (in most cases it’s this statement increment/decremant the control variable of the loop).

The loop body stops execution if the condition is false.

Let’s look at the example:

123
for (int x = 1; x < 5; x++) { cout << x << endl; }
copy

This code prints numbers from 1 to 4. Firstly the code sets in the variable x the value 1 before the loop starts. Then we mention that the program should execute the block while the x is less than 5 (x<5) and increment it by 1 every time after the code block execution.

You can also change statements in any convenient way. For example, we want to print all even numbers descending from 10 to 0:

123
for (int x = 10; x >= 0; x-=2) { cout << x << endl; }
copy

Tarea

The office employee makes 13 calls per hour, he works 8 hours a day. Write a program that will output the number of calls the worker has made by each hour.

  1. Declare the variable calls type of int.
  2. Use for loop to calculate the hours.
  3. Calculate the number of calls by multiplying the variables i and 13.
  4. Print the variable calls in the for loop.

Please, don’t forget to type the semicolumn at the end of the lines.

Tarea

The office employee makes 13 calls per hour, he works 8 hours a day. Write a program that will output the number of calls the worker has made by each hour.

  1. Declare the variable calls type of int.
  2. Use for loop to calculate the hours.
  3. Calculate the number of calls by multiplying the variables i and 13.
  4. Print the variable calls in the for loop.

Please, don’t forget to type the semicolumn at the end of the lines.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Sección 4. Capítulo 3
toggle bottom row

For Loop

If you want to execute the code a certain number of times use the for loop.

Syntax:

It looks a bit difficult but let’s understand what is actually going on:

  • The statement#1 is an initial statement. It is executed at the beginning of for loop and doesn’t repeat later.
  • The statement#2 is the condition. The code block in the loop is executed if the condition is true (for example x < 4).
  • The statement#3 is executed every time after the code block execution (in most cases it’s this statement increment/decremant the control variable of the loop).

The loop body stops execution if the condition is false.

Let’s look at the example:

123
for (int x = 1; x < 5; x++) { cout << x << endl; }
copy

This code prints numbers from 1 to 4. Firstly the code sets in the variable x the value 1 before the loop starts. Then we mention that the program should execute the block while the x is less than 5 (x<5) and increment it by 1 every time after the code block execution.

You can also change statements in any convenient way. For example, we want to print all even numbers descending from 10 to 0:

123
for (int x = 10; x >= 0; x-=2) { cout << x << endl; }
copy

Tarea

The office employee makes 13 calls per hour, he works 8 hours a day. Write a program that will output the number of calls the worker has made by each hour.

  1. Declare the variable calls type of int.
  2. Use for loop to calculate the hours.
  3. Calculate the number of calls by multiplying the variables i and 13.
  4. Print the variable calls in the for loop.

Please, don’t forget to type the semicolumn at the end of the lines.

Tarea

The office employee makes 13 calls per hour, he works 8 hours a day. Write a program that will output the number of calls the worker has made by each hour.

  1. Declare the variable calls type of int.
  2. Use for loop to calculate the hours.
  3. Calculate the number of calls by multiplying the variables i and 13.
  4. Print the variable calls in the for loop.

Please, don’t forget to type the semicolumn at the end of the lines.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

If you want to execute the code a certain number of times use the for loop.

Syntax:

It looks a bit difficult but let’s understand what is actually going on:

  • The statement#1 is an initial statement. It is executed at the beginning of for loop and doesn’t repeat later.
  • The statement#2 is the condition. The code block in the loop is executed if the condition is true (for example x < 4).
  • The statement#3 is executed every time after the code block execution (in most cases it’s this statement increment/decremant the control variable of the loop).

The loop body stops execution if the condition is false.

Let’s look at the example:

123
for (int x = 1; x < 5; x++) { cout << x << endl; }
copy

This code prints numbers from 1 to 4. Firstly the code sets in the variable x the value 1 before the loop starts. Then we mention that the program should execute the block while the x is less than 5 (x<5) and increment it by 1 every time after the code block execution.

You can also change statements in any convenient way. For example, we want to print all even numbers descending from 10 to 0:

123
for (int x = 10; x >= 0; x-=2) { cout << x << endl; }
copy

Tarea

The office employee makes 13 calls per hour, he works 8 hours a day. Write a program that will output the number of calls the worker has made by each hour.

  1. Declare the variable calls type of int.
  2. Use for loop to calculate the hours.
  3. Calculate the number of calls by multiplying the variables i and 13.
  4. Print the variable calls in the for loop.

Please, don’t forget to type the semicolumn at the end of the lines.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 4. Capítulo 3
Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt