3. for loop
Syntax:
for(initialization; expression; increment/decrement)
{
statements;
}
- Initialization is as assignment operation, a variable holds the initial value.
- for loop holds an expression.
- expression gives the result either true or false.
- if the result is true then for loop statements will executes.
- once execution done, next the initial initialized value will be increment / decrement to one.
- Once again here expression will be evaluate and resulted either true or false, if result is true it will continue the execution.
- if the result is false then for loop terminates the execution.
--------------------------------------------------------------------------------
Program : A program to print 1 to 5 values
Program name : ForDemo1.java
Output :
i value is : 1
i value is : 2
i value is : 3
i value is : 4
i value is : 5
class ForDemo1
{
public static void main(String args[])
{
for(int i=1; i<=5; i++)
{
System.out.println("i value is: "+i);
}
}
}
Compile : javac ForDemo1.java
Run : java ForDemo1
Output :
i value is : 1
i value is : 2
i value is : 3
i value is : 4
i value is : 5
--------------------------------------------------------------------------------
Thanks for your time.
- Nireekshan