1. This Program contains variables declaration and operators
public class JavaBasics {
public static void main(String[] args)
{
System.out.println("welcome to java");
int a=20,b=30; //This is Single Line Comment
System.out.print("the value of a is "+a+" and b is "+b);
int c=a+b; int d=a*b; float e=a/b; float f=a%b;
// System.out.println(c+" "+d+" "+e+" "+f);
int x=c=a;
System.out.println("c ="+c+" x= "+x);
int y;
x=1;
y=x++;
System.out.println("y ="+y+" x is "+x);
x=1;
y=++x;
System.out.println("y ="+y+" x is "+x);
boolean i =true;
boolean j =false;
System.out.println("or value is "+(i|j)+"and value is "+(i&j));
}
}
output:
welcome to java
the value of a is 20 and b is 30c =20 x= 20
y =1 x is 2
y =2 x is 2
or value is trueand value is false
2.This Program contains the if-else statement,for and do-while loops
public class JavaLoops {
public static void main(String[] args)
{
int a=10,b=20,c=30;
if(a!=20) { System.out.println("a value is "+a); }
if(a>20){ System.out.println("a value is greater than 20 ");
} else { System.out.println("a value is less than 20 "); }
if(a>b && a>c) {
System.out.println("a is the biggest "+a);
} else if (b>a && b>c) {System.out.println("b is the biggest "+b); }
else {System.out.println("c is the biggest "+c); }
for(int i=0;i<5;i=i+2) { System.out.println("i value is "+i); }
int x=7;
while(x<9) {
System.out.println("x value is "+x);
x=x+1 ; }
do {
System.out.println("x value is "+x);
x++; } while(x<11) ;
}
}
output:
i value is 4
x value is 7
x value is 8
x value is 9
x value is 10
No comments:
Post a Comment