Unit-3 Programming Constructs

Example 1 : IfDemo

import java.*;
class IfDemo
{
public static void main(String args[])
{
int a=10;
if(a>0)
{
System.out.println("A is positive");
}
else
{
System.out.println("A is Negative");
}
}
}
OUTPUT : IfDemo

Example 2 : IfDemo2

import java.*;
class IfDemo2
{
public static void main(String args[])
{
int a,b,c;
a=20;
b=34;
c=25;
if(a>b&&a>c)
System.out.println("A is bigger");
else
if(b>c)
System.out.println("B is bigger");
else
System.out.println("C is bigger");
}
}
OUTPUT : IfDemo2

Example 3 : IfDemo3

import java.io.*;
class IfDemo3
{
public static void main(String args[])
{
int mark=65;
if(mark>=90)
System.out.println("A Grade");
else if(mark>=80)
System.out.println("B Grade");
else if(mark>=70)
System.out.println("C Grade");
else if(mark>=60)
System.out.println("D Grade");
else
System.out.println("F Grade");
}
}
OUTPUT : IfDemo3

The Conditional Operator

Example : ConOprDemo In Java

import java.io.*;
class ConOprDemo
{
public static void main(String args[])
{
int x=20;
int y=10;
int z=0;
z=y<y?x:y;
System.out.println("the value is "+z);
}
}
OUTPUT : ConOprDemo

Switch…Case in JavaProgramming

example : SwitchDemo

import java.io.*;
class SwitchDemo
{
public static void main(String args[])
{
int val1,val2;
char opr;
val1=Integer.parseInt(args[0]);
val2=Integer.parseInt(args[1]);
opr=args[2].charAt(0);
switch(opr)
{
case '+':
System.out.println("Sum="+(val1+val2));
break;
case '-':
System.out.println("Difference="+(val1-val2));
break;
case'*':
System.out.println("Product="+(val1*val2));
break;
case'/':
System.out.println("Quotient="+(val1/val2));
break;
default:
System.out.println("Invalid choice");
}
}
}

Looping…Statements In Java

Example : ForDemo

import java.io.*;
class ForDemo
{
public static void main(String args[])
{
for(int i=1;i<5;i++)
{
System.out.println("\t\t\t"+i);
}
}
}
OUTPUT : ForDemo

While…Loops In Java

Example : WhileDemo

import java.io.*;
class WhileDemo
{
public static void main(String args[])
{
int nos=1;
int x=1;
System.out.println("first five odd nos:\n");
while(nos<=5)
{
System.out.println("\t\t"+x);
x=x+2;
nos=nos+1;
}
}
}
OUTPUT : WhileDemo

Do While…Loops In Java

Example : DoDemo

import java.io.*;
class DoDemo
{
public static void main(String args[])
{
int x=1;
do
{
System.out.println("lopping round"+x);
x++;
}
while(x<=10);
}
}
OUTPUT : DoDemo

Jump Statements Or Unconditional Statements

Break Statement in Java

Example : BreakDemo

import java.io.*;
class BreakDemo
{
	public static void main(String args[])
	{
		int i=0;
		while(i<10)
		{
		if(i==5)
		{
			break;
		}
		else
		{
		System.out.println(i);
		}
		i++;
		}
	}
} 
OUTPUT : BreakDemo

Continue…Statement In Java

Example : ContDemo

import java.io.*;
class ContDemo
{
public static void main(String args[])
{
int i;
for(i=0;i<10;i++)
{
System.out.println(i+" ");
if(i%2==0)
continue;
System.out.println(" ");
}
}
}
OUTPUT : ContDemo

Labeled…Loops In Java

Example : BreakLoopDemo

import java.io.*;
class BreakLoopDemo
{
public static void main(String args[])
{ 
boolean t=true;
first:{
second:{
third:{
System.out.println("before the break:");
if(t)
break second;
System.out.println("this won't execute");
}
System.out.println("this won't execute");
}
System.out.println("this is after second block.");
}
}
}
OUTPUT : BreakLoopDemo

Factorial In Java

Example : Factorial

import java.io.*;
class Factorial
{
public static void main(String args[])
{
int fact=1;
for(int i=1;i<=10;i++)
{
fact*=i;
System.out.println(i+"Factorial is:"+fact);
}
}
}
OUTPUT : Factorial

MultiTable In Java

Example : MultiTable

import java.io.*;
class MultiTable
{
public static void main(String args[])
{
byte i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=1;j++)
System.out.println(i*j+"\t");
System.out.println("\n");
}
}
}
OUTPUT : MultiTable

ReverseNumber In Java

Example : ReverseNum

import java.io.*;
class ReverseNum
{
public static void main(String args[])
{
int num=12345;
int revnum=0;
System.out.println("Number :"+num);
while(num>0)
{
revnum=revnum*10+num%10;
num/=10;
}
System.out.println("Reversed Number :"+revnum);
}
}
OUTPUT : ReverseNum