for

Iterates through the loop, starting from the specified initial condition and will iterate through the statements enclosed within the { code block } until the test is false. The initial condition, test and iterator are separated with a semi-colon ;

 

Syntax:

for (initialisertestiterator ) {

statements ;

}

 

#include <iostream>
using namespace std ;

int main () {

	for (int myVar = 0 ; myVar < 5 ; myVar++) {

		cout << "myVar is now: " << myVar << endl ;
	}
	return 0;
}

Compile & Run:

myVar is now: 0
myVar is now: 1
myVar is now: 2
myVar is now: 3
myVar is now: 4

Leave a Reply