Conteúdo do Curso
Introduction to C++
Introduction to C++
While Loop
What is a loop in C++? It’s a cycle which repeats until the certain condition is true.
For example, one of the most popular loops is while
:
As long as the specified condition is true we repeat doing the code in the while cycle. When the condition becomes false, the program starts to execute the line that follows this loop. An example of the while loop:
int i = 0; while (i < 3) { i ++; cout << i << endl; }
Initially, we declare the variable i
as 0
. Then the while
loop checks the condition (i < 3
) by each iteration and if the statement is true, we increment the variable i
and print it. By the third iteration, i
becomes 3
and the program doesn’t execute the code in the while loop again since the condition is false.
You can also use the while loop to interact with the user’s input or do something certain number of times. For example, calculate the sum of 3 user’s inputs:
By each iteration we add the user’s input number to the variable sumInputs
- our sum and increment i
since to execute the code in the block 3 times.
Be careful writing the loops! Increase/decrease the value in the condition or your loop will never end!
Tarefa
Calculate the sum from 3 to 15 using the while
loop:
- Create a variable
i
type ofint
with value3
. - Using
while
loop, add all the numbers less or equal to15
and bigger or equal to3
. Assign the sum to the variablesum
. Do not forget to increase your number on each step (i ++
). - Print the variable
sum
.
Please, don’t forget to type the semicolumn at the end of the lines.
Obrigado pelo seu feedback!
While Loop
What is a loop in C++? It’s a cycle which repeats until the certain condition is true.
For example, one of the most popular loops is while
:
As long as the specified condition is true we repeat doing the code in the while cycle. When the condition becomes false, the program starts to execute the line that follows this loop. An example of the while loop:
int i = 0; while (i < 3) { i ++; cout << i << endl; }
Initially, we declare the variable i
as 0
. Then the while
loop checks the condition (i < 3
) by each iteration and if the statement is true, we increment the variable i
and print it. By the third iteration, i
becomes 3
and the program doesn’t execute the code in the while loop again since the condition is false.
You can also use the while loop to interact with the user’s input or do something certain number of times. For example, calculate the sum of 3 user’s inputs:
By each iteration we add the user’s input number to the variable sumInputs
- our sum and increment i
since to execute the code in the block 3 times.
Be careful writing the loops! Increase/decrease the value in the condition or your loop will never end!
Tarefa
Calculate the sum from 3 to 15 using the while
loop:
- Create a variable
i
type ofint
with value3
. - Using
while
loop, add all the numbers less or equal to15
and bigger or equal to3
. Assign the sum to the variablesum
. Do not forget to increase your number on each step (i ++
). - Print the variable
sum
.
Please, don’t forget to type the semicolumn at the end of the lines.
Obrigado pelo seu feedback!
While Loop
What is a loop in C++? It’s a cycle which repeats until the certain condition is true.
For example, one of the most popular loops is while
:
As long as the specified condition is true we repeat doing the code in the while cycle. When the condition becomes false, the program starts to execute the line that follows this loop. An example of the while loop:
int i = 0; while (i < 3) { i ++; cout << i << endl; }
Initially, we declare the variable i
as 0
. Then the while
loop checks the condition (i < 3
) by each iteration and if the statement is true, we increment the variable i
and print it. By the third iteration, i
becomes 3
and the program doesn’t execute the code in the while loop again since the condition is false.
You can also use the while loop to interact with the user’s input or do something certain number of times. For example, calculate the sum of 3 user’s inputs:
By each iteration we add the user’s input number to the variable sumInputs
- our sum and increment i
since to execute the code in the block 3 times.
Be careful writing the loops! Increase/decrease the value in the condition or your loop will never end!
Tarefa
Calculate the sum from 3 to 15 using the while
loop:
- Create a variable
i
type ofint
with value3
. - Using
while
loop, add all the numbers less or equal to15
and bigger or equal to3
. Assign the sum to the variablesum
. Do not forget to increase your number on each step (i ++
). - Print the variable
sum
.
Please, don’t forget to type the semicolumn at the end of the lines.
Obrigado pelo seu feedback!
What is a loop in C++? It’s a cycle which repeats until the certain condition is true.
For example, one of the most popular loops is while
:
As long as the specified condition is true we repeat doing the code in the while cycle. When the condition becomes false, the program starts to execute the line that follows this loop. An example of the while loop:
int i = 0; while (i < 3) { i ++; cout << i << endl; }
Initially, we declare the variable i
as 0
. Then the while
loop checks the condition (i < 3
) by each iteration and if the statement is true, we increment the variable i
and print it. By the third iteration, i
becomes 3
and the program doesn’t execute the code in the while loop again since the condition is false.
You can also use the while loop to interact with the user’s input or do something certain number of times. For example, calculate the sum of 3 user’s inputs:
By each iteration we add the user’s input number to the variable sumInputs
- our sum and increment i
since to execute the code in the block 3 times.
Be careful writing the loops! Increase/decrease the value in the condition or your loop will never end!
Tarefa
Calculate the sum from 3 to 15 using the while
loop:
- Create a variable
i
type ofint
with value3
. - Using
while
loop, add all the numbers less or equal to15
and bigger or equal to3
. Assign the sum to the variablesum
. Do not forget to increase your number on each step (i ++
). - Print the variable
sum
.
Please, don’t forget to type the semicolumn at the end of the lines.