The for loop is also entry-controlled loop.
Syntax:
for(initialization; condition; increment/decrement)
{
Body of loop
}
Initialization: in this part of the loop the variable is initialized such as i=0; and this is the control variable for the loop, which tells from where the loop is starting.
Condition: The condition is a relational expression and the value of the variable is evaluated using some condition. E.g: i<=5;
Increment/decrement: After the execution of the statements which are in the body of the loop the control will again transfer to the loop and then the value of the variable is incremented. This value can be incremented by one or more according to our need.
if we want to increment variable with one then we can use i++;
e.g:
for(a=1;a<5;i=i+1)
{
System.out.println(“java”);
}
It will print ‘java’ four times.
Program:
//3.PROGRAM TO PRINT EVEN AND ODD NUMBERS
class evenOdd
{
public static void main(String args[ ])
{
int i;
System.out.println(“Even and Odd numbers upto 10 are:”);
System.out.println(“Odd numbers are:”);
for(i=1;i<=10;i=i+2) //PRINTING ODD NUMBERS
{
System.out.println(+i);
}
System.out.println(“Even numbers are:”);
for(i=2;i<=10;i=i+2) //PRINTING EVEN NUMBERS
{
System.out.println(+i);
}
}
}