How do I use the for loop statement?
Category: fundamental, viewed: 888 time(s).
The for loop can be use to iterate over a range of values. For instance if you want to iterate from zero to 10 or if you want to iterate through all the items of an array. Below you'll see two forms of a for loop. The first one is the general form of a for loop and the second one is an enhanced for loop that also known as the for..each loop.
The general form of for loop consists of three parts:
for (initialization; termination; increment) {
....
}
- The
initialization: it initializes the loop, it executed once at the beginning of the loop. - The
termination: the loop executes as long as the termination evaluates totrue. - The
increment: it executed at the end of every loop, the expression can be either an increment or decrement.
The result of the program is:
i = 0 i = 1 i = 2 i = 3 i = 4 i = 5 i = 6 i = 7 i = 8 i = 9 i = 10 number = 0 number = 1 number = 2 number = 3 number = 4 number = 5 number = 6 number = 7 number = 8 number = 9 number = 10
Download Hundreds of Complimentary Industry Resources
Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more;
all available at no cost to you. With more than 600 complimentary offers, you'll find
plenty of titles to suit your professional interests and needs.
Click Here and Sign up today!


