Showing posts with label if else sample program in java. Show all posts
Showing posts with label if else sample program in java. Show all posts

Monday, August 2, 2010

Sample Program of while Loop

this program is using while statement to print 5 hello world in your screen .

here is a sample program;

import java.io.*;

public class samplewhile
{
public static void main(String args[])
{
//declaration
int start=1;
while(start<=5)
{
System.out.print("Hello World");
start++;
}
}
}

sample program of switch statement

This program will show to you how switch statement execute by the java compiler.

import java.util.Scanner;

public class SampleSwitch
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
//declaration
int num;

System.out.print("Enter a number:");
num=input.nextInt();

Switch(num)
{
  case 1:
  case 3:
  case 5:
  case 7:
  case 9:
    System.out.print("You enter odd number:");
    break;
case 2:
  case 4:
  case 6:
  case 8:
  case 10:
    System.out.print("You enter even number:");

    break;
default:
  System.out.print("Your number you enter is not found in the case");
}


}
}

understand this program and practice to create your own program using switch statement,,

Thursday, July 8, 2010

IF ELSE sample program

This program will determine the age of person if 18 above  or 18 below. the program will show message " Your qualified to vote" if the age of person is 18 above, else the program will show message " Your not qualified to vote" if the age of person is 18 below.

here is the sample program.

import java.io.*;

public class SampleIf
{
public static void main(String args[])
{
//declaration
int age=18;

if(age>=18)
   {
     System.out.println("Your Qualified to Vote");
    }
else
   {
    System.out.print("Your not Qualified to vote");
   }
}

}