while (expresison)
{
Statements ;
}
Program to print the Fibonacci series using while loop
class FiboTest
{
public static void main(String args[])
{
int a = 0;
int b = 1;
int max = 50;
int fib = 1;
System.out.print("Fibonacci series is: "+a);
while ( fib <= max )
{
System.out.print(" "+fib);
fib = a+b;
a = b;
b = fib;
}
}
}
for (initialization; condition; increment/decrement)
{
statements ;
}
Write a Java program to print even numbers between the given ranges using for loop.
class EvenNumber
{
public static void main(String args[])
{
int n;
System.out.print("Even number are: ");
for( n = 0; n <= 20; ++n)
{
if (n % 2 == 0)
System.out.print(" "+n);
}
}
}
do
{
Statements ;
}
while (condition) ;
class DoWhile
{
public static void main(String args[])
{
int a = 1;
do
{
System.out.println("Value of a : "+a);
++a;
}
while (a<10);
}
}
class BreakDemo
{
public static void main(String args[])
{
int arr[] = {5,10,15,20,25,30};
int n ;
int search = 15;
boolean b = false;
for(n = 0; n < arr.length; n++)
{
if(arr[n] == search)
{
b = true;
break;
}
}
if(b)
System.out.println("Number found " +search+" at index of "+n);
else
System.out.println("Element is not found");
}
}
Program for printing odd and even number is different columns.
class ContinueDemo
{
public static void main(String []args)
{
for (int j = 1; j <= 10; j++)
{
System.out.print(j+" ");
if (j % 2 != 0)
continue;
System.out.println(" ");
}
}
}