0% found this document useful (0 votes)
4 views25 pages

Unit 1

This document provides an overview of Java, covering its history, data types, operators, and the principles of object-oriented programming. It details the evolution of Java, its key features known as 'Java Buzz Words', and compares object-oriented programming with procedure-oriented programming. Additionally, it explains data types, literals, variables, type conversion, and arrays in Java.

Uploaded by

sivamaheshpvi1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views25 pages

Unit 1

This document provides an overview of Java, covering its history, data types, operators, and the principles of object-oriented programming. It details the evolution of Java, its key features known as 'Java Buzz Words', and compares object-oriented programming with procedure-oriented programming. Additionally, it explains data types, literals, variables, type conversion, and arrays in Java.

Uploaded by

sivamaheshpvi1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

UNIT – 1

HISTORY, DATA TYPES AND OPERATORS (12 Hours)


History & Evolution of Java: Creation of Java – The java Buzz words – An overview of Java Object
Oriented Programming. Data types: A closer Look at Literals – Variables – Type conversion and
casting – Automatic type promotion in Expressions. Arrays: One Dimensional Array – Multi
Dimensional Arrays. Operators: Arithmetic Operators – Bitwise operators – Relational operators –
Boolean Logical operators – Assignment operators – Conditional operators–Operator Precedence—
Control statements.
Q.No.1 CREATION OF JAVA

Java language was developed by James Gosling and Patrick Naughton at


sun microsystems, in 1991.It took 18 months to develop the first working version. This
language was initially called “Oak” but was renamed “Java” in 1995.

JDK 1.0 was released on January 23, 1996. After the first release of Java, there have
been many additional features added to the language. Now Java is being used in
Windows applications, Web applications, enterprise applications, mobile applications,
cards, etc. Each new version adds new features in Java.The versions of java are
jdk1.0,jdk 1.1,jdk 1.2,jdk1.4 etc.

Q.No.2 THE JAVA BUZZ WORDS


Java is the most popular object-oriented programming language. Java has
many advanced features, a list of key features is known as Java Buzz Words. The
java team has listed the following terms as java buzz words.

 Simple
 Secure
 Portable
 Object-oriented
 Robust
 Architecture-neutral (or) Platform Independent
 Multi-threaded
 Interpreted
 High performance
 Distributed
 Dynamic

 Simple
Java programming language is very simple and easy to learn,
understand, and code. Most of the syntaxes in java follow C language
and object-oriented programming concepts are similar to C++. In a java
programming language, many complicated features like pointers,
operator overloading, structures, unions, etc. have been removed. One of
the most useful features is the garbage collector it makes java more
simple.
 Secure
Java is said to be more secure programming language because it does not
have pointers concept, java provides a feature "applet" which can be
embedded into a web application. The applet in java does not allow
access to other parts of the computer, which keeps away from harmful
programs like viruses and unauthorized access.
 Portable
Portability is one of the core features of java which enables the java
programs to run on any computer or operating system. For example, an
applet developed using java runs on a wide variety of CPUs, operating
systems, and browsers connected to the Internet.
 Object-oriented
Java is said to be a pure object-oriented programming language. In java,
everything is an object. It supports all the features of the object-oriented
programming paradigm. The primitive data types java also implemented
as objects using wrapper classes, but still, it allows primitive data types
to archive high-performance.
 Robust
Java is more robust because the java code can be executed on a variety of
environments, java has a strong memory management mechanism
(garbage collector), java is a strictly typed language, it has a strong set of
exception handling mechanism, and many more.
 Architecture-neutral (or) Platform Independent
Java has invented to archive "write once; run anywhere, any time,
forever". The java provides JVM (Java Virtual Machine) to to archive
architectural-neutral or platform-independent. The JVM allows the java
program created using one operating system can be executed on any
other operating system.
 Multi-threaded
Java supports multi-threading programming, which allows us to write
programs that do multiple operations simultaneously.
 Interpreted: Java enables the creation of cross-platform programs by
compiling into an intermediate representation called Java bytecode. The
byte code is interpreted to any machine code so that it runs on the native
machine.
 High performance
Java provides high performance with the help of features like JVM,
interpretation, and its simplicity.
 Distributed
Java programming language supports TCP/IP protocols which enable
the java to support the distributed environment
of the Internet. Java also supports Remote Method Invocation (RMI), this
feature enables a program to invoke methods across a network.
 Dynamic
 Java is said to be dynamic because the java byte code may be dynamically
