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

Conteúdo do Curso

Introduction to C++

Introduction to C++

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

bookLoops in Arrays

Initialize the array declaring every time the single element can take a lot of your code. To go through the array you can use for loops. Let’s print each element of the array using loops:

1234
int numbers[5] = {1, 2, 3, 4, 5}; for (int i = 0; i <= 4; i++) { cout << numbers[i] << " "; }
copy

In this code, the for loop goes through each element by its index and print in such a way the whole array.

The last index of the array is 4 since we have 5 elements. Keep in mind that the first element’s index is 0 that’s why the for loop initialized the variable i to 0.

We can also define each element in the loops (for example, by its index):

12345
int numbers[5]; for (int i = 0; i <= 4; i++) { numbers[i] = i; cout << numbers[i] << " "; }
copy

Tarefa

Print each element of the array with its index.

  1. Define the array.
  2. Use for loop to go through array.
  3. In output define the element and index in the gaps.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 5. Capítulo 2
toggle bottom row

bookLoops in Arrays

Initialize the array declaring every time the single element can take a lot of your code. To go through the array you can use for loops. Let’s print each element of the array using loops:

1234
int numbers[5] = {1, 2, 3, 4, 5}; for (int i = 0; i <= 4; i++) { cout << numbers[i] << " "; }
copy

In this code, the for loop goes through each element by its index and print in such a way the whole array.

The last index of the array is 4 since we have 5 elements. Keep in mind that the first element’s index is 0 that’s why the for loop initialized the variable i to 0.

We can also define each element in the loops (for example, by its index):

12345
int numbers[5]; for (int i = 0; i <= 4; i++) { numbers[i] = i; cout << numbers[i] << " "; }
copy

Tarefa

Print each element of the array with its index.

  1. Define the array.
  2. Use for loop to go through array.
  3. In output define the element and index in the gaps.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 5. Capítulo 2
toggle bottom row

bookLoops in Arrays

Initialize the array declaring every time the single element can take a lot of your code. To go through the array you can use for loops. Let’s print each element of the array using loops:

1234
int numbers[5] = {1, 2, 3, 4, 5}; for (int i = 0; i <= 4; i++) { cout << numbers[i] << " "; }
copy

In this code, the for loop goes through each element by its index and print in such a way the whole array.

The last index of the array is 4 since we have 5 elements. Keep in mind that the first element’s index is 0 that’s why the for loop initialized the variable i to 0.

We can also define each element in the loops (for example, by its index):

12345
int numbers[5]; for (int i = 0; i <= 4; i++) { numbers[i] = i; cout << numbers[i] << " "; }
copy

Tarefa

Print each element of the array with its index.

  1. Define the array.
  2. Use for loop to go through array.
  3. In output define the element and index in the gaps.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Initialize the array declaring every time the single element can take a lot of your code. To go through the array you can use for loops. Let’s print each element of the array using loops:

1234
int numbers[5] = {1, 2, 3, 4, 5}; for (int i = 0; i <= 4; i++) { cout << numbers[i] << " "; }
copy

In this code, the for loop goes through each element by its index and print in such a way the whole array.

The last index of the array is 4 since we have 5 elements. Keep in mind that the first element’s index is 0 that’s why the for loop initialized the variable i to 0.

We can also define each element in the loops (for example, by its index):

12345
int numbers[5]; for (int i = 0; i <= 4; i++) { numbers[i] = i; cout << numbers[i] << " "; }
copy

Tarefa

Print each element of the array with its index.

  1. Define the array.
  2. Use for loop to go through array.
  3. In output define the element and index in the gaps.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 5. Capítulo 2
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
some-alt