Loops
There are mainly three types of loops.
1.For loop
2.While loop
3.Do While loop
As in the name the "Loop" means a cycle ,which it stops after a certain boolen condition.
In this post i will describe about the For loop.
Here is a small Example for the for loop.
class Myjava {
public static void main(String args[]){
for(int x=0 ; x<5 ; x++){
System.out.print("kamal-->");
System.out.println(x);
}
}
}
out put of the programme-->
kamal-->0
kamal-->1
kamal-->2
kamal-->3
kamal-->4
Now i will describe the programme for you.
int x --> we called this as initialization statement.The variable x is local to only for loop.
x < 5 --> In a for loop the second statement should be a full fill Boolean condition.
Other wise the programme will give a compile error.
x++ --> we called this as the generally inclement statement.
please check out the out put of the below programme.
class Myjava {
public static void main(String args[]){
for(int x=2 ; x<=10 ; x=x+2){
System.out.print("Even Number-->");
System.out.println(x);
}
}
}
out put of the programme-->
Even Number-->2
Even Number-->4
Even Number-->6
Even Number-->8
Even Number-->10
Above programme output the even numbers up to 10.
Using the for loops we can done more mathematical calculations.
Here is a Example -
class Myjava {
public static void main(String args[]){
int sum=0;
for(int x=0;x<=10;x++){
sum =sum+x;
}
System.out.println(sum);
}
}
out put of the programme --> 55
This programme out put the sum of first 10 ten numbers--> 1+2+3+4+5+6+7+8+9+10 =55
for your more knowledge -->
Java naming standards
An identifier can be started with a alphabet character.
class Myjava{
int 4x = 2 ; --> incorrect
int $x = 2 ; --> correct
int -x = 3 ; --> correct
int x+y = 4 ; --> incorrect
int double = 4 ; --> in correct
int x4y = 3; --> correct
}
Class name have to started with a capital letter.
Method and Variable names have to started with simple letter.

Comments
Post a Comment