You are on page 1of 2

For loop:

The statements in the for loop repeat continuously for a specific number of times. The while and
do-while loops repeat until a certain condition is met. The for loop repeats until a specific count
is met. Use a for loop when the number of repetition is known, or can be supplied by the user.
The coding format is:

Syntax:

for(startExpression; testExpression; countExpression)


{ block of code;
}

The startExpression is evaluated before the loop begins. It is acceptable to declare and assign in
thestartExpression (such as int x = 1;). This startExpression is evaluated only once at the
beginning of the loop.

The testExpression will evaluate to TRUE (nonzero) or FALSE (zero). While TRUE, the body of
the loop repeats. When the testExpression becomes FALSE, the looping stops and the program
continues with the statement immediately following the for loop body in the program code.

The countExpression executes after each trip through the loop. The count may increase/decrease
by an increment of 1 or of some other value.

Braces are not required if the body of the for loop consists of only ONE statement.

Nested loops:

The placing of one loop inside the body of another loop is called nesting. When you "nest"
twoloops, the outer loop takes control of the number of complete repetitions of the inner
loop. While all types of loops may be nested, the most commonly nested loops are for loops.

Syntax of Nested Loops

for(initialization; test; increment)


{
statements;
for(initialization; test; increment)
{
statements; |
|
|
for(initialization; test; increment)
{
statements;
}
}
}

Tasks:

1. Write a program that reads five numbers (each between 1 and 30). For each number
read, your program should print a line containing that numberof adjacent asterisks. For
example, if your program reads the number seven, it should print *******.

2. Count the number of Odd numbers between 0 n 100, Also print every time you get
multiple of 10 (e.g., 10 , 20 , 30 etc. ). Use For Loop, arithmetic and relational operators.

You might also like