updated on a running system and it has a dynamic memory allocation
and deallocation (objects and garbage collector).

Q.No.3 OBJECT ORIENTED PROGRAMMING


The term Programming paradigm means the methodology for writing program
codes. In general, two paradigms are used to construct a program. These two ways are:
1. a process-oriented model
2. an object-oriented mode
Many programming languages support both the paradigms like python. However,
Java is exclusively object-oriented.

Features of the Object Oriented Paradigm


 OOP breaks a problem into a number of entities called objects and then builds data
and functions around them.
 It treats data as a critical element in the program development and therefore
restricts the flow of data.
 OOP protects the data from accidental modification from outside functions.
 Objects of the different classes can interact easily through functions.
 The object-oriented paradigm follows a bottom-up approach.

Comparison between Procedure-Oriented and Object-


Oriented Approach
1. In the case of POP, the program is divided into small parts based on the functions.
On the other hand, in OOP, the program is divided into objects, which are instances
of classes.
2. In procedure-oriented programming, functions are the highest priority, and data is
the lowest priority. Whereas in object-oriented programming, the data is a critical
element.
3. The procedure-oriented approach is less secure in comparison to the object-
oriented approach. In OOP, due to abstraction data hiding is possible, which makes
it more secure.
Q.No.4 DATA TYPES
Datatypes identify the size and type of value stored in variables. There are eight primitive
datatypes available in java.
i)Integer type: Floating-Point type
1)byte 5)float
2)short 6)double
3)int
4)long
Characters Boolean
7)char 8)Boolean
Data Size Description
Type
byte 1 byte It stores whole numbers from -128 to 127
It is a smallest integer type
Examples:
byte a,b;
short 2 bytes It Stores whole numbers from -2,147,483,648 to 2,147,483,647
Examples:
short a,b;
int 4 bytes It Stores whole numbers from -2,147,483,648 to 2,147,483,647
Examples:
int a,b;
long 8 bytes It stores whole numbers from -9,223,372,036,854,775,808
to 9,223,372,036,854,775,807
Examples:
long a,b;
float 4 bytes It stores fractional numbers. Sufficient for storing 6 to 7
decimal digits
Examples:
float a,b;
double 8 bytes It stores fractional numbers. Sufficient for storing 15 decimal
digits
Examples:
double a,b=5.9;
boolean 1 bit It stores true or false values
Examples:
boolean a=true;
char 2 bytes It Stores a single character/letter or ASCII values
Examples:
char a,b=’r’;
Q.No 5. LITERALS
. Literals are data used for representing fixed values. It can be assigned directly to a
variable.For example, int a = 1; float b = 2.5; char c = 'F'; Here, 1, 2.5, and 'F' are literals.
1)Integer Constants:
*Any whole number value is an integer.
* An integer constant refers to a sequence of digits without a decimal point.
Example: 0 -33 32767
There are three types of integer constants namely,
a) Decimal integer constant
b) Octal integer constant
c) Hexadecimal integer constant

a)Decimal Integer constant (base 10)


It consists of any combinations of digits taken from 0 through 9 and the first digit must be
other than 0.
Valid Examples: 0 32767 -9999 -23
Invalid Examples:
12,245 - Illegal character (,)
10 20 30 - Illegal character (blank space)
b)Octal Integer Constant (base 8)
It consists of any combinations of digits taken from 0 through 7 and the first digit must be 0.
Valid Examples : 037 0 0435
Invalid Examples :
0786 - Illegal digit 8
123 - Does not begin with zero
01.2 - Illegal character (.)
c) Hexadecimal integer constant (base 16)
It consists of any combinations of digits taken from 0 - 9 and also a- f
(either uppercase or lowercase).
The letters a through f (or A through F) represent the decimal values 10 through 15 respectively.
This constant must begin with either 0x or 0X.
Valid Examples : 0x 0X1 0x7F
Invalid Examples : 0xefg - Illegal character g
123 - Does not begin with 0x
ii) Real or Floating-point constant
A real constant is combination of a whole number followed by a decimal point and
the fractional part.
Example: 0.0083 -0.75 .95 215. 6
Thefloating-point constants can be written in two forms:
a). Fractional or Normal form
b). Exponential or Scientific form
(a)Fractional form
A real constant consists for a series of digits representing the whole part of the number,
followed by a decimal point and the fractional part.
Valid Real constants (Fractional): 0.0 -0.1 +123.456 .2 2.
Invalid Real constant: - 1 - a decimal point is missing
1, 2.3 - illegal character (.)
b)Exponential form
A real constant is combination of a whole number followed by a decimal point and
the fractional part. If the value of a constant is either too small or too large, exponential form of
representation is usually used.
Syntax:

