Wednesday 21 September 2016

for loop

A for loop is a programming language statement which allows code to be repeatedly executed.
The syntax for this is
for ( <expression_1> ; <expression_2> ; <expression_3> )
    <statement>
  • expression_1 is used for intializing variables which are generally used for controlling terminating flag for the loop.
  • expression_2 is used to check for the terminating condition. If this evaluates to false, then the loop is terminated.
  • expression_3 is generally used to update the flags/variables.
A sample loop will be
for(int i = 0; i < 10; i++) {
    <statements>;
}
 
Ex:-
for (int i = a; i <= b; ++i) {
    if (i > 9) {
        i % 2 == 0 ? cout << "even\n" : cout << "odd\n";
    } 

No comments:

Post a Comment