Jump Statements (By using break)
Syntax:
While loop In Java, the break statement has these three uses :-
while (condition) 1. It terminates a statement sequence in
{ a switch statement.
2. It can be used to exit from a loop.
Statement; 3. It can be used as a "civilized" form of goto.
Statement;
If (condition)
Break;
Statement;
Statement;
Statement;
Do-while loop
Do
Statement;
If (condition)
Break;
}while (condition);
Statement;
for loop:-
for(initial value; final value; increment value)
Statement;
Statement;
If (condition)
Break;
Statement;
Statement;
Statement;
Program: 1(sample program using break statement in for loop):-
class break1
{
public static void main(String args[])
int i=0;
for(i=0;i<10;i++)
if(i==5)
break;
[Link](i);
Compile:
F:\jdk\bin>javac [Link]
F:\jdk\bin>java break1
Output:-
0 1 2 3 4
By using continue
The continue statement breaks one iteration (in
Syntax:
the loop), if a specified condition occurs, and
while loop continues with the next iteration in the loop.
while (condition)
Statement;
Statement;
If (condition)
continue;
Statement;
Statement;
Statement;
do-while loop
do
Statement;
If (condition)
Continue;
}while (condition)
Statement;
For loop:-
for (initial value; final value; increment value)
Statement;
Statement;
If (condition)
continue;
Statement;
Statement;
Statement;
Program: 1(sample program using continue statement in for loop):-
class continue1
public static void main(String args[])
{
int i=0;
for(i=0;i<7;i++)
if(i==5)
continue;
[Link](i);
Compile:
F:\jdk\bin>javac [Link]
F:\jdk\bin>java continue1
Output:-
0 1 2 3 4 6
For-each loop:-
The java for-each loop or enhanced for loop is introduced since J2SE 5.0. it
provides an alternative approach to traverse the array or collection in java. it is
mainly used to traverse the array or collection elements.
The advantages of the for-each loop is that it eliminates the possibility of bugs
and make the code more readable. It is known as the for-each loop because it
traverses each element one by one.
The drawback of the enhanced for loop is that it cannot traverse the element
in reverse order. Here , you do not have the option to skip any element
because it does not work on an index basis.
Syntax:-
The syntax of java for-each loop consists of data type with the variable
followed by colon (:),then array or collection.
How do you break a forEach loop?
for(data type variable : array|collection)
There is no way to stop
{ or break a forEach() loop other
than by throwing an exception.
Statement;
}
Flow chart:-
Program: 1 (sample program for for_each loop using string):-
public class feach1
public static void main(String args[])
for(String element : args)
[Link](element);
}
}
Compile:
F:\jdk\bin>javac [Link]
F:\jdk\bin>java feach aaa bbb ccc
Output:-
aaa
bbb
ccc
Program: 2 (program for for_each loop using array):-
public class feach2
public static void main(String args[])
int arg[]={10,20,30};
for(int num : arg)
[Link](num);
}
}
Compile:
F:\jdk\bin>javac [Link]
F:\jdk\bin>java feach2
Output:-
10
20
30
Program: 3 (program for display the countries):-
public class feach3
Enum:
{
An enum is a special "class" that represents a
enum country{India, Japan, China}; group of constants (unchangeable variables,
like final variables).
public static void main(String args[])
To create an enum, use the enum keyword
{ (instead of class or interface), and separate the
constants with a comma. Note that they should
for(country country : [Link]()) be in uppercase letters:
{ Example
enum Level {
[Link]("The country is "+country);
LOW,
}
MEDIUM,
}
HIGH
}
}
Compile:
F:\jdk\bin>javac [Link]
F:\jdk\bin>java feach3
Output:-
The country is India
The country is Japan
The country is China
Program: 4 (program for for_each loop using display the total value):-
class feach4
public static void main(String args[])
int arr[]={12,13,14,44};
int total=0;
for(int i: arr)
total=total+i;
[Link]("Total : "+total);
}
Compile:
F:\jdk\bin>javac [Link]
F:\jdk\bin>java feach4
Output:-
Total : 83
Program: 5 (program for for_each loop using character array):-
class feach5
public static void main(String args[])
char[] vowels={'a', 'e', 'i', 'o', 'u'};
for(char item: vowels)
[Link](item);
Compile:
F:\jdk\bin>javac [Link]
F:\jdk\bin>java feach5
Output:-
a e i o u
Program: 6 (program for integer array and display the Sum value)
class feach6
public static void main(String args[])
int[] numbers={3,4,5,-5,0,12};
int sum=0;
for(int number: numbers)
sum+=number;
[Link]("Sum="+sum);
}
Compile:
F:\jdk\bin>javac [Link]
F:\jdk\bin>java feach6
Output:-
Sum=19
FUNCTIONS
Math Function:-
Function Action
sin(x) Return the sin of the angle x is radius
cos(x) Return the cos of the angle x is radius
tan(x) Return the tan of the angle x is radius
asin(x) Return the angle whose sin is x
acos(x) Return the angle whose cos is x
atan(x) Return the angle whose tan is x
pow (x, y) Return x raised to y ( x y )
exp(x) Return e raised to x (e x )
log(x) Return natural algorithm of x
sqrt(x) Return the square root of x
abs(x) Return absolute value of x
max(x, y) Return maximum value of x and y
min(x, y) Return minimum value of x and y
Program: 1 (Program for Math function for sin of x) :-
import [Link].*;
class fun1
public static void main(String args[])
DataInputStream in=new DataInputStream([Link]);
try
Usage:
{ sin() returns the trignometric sine of an angle in
between 0.0 and pi. If the argument is NaN or
int x=0;
infinity, then the result is NaN. If the argument is
zero, then the result is a zero with the same sign as
double y=0;
the argument. The value returned will be between -1
[Link]("Enter the X value : "); and 1.
The Math. sin() method returns a numeric value
x=[Link]([Link]()); between -1 and 1, which represents the sine of the
angle given in radians.
y=[Link](x);
[Link](y);
}catch(Exception e) Compile:
{ F:\jdk\bin>javac [Link]
[Link]("Error"); F:\jdk\bin>java fun1
} Output:-
} Enter the X value :
} 6
-0.27941549819892586
Program: 2 (Program for Math function for cos of x)
import [Link].*; Useage:-
class fun2 cos(double a) returns the trigonometric cosine of an
angle. If the argument is NaN or an infinity, then the
{ result is NaN. The computed result must be within 1 ulp
of the exact result.
public static void main(String args[])
DataInputStream in=new DataInputStream([Link]);
try
int x=0;
double y=0;
[Link]("Enter the value : ");
x=[Link]([Link]());
y=[Link](x);
[Link](y);
}catch(Exception e) Compile:
{ F:\jdk\bin>javac [Link]
[Link]("Error"); F:\jdk\bin>java fun2
} Output:
} Enter the value :
} 7
0.7539022543433046
Program: 3 (Program for Math function for tan of x):-
import [Link].*; Usage:-
class fun3 tan() returns the trigonometric tangent of an
angle. If the argument is NaN or an infinity, then
{ the result returned is NaN. If the argument is
zero, then the result is a zero with the same sign
public static void main(String args[]) as the argument.
DataInputStream in=new DataInputStream([Link]);
try
int x=0;
double y=0;
[Link]("Enter the value: ");
x=[Link]([Link]());
y=[Link](x);
[Link](y);
}catch(Exception e) Compile:
{ F:\jdk\bin>javac [Link]
[Link]("Error"); F:\jdk\bin>java fun3
} ` Output:-
} Enter the value:
} 1
1.5574077246549023
Program: 4 (Program for Math function for asin of x):-
import [Link].*; Usage:-
class fun4 asin() returns the arc sine of an angle in between -pi/2 and pi/2.
Arc sine is also called as an inverse of a sine. If the argument is
{ NaN or its absolute value is greater than 1, then the result is
NaN. If the argument is zero, then the result is a zero with the
public static void main(String args[]) same sign as the argument.
DataInputStream in=new DataInputStream([Link]);
try
int x;
double y=0;
[Link]("Enter the value: ");
x=[Link]([Link]());
y=[Link](x);
[Link](y);
}catch(Exception e) Compile:
{ F:\jdk\bin>javac [Link]
[Link]("Error");` F:\jdk\bin>java fun4
} Output:-
} Enter the value:
} 1
1.5707963267948966
Program: 5 (Program for Math function for acos of x):-
import [Link].*; Usage:-
acos() returns the arc cosine of an angle in between 0.0 and pi.
class fun5
Arc cosine is also called as inverse of a cosine. If the argument is
NaN or its absolute value is greater than 1, then the result is
{
NaN.
public static void main(String args[])
DataInputStream in=new DataInputStream([Link]);
try
int x;
double y;
[Link]("Enter the value: ");
x=[Link]([Link]());
y=[Link](x);
[Link](y);
}catch(Exception e) Compile:
{ F:\jdk\bin>javac [Link]
[Link]("Error"); F:\jdk\bin>java fun5
} Output:-
} Enter the value:
} -1
3.141592653589793
Program: 6 (Program for Math function for atan of x):-
import [Link].*;
Usage
class fun6
atan() is used to calculate the trigonometric Arc
{ Tangent of an angle. Arc Tangent is also called as
inverse of a tangent. This method returns the values
public static void main(String args[]) between -pi/2 and pi/2.
DataInputStream in=new DataInputStream([Link]);
try
int x=0;
double y=0;
[Link]("Enter the value: ");
x=[Link]([Link]());
y=[Link](x);
[Link](y);
}catch(Exception e) Compile:
{ F:\jdk\bin>javac [Link]
[Link]("Error"); F:\jdk\bin>java fun6
} Output:-
} Enter the value:
} 1
0.7853981633974483
Program: 7 (Program for Math function for power of x and y):-
import [Link].*;
class fun7
public static void main(String args[])
DataInputStream in=new DataInputStream([Link]);
try{
Usage:-
int x, y; pow() is used to calculate a number raise to the power of
some other number.
double z=0;
If the second parameter is positive or negative zero then
[Link]("Enter the value: "); the result will be 1.0.
x=[Link]([Link]()); If the second parameter is 1.0 then the result will be same
as that of the first parameter.
[Link]("enter the value : ");
y=[Link]([Link]()); Compile:
z=[Link](x, y); F:\jdk\bin>javac [Link]
[Link](z); F:\jdk\bin>java fun7
}catch(Exception e) Output:-
{ Enter the value:
[Link]("Error"); 2
} enter the value :
} 2
} 4.0
Program: 8 (Program for Math function for expression of x):-
import [Link].*;
class fun8
public static void main(String args[])
DataInputStream in=new DataInputStream([Link]);
try Usage:-
{ returns Euler's number e raised to the power of a double
value.
int x=0;
If the argument is NaN, the result is NaN.
double y=0; If the argument is positive infinity, then the result is
positive infinity.
[Link]("Enter the value: ");
x=[Link]([Link]());
y=[Link](x);
[Link](y);
}catch(Exception e) Compile:
{ F:\jdk\bin>javac [Link]
[Link]("Error"); F:\jdk\bin>java fun8
} Output:-
} Enter the value:
} 5
148.4131591025766
Program: 9 (Program for Math function for log of x):-
import [Link].*;
Usage:-
log() method returns the natural logarithm (base e) of a
double value as a parameter. There are various cases : If
the argument is NaN or less than zero, then the result is
NaN. If the argument is positive infinity, then the result is
positive infinity.
class fun9
public static void main(String args[])
DataInputStream in=new DataInputStream([Link]);
try
int x=0;
double y=0;
[Link]("Enter the value: ");
x=[Link]([Link]());
y=[Link](x);
[Link](y);
}catch(Exception e) Compile:
{ F:\jdk\bin>javac [Link]
[Link]("Error"); F:\jdk\bin>java fun9
} Output:-
} Enter the value:
} 5
1.6094379124341003
Program: 10 (Program for Math function for square root of x):-
import [Link].*;
class fun10
{
public static void main(String args[])
DataInputStream in=new DataInputStream([Link]);
try
Usage:-
{ returns the square root of a value of type double passed to
it as argument. If the argument is NaN or negative, then the
int x=0; result is NaN. If the argument is positive infinity, then the
result is positive infinity.
double y=0;
[Link]("Enter the value: ");
x=[Link]([Link]());
y=[Link](x);
[Link](y);
}catch(Exception e) Compile:
{ F:\jdk\bin>javac [Link]
[Link]("Error"); F:\jdk\bin>java fun10
} Output:-
} Enter the value:
} 25
5.0
Program: 11 (Program for Math function for absolute value of x):-
import [Link].*;
class fun11
{
public static void main(String args[])
DataInputStream in=new DataInputStream([Link]);
Usage:-
try
returns the absolute value of a given argument.
{
If the argument is not negative, the argument is returned.
int x=0;
If the argument is negative, the negation of the argument is
returned.
double y=0;
[Link]("Enter the value: ");
x=[Link]([Link]());
y=[Link](x);
[Link](y);
}catch(Exception e) Compile:
{ F:\jdk\bin>javac [Link]
[Link]("Error"); F:\jdk\bin>java fun11
} Output:-
} Enter the value:
} 10
10.0
Program: 12 (Program for Math function for maximum value of x and y):-
import [Link].*;
class fun12
public static void main(String args[])
{
DataInputStream in=new DataInputStream([Link]);
try{ Usagae:
max() function is an inbuilt function in Java which
int x,y;
returns maximum of two numbers. The
double z=0; arguments are taken in int, double, float and long.
If a negative and a positive number is passed as
[Link]("Enter the value: "); argument then the positive result is generated.
x=[Link]([Link]());
[Link]("enter the value : "); Compile:
y=[Link]([Link]()); F:\jdk\bin>javac [Link]
z=[Link](x,y); F:\jdk\bin>java fun12
[Link](z); Output:-
}catch(Exception e) Enter the value:
{ 20
[Link]("Error"); enter the value :
} 10
} 20.0
Program: 13 (Program for Math function for minimum of x and y):-
import [Link].*;
class fun13
public static void main(String args[])
{
ataInputStream in=new DataInputStream([Link]);
Usage:-
try{
min() function is an inbuilt function in java which
int x,y; returns minimum of two numbers. ... If a negative
and a positive number is passed as argument then
double z=0; the negative result is generated. And if both
parameters passed are negative then the number
[Link]("Enter the value: "); with the higher magnitude is generated as result
x=[Link]([Link]());
[Link]("enter the value : "); Compile:
y=[Link]([Link]()); F:\jdk\bin>javac [Link]
z=[Link](x, y); F:\jdk\bin>java fun13
[Link](z); Output:-
}catch(Exception e) Enter the value:
{ 10
[Link]("Error"); enter the value :
} 20
} 10.0