Sunday, January 12, 2014

Java Control Structures Programming Examples


//Note:  Analysis the programs errors,outputs and Understand the Java if,for,while,do while,continue,break Statements


class Demo1{
public static void main(String arg[]){
int a=0;
int b=5;
int c=6;
if((a!=0)&&((b/a)>=2))
{
System.out.println("True");
}
else
{
System.out.println("False");
}
}
}

class Demo2{
public static void main(String arg[]){
int a=2;
int b=5;
int c=6;
if((a<b)||(a>c))
{
System.out.println("True");
}
else
{
System.out.println("False");
}
}
}

class Demo3{
public static void main(String arg[]){
int a=1;
int b=5;
int c=6;
if((a!=0)&&((b/a++)>=2))
{
System.out.println("True");
}
else
{
System.out.println("False");
}
System.out.println("a="+a);
}
}

class Demo4{
public static void main(String arg[]){
int a=11;
int b=5;
if(a>b){
System.out.println("Maximum="+a);
}
else{
System.out.println("Maximum="+b);
}
}
}

class Demo5{
public static void main(String arg[]){
int a=5;
int b=7;
int c=17;
if((a>b)&&(a>c))
System.out.println("Maximum="+a);
if((b>a)&&(b>c))
System.out.println("Maximum="+b);
if((c>b)&&(c>a))
System.out.println("Maximum="+c);
}
}

class Demo6{
public static void main(String arg[]){
int m=3;
if(m==1){
System.out.println("January");
}else if(m==2){
System.out.println("Febrauary");
}else if(m==3){
System.out.println("March");
}else{
System.out.println("some other months");
}
}
}

class Demo7{
public static void main(String arg[]){
int m=0;
switch(m){
case 0:
      System.out.println("zero");
        break;
case 1:
        System.out.println("one");
        break;
case 2:
        System.out.println("two");
        break;
case 3:
        System.out.println("three");
        break;
default :
System.out.println("some other values");
        break;
}
}
}

class Demo8{
public static void main(String arg[]){
long m=0;
switch((int)m){
case 0:
case 1:
case 2:
        System.out.println("the value may be zero,one or two");
        break;

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

default :
System.out.println("some other values");
}
}
}

class Demo9{
public static void main(String arg[]){
char m='h';
switch(m){
case 'z':
case 'o':
case 't':
        System.out.println("the value may be zero,one or two");
        break;

case 'h':
        System.out.println("three");
        break;

default :
System.out.println("some other values");
}
}
}

class Demo10{
public static void main(String arg[]){
int i,sum=0;
for(i=1;i<=10;i=i+1){
sum=sum+i;
 }
System.out.println("sum="+sum);
}
}

class Demo11{
public static void main(String arg[]){
int i,sum=0;
for(i=0;i<=10;i=i+2){
sum=sum+i;
 }
System.out.println("even sum="+sum);

}
}

class Demo12{
public static void main(String arg[]){
int i,sum=0;
for(i=1;i<=10;i=i+2){
sum=sum+i;
 }
System.out.println("odd sum="+sum);
}
}

class Demo13{
public static void main(String arg[]){
int i,prod=1;
for(i=1;i<=5;i=i+1){
prod=prod*i;
 }
System.out.println("prod="+prod);

}
}

class Demo14{
public static void main(String arg[]){
int i,s=0;
for(i=1;i<=10;i=i+1){
if(i==7) continue;
s=s+i;
System.out.println(i);
}
System.out.println("sum="+s);
}
}

class Demo15{
public static void main(String arg[]){
int i,s=0;
for(i=1;i<=10;i++){
if(s>=35) break;
s=s+i;
System.out.println(i);
}
System.out.println("sum="+s);
}
}

class Demo16{
public static void main(String arg[]){
int i=1,s=0;
while(i<=10){
s=s+i;
System.out.println(i);
if (i==7) continue;
i++;
}
System.out.println("sum="+s);
}
}

class Demo17{
public static void main(String arg[]){
int i=1,s=0;
while(i<=10){

if (i==7){
i++;
continue;
}
s=s+i;
System.out.println(i);
i++;
}
System.out.println("sum="+s);
}
}

class Demo18{
public static void main(String arg[]){
int i=1,s=0;
do{
System.out.println(i);
if(i==7) {
i++;
continue;
}else
{
s=s+i;
i++;
}
}while(i<=10);
System.out.println("s="+s);
}
}

No comments: