Professional Documents
Culture Documents
Simple if statement:
An “if” statement consists of a Boolean expression followed by one or more
statements.
Syn:
The syntax of an if statement is:
if(Expression)
{
//Statements will execute
//if the Boolean expression is true
}
if-else Statement:
The if-else statement is an extension of the simple if statement. The general form is
if(condition){
True block
}
else{
Alternate block
}
If the test condition (condition) is true then the true block is immediately following the if
statement is executed; otherwise the alternate block is executed. In either case , either true
block or alternate block will be executed not both. In both the cases the control is transferred
to Statement-x
else - - if Ladder
A common programming construct that is based upon a sequence conditions is the else-if
ladder.
Syn:
if(condition)
statement;
else if(condition)
statement;
else if(condition)
statement;
...
else
statement;
The if statements are executed from the top down. As soon as one of the conditions is true,
the statement associated with that if is executed, and the rest of the ladder is bypassed. If
none of the conditions is true, then the final else statement will be executed. The final else
acts as optional. If there is no final else and all other conditions are false, then no action will
take place.
Example:
import java.util.*;
class biggest{
public static void main(String args[]){
int n1,n2,n3;
Scanner sc = new Scanner(System.in);
System.out.println("Enter three numbers: ");
n1= sc.nextInt();
n2= sc.nextInt();
n3=sc.nextInt();
if(n1 > n2){
if(n1 > n3)
System.out.println(n1 +" is big ");
else
System.out.println(n3 +" is big ");
}
else{
if(n2 > n3)
System.out.println(n2 +" is big ");
else
System.out.println(n3 +" is big ");
}
}
}
switch (expression)
{
case value1:
// statement sequence
break;
case value2:
// statement sequence
break;
...
case valueN:
// statement sequence
break;
default:
// default statement sequence
}
The switch statement works like this: The value of the expression is compared with
each of the “ case “ values. If a match is found, the statements following that case are
executed. If none of the case values are not matched, then the optional default statement is
executed. The break statement is used inside the switch to terminate a statement sequence.
Example:
class ColorDemo{
public static void main(String args[])throws Exception{
char color ;
System.out.print("Enter a charecter : ");
color=(char)System.in.read();
switch (color){
case 'r':
System.out.println ("red"); break;
case 'g':
System.out.println ("green"); break;
case 'b':
System.out.println ("blue"); break;
case 'y':
System.out.println ("yellow"); break;
case 'w':
System.out.println ("white"); break;
default:
System.out.println ("No Color Selected");
}
}
}
The exp1 is evaluated first. If the result is true, exp2 is evaluated and returned as the value of
the conditional expression. Otherwise exp3 is evaluated and its value is returned. When the
conditional operator is used, the code becomes simpler, efficient. However the readability is
poor.
Example:
import java .util.*;
class cond_ex{
public static void main(String args[]){
int x, y,big;
Scanner sc = new Scanner (System.in);
System.out.println("Enter two nos :");
x=sc.nextInt();
y=sc.nextInt();
big=(x>y) ? x: y;
System.out.println("Biggest no : "+big);
}
}
It is an entry control loop, i.e. the condition is checked at the time of entry point of the
loop body. First it evaluates the loop condition, if it is true then the loop body will
executes. After executing the loop body the control again transfers to the condition.
This process will continue until the condition will become false. Once the condition is
false then the control transfers to out of loop.
Example:
import java .util.*;
class series{
public static void main(String args[]){
int n,i=1;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a no :");
n=sc.nextInt();
while(i<=n){
System.out.print(i+" ");
i++;
}
}
}
Like initialization the increment part may also have more than one expression and
separated by comma.
for(n=1, m=20 ; n<=m ; n=n+1, m=m-2)
One or more sections can be omitted from the for statement. However, the semicolons
separating the sections must remain.
for( ; m<=10 ; )
Initialization
Example:
import java.util.*;
class prime {
public static void main(String args[ ]) {
int n,i,c;
String str;
Scanner sc= new Scanner (System.in);
System.out.println("Enter a number ");
n= sc.nextInt();
for(i=1,c=0 ; i<=n ; i++) {
if(n%i==0)
c++;
}
str = (c = = 2) ? "prime no " : "Not a prime no ";
System.out.println (str) ;
}
}
Loop Body do
{
// Loop Body;
F T }while(Cond);
Cond
Exaple:
import java.util.*;
class digit_sum{
public static void main(String args[ ]){
int n,sum=0,r;
Scanner sc = new Scanner(System.in);
System.out.print ("Enter a number : ");
n=sc.nextInt( );
int t=n;
do{
r=t%10;
sum=sum+r;
t=t/10;
} while( t > 0) ;
System.out.println("The digital sum of "+n +" is "+sum);
}
}
Example:
import java.util.*;
class break_ex{
public static void main(String args[ ]){
int n,i,c;
String str;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
n=sc.nextInt();
class classname
{
type instance-variables;
type methodname1(parameter-list) {
// body of method
}
}
Variables defined within a class are called instance variables because each instance
of the class (that is, each object of the class) contains its own copy of these variables.
Example:
class rectangle
{
int length;
int width;
void getdata(int x, int y) { }
int area( ) { }
}
The new operator dynamically allocates memory for an object and returns a reference
to it.
r2.read( );
System.out.println("Area of first recangle : "+r1.area());
System.out.println("Area of second recangle : "+r2.area());
}}
Example:
class rectangle{
int length, width;
public rectangle( ){ //constructor method
length=10;
width=5;
}
int area( ){
return(length*width);
}}
class constructor_demo{
public static void main ( String args[ ] ){
rectangle r1 = new rectangle( );
Default constructor:
When we create a constructor with out any parameters then it is known as default
constructor. We can instantiate the object normally.
Parameterized constructor:
We can define a constructor which takes parameters as the normal method can.
When we are using a parameterized constructor to construct the object we need to
supply the required no , type of parameters at the time of instantiation, the constructor
is implicitly invoked by passing certain no of parameters.
Example:
class rectangle {
int length, width;
public rectangle (int x, int y){ // parameterized constructor
length=x;
width=y;
}
int area( ){
return(length*width);
}}
class constructor_para {
public static void main ( String args[ ] ) {
rectangle r1 = new rectangle(10,5);
rectangle r2= new rectangle(20,10);
System.out.println("Area of first recangle : "+r1.area());
System.out.println("Area of second recangle : "+r2.area());
}}
Overloading of constructors
We can also define more than one constructor in a class so that we can customize the
way object creation. At the time instantiation we can invoke the required constructor.
Example:
class rectangle {
int length, width;
public rectangle( ){ //default constructor
length=0;
width=0;
}
//constructor with two parameters
public rectangle(int x, int y) {
length=x;
width=y;
}
//constructor with one parameter
public rectangle ( int x ){
length=width=x;
}
int area( ) {
return (length*width);
}
}
class constructor_over {
Example:
class rectangle {
int length, width;
public rectangle ( int x, int y) {
length=x;
width=y;
}
public rectangle(int x) {
length=width=x;
}
public rectangle (rectangle t) {
length=t.length;
width=t.width;
}
int area ( ) {
return(length*width);
}
}
class clone {
public static void main ( String args[ ] ) {
rectangle r1 = new rectangle(10,5);
rectangle r2= new rectangle(r1);
System.out.println("Area of first recangle : "+r1.area());
System.out.println("Area of Third recangle : "+r2.area());
}}
classname . method( )
}
static void callme() {
System.out.println("a = " + a);
}}
class stat2 {
public static void main ( String args[ ] ) {
StaticDemo.callme( );
System.out.println ("b = " + StaticDemo.b);
}}
Specification Members
Public Accessible everywhere
Protected Accessible by any class in the same package as its class, and
accessible only by subclasses of its class in other packages.
default (no modifier) Only accessible by classes, including subclasses, in the same
package as its class (package accessibility).
Private Only accessible in its own class and not anywhere else