Mantissa e exponent

In exponential form, the real constant is represented in two parts.


Mantissa - The mantissa is either a real number expressed in decimal notation or an
integer.
Exponent - The exponent is an integer with an optional plus or minus sign followed by
a series of digits.
Example: 0.000342 can be represented in exponential form
7500000000 can be represented in exponential form as 7.5e9 or 75E8
iii) Single Character constants:
A single character constant or character constant is a single alphabet, a single digit or a
single special symbol enclosed within single quotes.
Valid Character Constants: ‘A‫ۥ‬
‫ۥ‬Invalid: - ‫ۥ‬123‫ۥ‬Length should be 1
"A" - Enclosed in single quotes
iv)String Constant
String constant consists of a sequence of characters enclosed in double quotes.
Examples:
Valid String Constants: - "W" "100" "24, Kaja Street"

Invalid String Constants: - "W the closing double quotes missing


Raja" the beginning double quotes missing
Backslash Character constants
C supports some special backslash character constants that are used in output functions. It
consists of two characters. These characters combinations are known as escape sequence.
The below tables shows the list of backslash character constants.
Q.No.6 VARIABLES
A variable is a name of the memory location. It is used to store data. Its value can be
changed during the program execution, and it can be reused many times. The variables must be
declared before they are used in the program.
Rules for naming variables
 The first character must be a letter.
 A variable can have alphabets, digits, and underscore.
 A variable name can consist of Capital letters A-Z, lowercase letters a-z, digits 0-9, and
two special characters such as underscore and dollar Sign.
 Commas(,) or blank spaces are not allowed.
 Java keywords cannot be used as variable names.
 Variable names are case-sensitive.
Declaring variables:
All variables must be declared before they are used in the program.
Syntax:
datatype variablename=value;
Valid variable names:
int a;
int _ab;
int a30;
Invalid variable names:
int 2;
int a b;
int long;
Initialization of variables:
Assigning a value to variable is known as variable initialization.
Syntax:
datatype variablename=value;
Examples:

int x=10;
float f=2.4f;
char c=’e’;
Java allows variables to be initialized dynamically using any expression.
Example
Int c=a+b // ‘c’ is dynamically assigned ( ie run time)
The scope and life time of variables:
The scope of a variable refers to the areas or the sections of the program in which
the variable can be accessed, and the lifetime of a variable indicates how long the variable
stays alive in the memory.
A variable’s scope is, it is accessible only within the block in which it is declared. A block
begins with a left curly brace { and ends with a right curly brace }.
In java there are three major scopes,
1) Class Level Scope
2) Method Level Scope
3) Block level scope
Class level scope
Variables declared inside a class have class level scope and can be accessed by all the
functions in that class.
Method level scope (local variables):
Variables declared inside a method have method level scope and can be accessed inside
the method.
Block level scope
A variable declared inside a block(pair of brackets “{” and “}” ) has block level scope
and can be accessed within the block(brackets) only.
Q.No.7 TYPE CONVERSION AND CASTING
The process of converting the value of one data type (int, float, double, etc.) to another data
type automatically is known as type conversion. The compiler does this automatic conversion at
compile time
Example:
int a= 10;
System.out.println("The integer value: " + a);
float b = a; // convert int into float type
System.out.println("The float value: " + b);
Output
The integer value: 10
The float value: 10.0

Type Casting
It is a process in which the programmer manually converts one data type into another data
type. For this, the casting operator parenthesis () is used.
Example:
float num = 10.99;
System.out.println("The float value: " + num);
// convert into int type
int data = (int)num;
System.out.println("The integer value: " + data);
Output:
The float value: 10.99
The integer value: 10
Automatic type promotion in Expressions
While evaluating expressions, the intermediate value may exceed the range of operands and
hence the expression value will be promoted. Some conditions for type promotion are:

