You are on page 1of 5

if_else statement->

------------------ex->wap to find the big number b/w two number


class Demo
{
public static void main(String []args)
{
int a =Integer.parseInt(args[0]);
int b =Integer.parseInt(args[1]);
if(a>b)
{
System.out.println(a+" is big");
}
else
{
System.out.println(b+" is big");
}
}
}

ex->wap to find the big number b/w three number


class Demo
{
public static void main(String []args)
{
int a =Integer.parseInt(args[0]);
int b =Integer.parseInt(args[1]);
int c =Integer.parseInt(args[2]);
if(a>b && a>c)
{
System.out.println(a+" is big");
}
else if(b>c && b>a)
{
System.out.println(b+" is big");
}
else
{
System.out.println(c+" is big");
}
}
}
ex->wap to find the number is even or odd
class Demo
{
public static void main(String []args)
{
int a =Integer.parseInt(args[0]);
if(a%2==0)
{
System.out.println(a+" is even");
}

else
{
System.out.println(a+" is odd");
}
}
}
-------------------------------------------------------------------loop->when we want to execute the statement multiple times till condition is tru
e then
------we keep statement inside the loop
there are three types of loop
1-while loop
2-do while loop
3-for loop
for loop->
--------- syntex->for(inetialization ; condition ; increment)
{
----------------}
ex->wap to print the counting up to 50
class Demo
{
public static void main(String []args)
{
for(int i=1;i<=50;i++)
{
System.out.println(i);
}
}
}
ex->waap to print the counting up to given number
class Demo
{
public static void main(String []args)
{
int a =Integer.parseInt(args[0]);
for(int i=1;i<=a;i++)
{
System.out.println(i);
}
}
}
ex->wap to print the table of any number
class Demo
{
public static void main(String []args)
{
int n =Integer.parseInt(args[0]);
for(int i=1;i<=10;i++)

{
int c=n*i;
System.out.println(n+"*"+i+" = "+c);
}
}
}
--------------------------------------------------------------nested loop->loop inside the loop is called nested loop
ex->wap to print the piramid triangle(Imp)
class Demo
{
public static void main(String[] args)
{
for(int i=0;i<5;i++)
{
for(int j=0;j<5-i;j++)
{
System.out.print(" ");
}
for(int k=0;k<=i;k++)
{
System.out.print(" * ");
}
System.out.println();
}
}
}
------------------------------------------------------------------break->it is a keyword that provide the termination from the current loop
countinue->it is a keyword that provide the skip of the particular statement fro
m the current loop
ex->wap to find the number is prime or not
class Demo{
public static void main(String args[]){
int num = Integer.parseInt(args[0]);
int flag=0;
for(int i=2;i<num;i++){
if(num%i==0)
{
System.out.println(num+" is not a Prime Number")
;
flag = 1;
break;
}
}
if(flag==0)
System.out.println(num+" is a Prime Number");
}
}
----------------------------------------------------------------------------------------Array in java->1-Normally, array is a collection of similar type of elements tha
t have ---------------contiguous memory location.
2-in Java, array is an object the contains elements of similar data type.

3-It is a data structure where we store similar elements.


4-We can store only fixed set of elements in a java array.
5-Array in java is index based, first element of the array is sto
red at 0 index.
-->Disadvantage of Array->1-size is fix
2-it can store only homogeneous(similar) data type
--declaring Array Variables->
<element type>[]<array name>;
or
<element type><array name>[];
ex-> int []x; float y[];
--constructing an Array->
<element type><array name> = new <element type>[<array s
ize>];
ex-> int []x = new int[10];
float y[] = new float[5];
--initializing of Array->
<element type><array name> = {<array list>};
ex-> int []x={1,2,3,4,5};
float y[]={1.2,3.3,4.7};
*Anonymous Array->A array without name is called Anonymous Array
------------------ ex-> new int[]{3,5,2,8,6};
ex->wap to print the value of Array
class Demo
{
public static void main(String args[])
{
int a[]=new int[5];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
char b[]={'A','B','C','D','E'};
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
System.out.println("---------------");
for(int i=0;i<b.length;i++)
{
System.out.println(b[i]);
}
}

You might also like