for Loops in C++
By: Baski
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; }
Archived Comments
Comment on this tutorial
- Data Science
- Android
- 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++