for Loops in C++
By: Baski in C++ Tutorials on 2007-09-09
When programming while loops, you'll often find yourself setting up a starting condition, testing to see if the condition is true, and incrementing or otherwise changing a variable each time through the loop. The following program demonstrates this.
1: // 2: // Looping with while 3: 4: #include <iostream.h> 5: 6: int main() 7: { 8: int counter = 0; 9: 10: while(counter < 5) 11: { 12: counter++; 13: cout << "Looping! "; 14: } 15: 16: cout << "\nCounter: " << counter << ".\n"; 17: return 0; 18: } Output: Looping! Looping! Looping! Looping! Looping! Counter: 5.
Analysis: The condition is set on
line 8: counter is initialized to 0. On line 10, counter
is tested to see whether it is less than 5. counter is incremented on
line 12. On line 16, a simple message is printed, but you can imagine that more
important work could be done for each increment of the counter.
A for loop combines three steps into one statement. The three steps are
initialization, test, and increment. A for statement consists of the
keyword for followed by a pair of parentheses. Within the parentheses
are three statements separated by semicolons.
The first statement is the initialization. Any legal C++ statement can be put here, but typically this is used to create and initialize a counting variable. Statement 2 is the test, and any legal C++ expression can be used here. This serves the same role as the condition in the while loop. Statement 3 is the action. Typically a value is incremented or decremented, though any legal C++ statement can be put here. Note that statements 1 and 3 can be any legal C++ statement, but statement 2 must be an expression--a C++ statement that returns a value. The program below demonstrates a for loop.
1: // 2: // Looping with for 3: 4: #include <iostream.h> 5: 6: int main() 7: { 8: int counter; 9: for (counter = 0; counter < 5; counter++) 10: cout << "Looping! "; 11: 12: cout << "\nCounter: " << counter << ".\n"; 13: return 0; 14: } Output: Looping! Looping! Looping! Looping! Looping! Counter: 5.
Analysis: The for statement on line 8 combines the initialization of counter, the test that counter is less than 5, and the increment of counter all into one line. The body of the for statement is on line 9. Of course, a block could be used here as well.
The for Statement
The syntax for the for statement is as follows:
for (initialization; test; action ) statement;
The initialization statement is used to initialize the state of a counter, or to otherwise prepare for the loop. test is any C++ expression and is evaluated each time through the loop. If test is TRUE, the action in the header is executed (typically the counter is incremented) and then the body of the for loop is executed. Example 1
// print Hello ten times for (int i = 0; i<10; i++) cout << "Hello! ";
Example 2
for (int i = 0; i < 10; i++) { cout << "Hello!" << endl; cout << "the value of i is: " << i << endl; }
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Calculating total based on the given quantity and price in C++
Sorting an array of Strings in C++
Matrix using nested for loops in C++
Compute the square root of the sum of the squares of an array in C++
Calculate average using Two-Dimensional Array in C++
Two-Dimensional Array Manipulation in C++
Compiling and Linking Multiple Source Files in C++
Escape Sequences for Nonprintable Characters in C++
Using the Built-in Arithmetic Types in C++
Comments