Basic Loops in C++
Airfare Daily Deals eCigarettes Eyeglasses Hotels Jewelry Online Backup Online Dating Online Printing Online Tickets Skin Care Textbook Rentals Vitamins Web Hosting Weddings
Find thousands of shopping-related forums
SEARCH

Basic Loops in C++

This article handles about loops in C++. When you’re programming in C++, you sometimes want to have a block of code run multiple times. Instead of typing it over and over again, and making your code very complicated, you can use a loop. There are many ways to use loops, and we’re going to go over two of them: the for loop and the while loop.

The while loop 

The while loop is the easiest, most basic way to create a loop. Let me give an example of a while loop:

int number = 1;

while (number <= 10) {

cout << “Number is equal to ” << number << endl;

}

Let’s go over this code. The first line initializes a variable called “number”, and gives it a value of 1. The second line is the start of the actual while loop. You start with the word “while”, and then you insert a test between the brackets, just like in an if statement. If you don’t know what an if statement is, you can learn all about it in this article. The while loop is going to check the test over and over again, and as long as the test returns true, the code between the braces will be executed. Now, if you run this program, you see that it will print “Number is equal to 1”, over and over again to the console, and it doesn’t stop. Now, sometimes you would want a loop never to stop, but mostly, you want it to run a certain amount of times, like 5, 15, 20, etc. So how do you accomplish this? It’s easy, just add this line between the cout command and the last brace: number++;. This line increments the value of number with one. Now, the code is going to check if number is smaller than 10, and increment it every time, so that at some point the value will be 11, and the loop will stop running. Now, if you run this program, you see that it prints 10 sentences to the screen. You can change the amount of sentences by changing the test of the while loop, for example: while (number <= 5) will make 5 sentences appear on the screen.

The for loop

The for loop is a little more complicated than the while loop, but it’s still very simple and useful. Let me give an example of a for loop:

for (int x=1; x<=10; x++) {

cout << “Number is equal to “ << x << endl;

}

This code will do the exact same thing as the while loop, but the syntax is different. Instead of only the test, the brackets hold three parts of code, first is the declaration of the variable Number (here called x), next is the test, and next is the value you want the variable to be incremented with. The only difference here with the while loop is that the for loop saves you even more lines.

Need an answer?
Get insightful answers from community-recommended
experts
in Computer Programming & Languages on Knoji.
Would you recommend this author as an expert in Computer Programming & Languages?
You have 0 recommendations remaining to grant today.
Comments (0)
ARTICLE DETAILS
RELATED ARTICLES
ARTICLE KEYWORDS