Friday, 9 September 2011

Break and Continue statements in java


public class BreakAndContinue {

  public static void main(String[] args) {
    for(int i = 0; i < 100; i++) {
      if(i == 74) break; // Out of for loop
      if(i % 9 != 0) continue; // Next iteration
      System.out.println(i);
    }
    int i = 0;
    // An "infinite loop":
    while(true) {
      i++;
      int j = i * 27;
      if(j == 1269) break; // Out of loop
      if(i % 10 != 0) continue; // Top of loop
      System.out.println(i);
    }
  }
}

Switch Case Demo in JAVA

class SwitchDemo
{
public static void main(String args[])
{
 int month=Integer.parseInt(args[0]);
switch(month)
{

case 1: System.out.println("January");break;

case 2: System.out.println("February");break;

case 3: System.out.println("March");break;

case 4: System.out.println("April");break;

case 5: System.out.println("May");break;

case 6: System.out.println("June");break;

case 7: System.out.println("July");break;

case 8: System.out.println("August");break;

case 9: System.out.println("September");break;

case 10: System.out.println("October"); break;

case 11: System.out.println("November"); break;

case 12: System.out.println("October"); break;

default: System.out.println("Sorry ! invallied choice");break;

}
}
}

public class VowelsAndConsonants {
  public static void main(String[] args) {
    for(int i = 0; i < 100; i++) {
      char c = (char)(Math.random() * 26 + 'a');
      System.out.print(c + ": ");
      switch(c) {
      case 'a':
      case 'e':
      case 'i':
      case 'o':
      case 'u':
                System.out.println("vowel");
                break;
      case 'y':
      case 'w':
                System.out.println(
                  "Sometimes a vowel");
                break;
      default:
                System.out.println("consonant");
      }
    }
  }
}




do while loop in java

class DoWhile
{
public static void main(String args[])
{

int no=1;

do
{

System.out.println("Number is:"+no);
no++;

} while(no<11);

}
}



While Loop in java

class WhileDemo
{
public static void main(String args[])
{

int no=1;

while(no<11)
{
System.out.println("Number :"+no);
no++;
}
}
}

java If else demo




class IfDemo
{
public static void main(String args[])
{

int no=Integer.parseInt(args[0]);

if(no>0)

System.out.println("Number is a Positive ");


else if(no<0)

System.out.println("Number is a Negative ");

else

System.out.println("Number is a 0");

}
}

java Ternary operator example ?:

class BigNo
{
public static void main (String args[])
{

int a = Integer.parseInt(args[0]);

int b = Integer.parseInt(args[1]);

int c = Integer.parseInt(args[2]);

int x = (a>b)?(a>c?a:c):(b>c?b:c);

System.out.println(+x);
}
}
                                                           click image shown below  to enlarge


java even odd program


 click on the image to enlarge



class Evenodd
{
public static void main(String args[])
{

 int n = Integer.parseInt(args[0]);

if (n%2==0)
System.out.println( " Even Number"+n);

else

System.out.println(" odd Number"+n);

}
}