| by Arround The Web | No comments

For Loop C++

In C++, loop statements repeat a certain piece of code or statement. They are mostly used to shorten code by performing the same function more than once and to cut down on redundancy. For loops, while loops and do-while loops are only a few of the loop types supported by C++. Each has a unique syntax, benefits, and application. When we wish to run a block of code repeatedly, we utilize the programming control structure known as the loop. Typically, it keeps running and stops when a certain criterion is met. You will discover a crucial idea in this post, namely C++ For Loop.

What is a for Loop in C++?

This repetition control structure enables us to repeatedly loop over a certain area of C++ code. If the test expression returns true, a for loop is executed. As soon as the test expression is false, the loop stops running. Before each iteration, the condition must be checked. The body of the loop is run if the evaluation produces a true result.

Syntax of for Loop

Below, we have mentioned the syntax of the for loop in C++ language.


The ‘init expression’ is the first parameter. We must initialize the loop counter to a certain number in this expression. For instance, int i=1.

The ‘condition’ is the second parameter. We must test the condition here. If the criterion is fulfilled, the for loop will continue; if it is not, it will end. In the next example, if i <= 5. The Increment is the third and last parameter. This expression increases/decreases the loop variable by a specified number after the loop body has run. For instance: i++;.

Now let’s see some programming examples discussing the for loop.

Example 1:

In our first example, we used the for loop to print the first 15 natural integers. To show how the for loop functions, refer to the attached code.

The main method is launched after the iostream header file is inserted in the code. A number is started, a condition is written to see if it is less than or equal to 15. The number is increased after each repeat in the for loop. In the code, it is written as (number=4; number=15; number++).

The condition “number <= 15” is examined. The statements written in the loop’s body are executed if the condition is satisfied. If the condition returns false, the program will execute statements written inside the loop. And following the execution of the for loop’s body of code, the “number++” command is run. In this illustration, each time the for loop’s body code is executed, the value of “number” rises by 1. Any expression that you want to run after each loop can be used here.

In the example above, “number = 4” gives the variable “number” the value 4. Next, the “number<=15” condition is examined. The code in the for loop’s body is performed since the value of “number” is 4. As a result, the current value of “number,” which is 4, is printed.

After the for loop’s body of codes has been run, step “number++” is run, which raises the value of “number” by 1. Therefore, 5 is the new value of the variable “number.”

The condition “number<=15” is once more checked and found to be true because the value of “number” is 5. For loop’s body code is once more run, 5 is printed. Then, the value of “number” is once more increased.

When the value of “number” is changed to 15, the expression “number <= 15” evaluates to true, printing 15. The condition “number<=15” now becomes false and the loop ends when number++ raises the value of “number” to 16.

#include <iostream>

int main()

{

    using namespace std;
    int number;
    for( number = 4; number <= 15; number++ )
{
        cout << number << endl;
    }
    return 0;
}

Here is the output in which you can see the numbers from 4 to 15 are printed by using the for loop.

Example 2:

This is the second example where we will determine the factorial of a positive number. First, the iostream header file has been incorporated into our code. This file will let us read from and write to the console. Then, to access its classes and functions without calling it, we included the std namespace. The main() method, which should contain the program’s logic, has been called in the following line of the code. The main() function’s body is where the {   denotes its beginning. Here, the integer variables a, n, and fact have been declared. A value of 1 has been set to the fact variable. On the console, we have printed some text. “Please type any positive number:” is written in the text.

The user will be asked to input a value for the variable num in the text. A for loop is then built. An integer variable named ‘a’ is created during initialization and a value of 1 is given to it. The condition says a’s value must not be greater or equal to that of the value of variable ‘n’. After each iteration, the increment raises the value of “a” by 1. The for-loop’s body is introduced by the symbol (). The formula fact = fact * a was used in the following code to calculate the value of the factorial. The loop then comes to an end.

The values of the variables “n” and “fact” will be printed on the console along with additional text in the following line. If the program runs successfully, the main() function returns a value. Finally, the main() function’s body’s conclusion can be seen. Here is the whole code:

#include <iostream>

using namespace std;

int main()

{

    int a, n, fact = 1;
    cout <> n;
    for (a = 1; a <= n; ++a)
     {
        fact *= a;  
    }
    cout << "Here is the factorial of " << n << " = " << fact;
    return 0;

}


When we run the above, it will first prompt the user to provide any positive number. Upon doing so, the factorial of that number is provided.


Here, you can see the factorial of the given number is 5 in our case.

Conclusion

We have provided details about the C++ for loop in this article. Until a specific condition is met, a collection of statements is continually executed in a for loop. For your help, we have also provided two thorough examples.

Share Button

Source: linuxhint.com

Leave a Reply