LOOP IN JAVA
INTRODUCTION
Loops are also known as “Iterative statements” because every loop repeats it's body part until the loop’s condition would be true.
TYPES OF LOOPS IN JAVA
In Java programming language, there are three types of loops available, which are described below.
1. While loop
2. Do while loop
3. For loop
PARTS OF LOOP
A loop can be divide in the following three sub parts.
1. Initialization: This part is responsible to initialize/start the loop’s variable.
Example (int a=0)
2. Test condition: This statement is used to implement the test criteria and responsible to stop the Loop’s execution.
a + +
2. Do while loop
3. For loop
PARTS OF LOOP
A loop can be divide in the following three sub parts.
1. Initialization: This part is responsible to initialize/start the loop’s variable.
Example (int a=0)
2. Test condition: This statement is used to implement the test criteria and responsible to stop the Loop’s execution.
Example while (a<10)
3. Increment or Decrement: These statements are used to change the value of loop’s variable.
INCREMENT EXAMPLES3. Increment or Decrement: These statements are used to change the value of loop’s variable.
a + +
a = a + 1
a = a + 2
DECREMENT EXAMPLES
a - -
DECREMENT EXAMPLES
a - -
a = a - 1
a = a - 2
Increment and decrement statements are also known as following names.
Pre increment (Example: ++a)
Post increment (Example: a++)
PRE AND POST INCREMENT EXAMPLE
class Pre_Post
{
public static void main(String args[])
{
int a=10;
// Exmaple Post-increment operator
System.out.println(a++);
// Example Pre-increment operator
System.out.println(++a);
}
}
Increment and decrement statements are also known as following names.
Pre increment (Example: ++a)
Post increment (Example: a++)
PRE AND POST INCREMENT EXAMPLE
class Pre_Post
{
public static void main(String args[])
{
int a=10;
// Exmaple Post-increment operator
System.out.println(a++);
// Example Pre-increment operator
System.out.println(++a);
}
}
- - - PROGRAM OUTPUT - - -
0 Comments