1. Java automatically promotes each byte, short, or char operand to int when evaluating an
expression.
2. If one operand is long, float or double the whole expression is promoted to long, float, or
double respectively.
byte a = 40;
byte b = 50;
byte c = 100;
int d = a * b / c;
The result of a * b exceeds the range of byte. To handle this kind of problem, Java automatically
promotes each byte or short operand to int. a * b is performed using integers.
While evaluating expressions, the result is automatically updated to a larger data type of
the operand. But if we store that result in any smaller data type it generates a compile-time
error, due to which we need to typecast the result.
Automatic promotions can cause compile-time errors.
b = b * 2; // Error! Cannot assign an int to a byte!
In this case we should use an explicit cast,

byte b = 50;
b = (byte) (b * 2);
which yields the correct value of 100

Q.no 8 ARRAYS
An array is a type of variable that can store multiple values in a single
variable.(ie)Array is a collection of related data items of same type that share a common
name.
Types of Array in java
There are three types of array.
i. Single Dimensional Array

ii. Two dimensional Array


iii. Multidimensional Array
One-Dimensional Arrays:
Arrays with only one index value is known as 1-D array
One Dimensional array declaration:
Arrays must be declared before using it.There are two ways we can declare an array.
First way:
datatype arrayname[]; OR datatype[] arrayname;
arrayname=new datatype[size];
Second way:
Datatype arraryname[]=new datatype[size];
Example:
int a[]; //declaring array
a = new int[20]; // allocating memory to array
2) int b[]=new b[10];

Initialization of 1-D array in Java:


Assigning values to 1-D array is called as initialization.
datatype arrayname[] = {list of values};
Example:
int[] a = { 1,2,3,4,5,6,7,8,9,10 };

Accessing Java Array Elements using for Loop


Each element in the array is accessed via its index. The index begins with 1 and
ends at total array size. All the elements of array can be accessed using Java for Loop.
// accessing the elements of the specified array
for (int i = 0; i < a.length; i++)
System.out.println("Element at index " + i + " : "+ a[i];
ii) Two dimensional array
An array with two index values is called 2-D array (ie) row and column values. We can access
any element of a 2D array using row numbers and column numbers.
Declaring 2 Dimensional Array
datatype arrayname[][]=new datatype[siz1][size2];
Example
int a[][]=new a[3][3];
Initialization of 2-D array in Java:
Assigning values to 2-D array is called as initialization.
datatype arrayName[][] = {list of values};

Examples:
int[][] a = new int[2][2];

a[1][1] =10; // Initializing Array elements at position [0][0]

a[1][2] =20;

a[2][1] = 15;

a[2][2] = 25;

or
int[][] a = { {10, 20}, {15, 25} };
10 20
15 25

22 44

iii)Multidimensional Array
Arrays with more than two index values is called multidimensional array.
Syntax:
datatype arrayname[][][]=new
datatype[siz1][size2][size3];
size1 indicates nos.of tables
size2 indicates nos. of rows in each table
size3 indicates nos. of columns in each table
Examples:
int[][] a = new int[2][2][2];
Initialization of multidimensional array in Java:
Assigning values to multidimensional array is called as initialization.
datatype arrayName[][][] = {list of values};
int[][][] a = { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } };
12
34

56
78

Q.No.9 OPERATORS:
Operators are used to perform operations on data.
Types
1)Arithmetic operators
2)Relational operators
3)Logical operators
4)Bitwise operators
5)Assignment operators
Arithmetic operator
Arithmetic are used to perform arithmetic/mathematical operations on operands(variables).
Assume variable A holds 10 and variable B holds 20, then −
Operator Description Example

+ Adds two operands. A + B = 30

− Subtracts second operand from the first. A − B = -10

* Multiplies both operands. A * B = 200

/ Divides numerator by de-numerator B/A=2

% Divides numerator by de-numerator and it returns reminder. B%A=0

2) Relational operators
Relational Operators are the operators used to compare the relationship between two
operands. The relational operators and their meanings are shown in below table.
Operator Meaning Example a=10,b=20 Output

< less than a<b 10<20 1

<= less than a<=b 10<=20 1


or equal
to

> greater a>b 10>20 0


than

>= greater a>=b 10>=20 0


than or
equal to

== equal to a==b 10==20 0

!= not equal a!=b 10!=20 1


to

Logical operators
Logical Operators are the operators used to compare the relationship between two or
more relational expressions.
Operator Meaning Description Example Output
a=10,b=20

&& Logical It returns true when both (a>5)&&(b>5) true


AND conditions are true otherwise
it returns false

||(pipe) Logical It returns true when at-least (a>5)||(b<5) true


OR one of the condition is true
otherwise it returns false

