How To Update A Variable Outside A While Loop C
In calculator programming, loops are used to repeat a block of lawmaking.
For instance, permit's say we desire to show a message 100 times. So instead of writing the print statement 100 times, we can use a loop.
That was just a simple case; we tin can achieve much more efficiency and sophistication in our programs past making effective use of loops.
There are 3 types of loops in C++.
-
for
loop -
while
loop -
do...while
loop
This tutorial focuses on C++ for
loop. Nosotros will learn about the other type of loops in the upcoming tutorials.
C++ for loop
The syntax of for-loop is:
for (initialization; condition; update) { // body of-loop }
Hither,
-
initialization
- initializes variables and is executed only in one case -
condition
- iftrue
, the torso offor
loop is executed
iffake
, the for loop is terminated -
update
- updates the value of initialized variables and over again checks the condition
To learn more about conditions
, bank check out our tutorial on C++ Relational and Logical Operators.
Flowchart of for Loop in C++
Example one: Printing Numbers From i to v
#include <iostream> using namespace std; int chief() { for (int i = 1; i <= v; ++i) { cout << i << " "; } render 0; }
Output
1 2 3 4 5
Here is how this plan works
Iteration | Variable | i <= 5 | Action |
---|---|---|---|
1st | i = 1 | true | i is printed. i is increased to ii . |
2nd | i = 2 | true | 2 is printed. i is increased to 3 . |
third | i = 3 | true | three is printed. i is increased to 4 . |
4th | i = 4 | truthful | four is printed. i is increased to 5 . |
5th | i = v | true | 5 is printed. i is increased to six . |
6th | i = 6 | simulated | The loop is terminated |
Case 2: Display a text 5 times
// C++ Plan to display a text 5 times #include <iostream> using namespace std; int chief() { for (int i = 1; i <= 5; ++i) { cout << "Howdy World! " << endl; } return 0; }
Output
Hullo World! Hello Globe! Hello World! Howdy Globe! Hello Globe!
Here is how this program works
Iteration | Variable | i <= 5 | Action |
---|---|---|---|
1st | i = ane | true | Hello Earth! is printed and i is increased to ii . |
2nd | i = 2 | truthful | Hello World! is printed and i is increased to 3 . |
3rd | i = three | true | Hello World! is printed and i is increased to 4 . |
4th | i = iv | true | Hello World! is printed and i is increased to 5 . |
5th | i = 5 | true | Howdy World! is printed and i is increased to 6 . |
6th | i = 6 | false | The loop is terminated |
Case iii: Find the sum of first n Natural Numbers
// C++ programme to notice the sum of offset due north natural numbers // positive integers such as 1,2,3,...n are known as natural numbers #include <iostream> using namespace std; int main() { int num, sum; sum = 0; cout << "Enter a positive integer: "; cin >> num; for (int i = 1; i <= num; ++i) { sum += i; } cout << "Sum = " << sum << endl; return 0; }
Output
Enter a positive integer: 10 Sum = 55
In the above example, nosotros have two variables num and sum. The sum variable is assigned with 0 and the num variable is assigned with the value provided by the user.
Annotation that nosotros have used a for
loop.
for(int i = 1; i <= num; ++i)
Here,
-
int i = i
: initializes the i variable -
i <= num
: runs the loop as long as i is less than or equal to num -
++i
: increases the i variable by 1 in each iteration
When i becomes 11
, the status
is false
and sum will be equal to 0 + 1 + 2 + ... + 10
.
Ranged Based for Loop
In C++eleven, a new range-based for
loop was introduced to work with collections such as arrays and vectors. Its syntax is:
for (variable : collection) { // body of loop }
Here, for every value in the drove, the for loop is executed and the value is assigned to the variable.
Instance 4: Range Based for Loop
#include <iostream> using namespace std; int principal() { int num_array[] = {1, 2, 3, 4, 5, 6, vii, viii, 9, 10}; for (int northward : num_array) { cout << due north << " "; } render 0; }
Output
1 2 3 four 5 vi 7 8 9 ten
In the above program, we have declared and initialized an int
array named num_array. It has 10 items.
Here, we accept used a range-based for
loop to access all the items in the array.
C++ Space for loop
If the status
in a for
loop is ever true
, it runs forever (until memory is full). For instance,
// infinite for loop for(int i = 1; i > 0; i++) { // cake of lawmaking }
In the above programme, the status
is always true
which volition then run the code for space times.
Check out these examples to learn more:
- C++ Plan to Calculate Sum of Natural Numbers
- C++ Program to Find Factorial
- C++ Program to Generate Multiplication Table
In the next tutorial, we will learn about while
and practice...while
loop.
How To Update A Variable Outside A While Loop C,
Source: https://www.programiz.com/cpp-programming/for-loop
Posted by: frasierpree1974.blogspot.com
0 Response to "How To Update A Variable Outside A While Loop C"
Post a Comment