How To Use the For Loop In C++ With Examples
This guide will cover how to use the for loop in C++ and will contain multiple code examples of the loop in action. If you are interested in learning about while loops check this guide out. The for loop is an extremely valuable asset to and C++ programmers coding arsenal. Learning how to use it will no doubt improve your abilities as a programmer. It should be noted that the for loop is known as an iterative statement, basically meaning it executed multiple iterations of the same code. Let’s get started learning how to use the for loop by looking at some example programs. The first example below is a simple for loop that will take the initial condition where “i = 0”, run the code, then add 1 to the value of “i” until it reaches a value of 10. It will then output the value of “i” and end the line.
//C++ for loop example code 1 #include <iostream> int i; void exampleloop(){ for(i = 0; i <= 10; i++){ std::cout << i << std::endl; } } int main(){ exampleloop(); return 0; }
The output of the above for loop example code would be the following:
How Does The For Loop Work in C++? An Example Diagram…
If you are unfamiliar with loops in general basically a loop is a sequence of code that continuously runs until either a condition is met or is it manually told to stop. In the above code the bit after the ‘for(i = 0; i <= 10; i++)‘ in line 4 between the curly brackets {line 5 and 6} will run continuously until the condition is met. The for loop works with 3 statements. If you are unfamiliar with statements they are basically just telling the compiler something in code. Think of it like making a statement in real life except you are talking to a computer and are typing the words. The 3 statements the for loop runs and what they do are shown in a diagram below.
Basically the way to would read the aforementioned for loop’s statements would be the following:
For initial condition i = 0, as long as i <= 10 then run the code within this loop and after add 1 to i.
A Little More Complex Example Code
You can basically put anything inside of your for loop and it will continuously run over and over again until the condition is met. This is particularly useful for running bits of code a certain number of times. Another example code for using the for loop in C++ Is shown below.
//C++ for loop example code 2 #include <iostream> #include <cstdlib> #include <ctime> int i, x = 0; bool correct = false; bool failed = false; int randomNumber; void forloop(){ std::cout << "Please guess a number between 1 and 10." << std::endl; randomNumber = rand() % 10 + 1; for(i = 0; !correct && !failed; i++){ std::cin >> x; std::cout << "You have used " << i+1 << " out of 5 guesses! Please guess again!" << std::endl; if(x == randomNumber){ std::cout << std::endl << "You've guessed the number correctly!" << std::endl; correct = true; } else if(i >= 4){ std::cout << std::endl << "You are out of guesses! ):" << std::endl; failed = true; } } } int main(){ std::srand (std::time (0)); forloop(); return 0; }
This code is an example of a random number guessing game where the user has a set number of tries. In this case the user has 5 tries to correctly guess the number before the “You are out of guesses! ):” text prints to the console. In this for loop the conditions for the loop to run are that they haven’t failed (!failed) and that they haven’t guessed the number correctly (!correct). If at any time either of these become true the for loop stops running. An example output for the program if you guess the number correctly can be seen below to the left and if you fail to the right.
As you can see if you guess more than 5 times without guessing the correct answer the for loop will exit and the message “You are out of guesses! ):” prints to the console. Just like the first for loop example code this code has an initial condition, an exit condition, and a statement that runs each time the for loop loops. This statement uses two boolean variables as the exit conditions. If either of the boolean variables become true then it will exit the for loop. The IDE used in this guide is called DevC++. Hopefully this guide has helped you learn how to use the for loop in C++ and the example code was helpful. If you have any questions about for loops leave a comment below!
Leave A Comment
You must be logged in to post a comment.