Previous Code:-
Decrement operator
List Code:-
Java codes
Next Code:-
while loop
Java Programs:-
List of Java Programs
Decrement operator
List Code:-
Java codes
Next Code:-
while loop
Java Programs:-
List of Java Programs
For loop is another loop in java
Syntax:-
for(initialization; condition; increment)
{
Body of the loop
}
Statement;
Note:-
# If body of loop is only one statement than braces ({}) are not needed.
# Body of the loop may have one or more statement.
# Initialization and increment can be skip but semicolon should be there to separate 3 section.
# Condition section can`t be skip in for loop.
# Variable declared in for loop is visible only in for loop.
Execution:-
In for loop 1st variable is initialized than condition will check if condition is true than body of the loop will executed. After body of the loop is execute, variable is incremented and the condition is again tested if condition is true than again body of loop is executed, this process is repeated until condition is true. When condition is false than statement next to while loop is executed.
For loop can be written in another way by skipping initialization and increment condition.
Syntax:-
Initialization;
for( ; condition; )
{
Body of the loop
Increment;
}
Variable can be declared in for loop
Example:-
for(int i=0; i < num; i++)
Example:-
Program to count n numbers
import java.util.Scanner;
class group{
public static void main(String angr[]){
Scanner num = new Scanner(System.in);
int n, i;
System.out.println("hey enter number");
n=num.nextInt();
for(i=0; i<=n;i++)
{
System.out.println(i);
}
}
}
Program to display even number up to 20
class simple
{
public static void main(String args[])
{
int num;
for(num=0; num<=20; )
{
num = num + 2;
System.out.println(num);
}
}
}
can i ask a favor ...
ReplyDeletei can't understand a certain thing how should i declare the initialization; condition; increment or what will be my basis in declaring those three???? plz help me sir tnx a lot
sir can i ask for a favor... what will be my basis in declaring initialization; condition; increment these 3??? what are the uses of these three? is it necesary to declare those 3??
ReplyDelete