! Logical It reverses the state of the !(a<5) true


NOT operand .(ie) if the condition is
true it returns false otherwise
it returns true.

) Assignment operators
Assignment operator is used to assign the result of an expression to a variable.(ie) it assigns
values from the right operands to the left operand. C has a collection of shorthand assignment
operators. The format of assignment operator is,
variablename operator=expression;
Operator Description Example

= Simple assignment operator.


Assigns values from right side
C = A + B will assign the value of A + B to C
operands to left side operand

+= Add and assignment operator.


It adds the right operand to
C += A is equivalent to C = C + A
the left operand and assign
the result to the left operand.

-= Subtract and assignment


operator. It subtracts the right
operand from the left C -= A is equivalent to C = C - A
operand and assigns the
result to the left operand.

*= Multiply and assignment


operator. It multiplies the
C *= A is equivalent to C = C * A
right operand with the left
operand and assigns the
result to the left operand.

/= Divide and assignment


operator. It divides the left
operand with the right C /= A is equivalent to C = C / A
operand and assigns the
result to the left operand.
Increment and decrement operators
Increment operators are used to increase the value of the variable by one and decrement operators
are used to decrease the value of the variable by one.

Operator Description

++ Increment

−− Decrement

Syntax:
Increment operator:
++variblename; (or) variablename++;
Decrement operator: --
--variblename; (or) variablename – -;

Example:
Increment operator : ++ i ; i ++ ;
Decrement operator : – – i ; i – – ;
Conditional operators
Conditional operators return one value if condition is true and returns another value if the
condition is false.
This operator is also called as ternary operator.
Syntax:
Expression1 ? expression2 : Expression3;
Expression1 is evaluated first. If it is true, then the expression exp2 is evaluated and its value
becomes the value of the expression. If expresssion1 is false, expression3 is evaluated and its value
becomes the value of the expression.
Example:
a=10,b=15,c
c=(a>b) ? a : b
Q.No 10 DECISION MAKING STATEMENTS OR CONTROL STATEMENTS OR

SELECTION STATEMENTS

Java supports two selection statements

i)if statement ii) switch statement

Decision making statements are used to execute or skip a block of statements based on the
result of condition.(i.e) If the condition is "true" statement block will be executed, if condition is
"false" then statement block will not be executed. These statements control the flow of program
execution, so they are also known as control statements..
Decision making statements are
1)If statement
2)if… else….statement
3)Nested if statement
4)Switch statement
1)If statement
Syntax
If (condition)
{
statement block;
}
Statement-x

Where,
if -- keyword
Condition---relational expression
The statement block may be a single statement or a group of statements. The condition is checked
first. If the condition is true, the statement block will be executed .If the condition is false, the
statement block will be skipped and the execution will jump to the statement-x.
Example
If(a>b)
{
System.out.println(“a is big”);
}
Flow chart of if statement:
2)if… else….statement
The if…..else statement is an extension of the simple if statement. The
general form is,
Syntax
If (condition)
{
statement block1;
}
else
{
statement block2;
}
Statement-x

The condition is checked first. If the condition is true, the statement block1 is executed. If the
condition is false, the statement block2 is executed
Example
int a,b;
if(a>b)
{
System.out.println(“a is big”);
}
else
{
System.out.println(“b is big”);
}
3)Nested if statement or Nesting of if….. else statement
Syntax
The condition1 is checked first. If the condition1 is true, again condition2
is checked. If the condition2 is true, the statement block1 will be executed. If the
condition2 is false, the statement block2 will be executed. If the condition1 is
false, the statement block3 will be executed.

Example
int a,b,c;
If(a>b)
{
if(a>c)
{
System.out.println(“a is big”);
}
else
{
System.out.println (“c is big”);
}
}
else
{
System.out.println (“b is big”);
}
4. else if ladder statement
Syntax:
If (condition1)
{
statement block1;
}
else if(condition2)
{
statement block2;
}
else if (condition3)
{
statement blocl3;
}
..
..
else if(condition n)
{
Statement blockn
}
else
{
default statement block;
}

The if else ladder statement is used to test set of conditions in


sequence. The conditions are tested from the top, downwards. As soon as a
true condition is found, the statement block associated with it is executed and
the control is transferred to the statement-x. When all the ‘n’ conditions
become false, then the final else containing the default statement block will be
executed.
Example:
int mark;
if (mark>79)
{
System.out.println (“Honours”);
}
else if( mark>59)
{
System.out.println (“First class”);
}
else if( mark>49)
{
System.out.println (“Second class”);
}
else if(mark>39)
{
System.out.println (“third class”);
}
else
{
System.out.println(“fail”);
}

5)Switch statement
Switch statement is used for complex programs when the number of
alternatives increases. The switch statement tests the value of the given
variable against the list of case values and when a match is found, a block of
statements associated with that case is executed.
Syntax:
switch(expression)
{
case label1:
statement block1;
break;
case label2:
statement block2;
break;
case label3:
statement block3;
break;
……….
………
……….
case labeln:
statement blockn;
break;
default:
default block;
break;
}

Switch statement is an extension of if ….. Else statement. It


permits any number of branches. When the switch is executed, the value of
the expression is compared against the labels label1, label2, label 3…. Labeln. If
the value of the expression matches with the case label , then the statement
block associated with the case label is executed. Then the control is transferred
to the next statement.
If the value of the expression does not match with any
labels, the default statement block is executed.
The break statement at the end of each block signals the end of a particular
case and causes an exit from the switch statement.
Example:
int day;
switch(day)
{
case 1:
System.out.println(“Sunday”);
break;
case 2:
System.out.println (“Monday”);
break;
case 3:
System.out.println (“Tuesday”);
break;
case 4:
System.out.println (“Wednesday”);
break;

case 5:
System.out.println (“Thursday”);
break;
case 6:
System.out.println (“Friday”);
break;
case 7:
System.out.println (“Saturday”);
break;
default:
System.out.println (“invalid day);
break;
}
Q.No 12 JUMP STATEMENTS
Continue
The continue statement used to skip the current iteration of a loop, if a specified condition
occurs, and continues with the next iteration in the loop. We can use continue statement inside any
types of loops such as for, while, and do-while loop.
Syntax:
continue;
for (int i = 1; i <= 10; i++)
{
if (i == 4)
{
continue;
}
System.out.println(i);
}
The above example skips the value of 4

Break Statement
break is a statement that is used to immediately terminate the loop without executing the
remaining part of the body of the loop.
Syntax:
break;
for (int i = 0; i < 10; i++)
{
if (i == 4)
{
break;
}
System.out.println(i);
}

Q.No.13 ITERATION STATEMENTS OR LOOPING STTEMENTS

It executes one or more statements repeatedly until given condition is true.

Advantage with looping statement

• Reduce length of Code


• Take less memory space.
• Burden on the developer is reducing.
• Time consuming process to execute the program is reduced.

Types of Loops.

There are three type of Loops available in 'java' programming language.

• while loop
• for loop
• do..while

Difference between conditional and looping statement

Conditional statement executes only once in the program where as looping


statements executes repeatedly several number of times.

1) While loop

It executes a block of statements repeatedly as long as given condition is


true.

Syntax
while(condition)
{
statement block
}

It is an entry-controlled loop. In while loop, a condition is checked first. If


a condition is true then the body of a loop is executed. After the body of a loop
is executed then control again goes back at the beginning, and the condition is
checked again. If it is true, the same process is executed until the condition
becomes false. Once the condition becomes false, the control goes out of the
loop.

Example:
class Example
{
public static void main(String args[])
{
int i;
i=1;
while(i<=5)
{
System.out.println("i value"+i);
i++;
}
}
}
Output
1
2
3
4
5

do..while
It is also called an exit-controlled loop. The do…while statement is same as while loop
statement except the condition is specified at the end of the body

Syntax
do
{
statement block;
} while(condition);

In the do-while loop, the body of a loop is always executed at least once. After the
body is executed, then it checks the condition. If the condition is true, then it will again
execute the body of a loop otherwise control is transferred out of the loop.
Example
class Example
{
public static void main(String args[])
{
int i;
i=1;
do
{
System.out.println("i value"+i);
i++;
} while(i<=5);
}}
Output
1
2
3
4
5
iii) for loop
It executes a block of statements as a specified number of
times.
Syntax
for(initilization;condition;increment/decrement)
{
body of statment
}

Steps:
1.Initialize value to variable
2.The condition is checked .If the condition is true, the statement block is
executed . If the condition is false, it exit from the loop
3.Increment/decrement value
4.Goto step 2.

Example:
class Example
{
public static void main(String args[])
{
int i;
for(i=1;i<=5;i++)
{
System.out.println("i value"+i);
}
}
}
Output
1
2
3
4
5

You might also like