Loops in Java
Loops are used to perform a task repeatedly. There are 3 types of loops
- while loop
- for loop
- do-while loop
Suppose if you want to print first 5 digits
You need to write 5 print statements
System.out.println(1); System.out.println(2); System.out.println(3); System.out.println(4); System.out.println(5);
Instead of writing repeated code, we use loops
While loop :
A control flow statement which is used to perform the repeated tasks based on a given Boolean condition. In while loop number of iterations is not fixed. Loop will be executed only if the condition is true. It is also called the entry control loop.
Flow Chart Diagram
Disadvantage: It generates an infinite loop if we don't give an incremental part
For Loop:
A control flow statement which is used to perform repeated tasks. But in for loop number of iterations are fixed.
Flow chart Diagram
Parts of for loop (Initialization, Conditional part, Incremental part)
Initialization: In this part, the variable is initialized. It is a starting point of the loop
Condition: In this part condition is checked, it returns a boolean. In the case of the true, the loop body is executed.
Increment/Decrement: Variable is incremented or decremented here for next iteration
Loop termination: When the condition becomes false, then the loop is getting terminated.
For Loop Example
public class ForLoop { public static void main(String[] args) { for(int j=1;j<=10;j++){ System.out.println("Values of ==> " + j); } } }
value of j ==> 1 value of j ==> 2 value of j ==> 3 value of j ==> 4 value of j ==> 5 value of j ==> 6 value of j ==> 7 value of j ==> 8 value of j ==> 9 value of j ==> 10
If you want to print 10 digits in reverse order then below code will work fine.
public class ForLoop{ public static void main(String[] args){ for(int k=1;k<=10;j--){ System.out.println("Values of ==> " + k); } } }
Post Increment
An operator that is used to increment the value of the variable after the expression is executed in which post-increment is used. The value is first used in an expression and then incremented.
public class PostIncrement{ public static void main(String[] args){ int i = 1; int j = i++; //post increment } }
Pre Increment
An operator that is used to increment the value of the variable before the expression is executed in which post-increment is used. The value is first incremented in an expression and then used.
public class PreIncrement{ public static void main(String[] args){ int a = 1; int b = ++a; //pre-increment } }
Post Decrement
An operator that is used to decrement the value of the variable before the expression is executed in which post-increment is used. The value is first decremented in an expression and then used.
public class PostDecrement{ public static void main(String[] args){ int i = 2; int j = i--; //post decrement } }
Pre Decrement
An operator that is used to decrements the value of the variable before the expression is executed in which post decrement is used. The value is first decremented in an expression and then used.
public class PreDecrement{ public static void main(String[] args){ int a = 2; int b = --a; //pre-decrement System.out.println("Value of a ==> "+a); System.out.println("Value of b ==> "+b); } }
do-while loop :
The do-While loop is similar to while loop but the difference is that the body of the do-while loop is executed once and then the condition is verified. A control flow statement that executes a piece of code at least once.
Flow Chart Diagram
If the test expression is true, the body of the loop is executed again and the test expression is evaluated.
The first loop body is executed then the condition is checked, this process continues until the condition is false.
This process goes on until the test expression becomes false.
If the test expression is false, the loop ends.
public class DoWhileLoop{ public static void main(String[] args){ int counter = 5; int factorial = 1; do{ factorial *= counter--; }while (counter > 0); System.out.println("result " +factorial); } }Output:
result ==> 120
Array in Java
Array: Stores similar data type values in an array variable.
- A collection of similar data types
- The lowest index is always “0”
- The highest index is always n-1 (n is the size of the array)
Int array
Integer array stores the integer data type.
Integer array stores the integer data type.
int arr[] = new int[4]; arr[0] = 10; arr[1] = 20; arr[2] = 30; arr[3] = 40; System.out.println(arr[2]); System.out.println(arr[3]);
Output:
value of arr[2] ==> 30 value of arr[3] ==> 40
If you will try to find the arr[4], it will give exception
When you go beyond the limit, an exception will raise “ArrayIndexOutOfBoundsException”
Size of array
The size of the array is determined by using length() method.
System.out.println(arr.length)
Print all the values of the array: use for loop
for(int i=0; i < arr.length; i++){ System.out.println(arr[i]); }
Double array
Double array stores the double data type.
double d[] = new double[3]; d[0] = 3; d[1] = 14.33; d[2] = 44.89;
Character array
Character array stores the character data type.
Character array stores the character data type.
char c[] = new char[3]; c[0] = 'a'; c[1] = '3'; c[2] = '$';
String array
String array stores the string data type.
String s[] = new String[3]; s[0] = "test"; s[1] = "World"; s[2] = "Hello";Disadvantages:
- The array size is fixed. Therefore it is called a static array
- Stores only similar data types
To overcome size problem we use Collections like ArrayList, HashTable
To overcome data type problem we use Object array
Object array(Object is a class) – It is used to store different data types
Object array
Object obj[] = new Object[6]; obj[0] = "Tom"; obj[1] = 25; obj[2] = "M"; obj[3] = 12.33; obj[4] = "1/10/1998"; obj[5] = "London";
for(int i=0; i<obj.length; i++) { System.out.println(obj[i]); }
Output
value of obj[0] ==> Tom value of obj[1] ==> 25 value of obj[2] ==> M value of obj[3] ==> 12.33 value of obj[4] ==> 1/10/1998 value of obj[5] ==> London
2D Array in Java
Collection of data in the form of cells
Represented as a matrix with a number of rows and columns
String x[][] = new String[3][5];
Output:
System.out.println(x.length); Number of row ==> 3 System.out.println(x[0].length); Number of columns ==> 5
//1st row x[0][0] = "A"; x[0][1] = "B"; x[0][2] = "C"; x[0][3] = "D"; x[0][4] = "E";
//2nd row x[1][0] = "A1"; x[1][1] = "B1"; x[1][2] = "C1"; x[1][3] = "D1"; x[1][4] = "E1";
//3rd row x[2][0] = "A2"; x[2][1] = "B2"; x[2][2] = "C2"; x[2][3] = "D2"; x[2][4] = "E2";Output
System.out.println(x[1][2]); value of x[1][2] ==> C1</ System.out.println(x[2][2]); value of x[1][2] ==> C2 System.out.println(x[0][4]); value of x[1][2] ==> EPrint all the values of 2D array
Two for loops are required to get array values
//row = 0, col = 0 to 4
//row = 1, col = 0 to 4
//row = 2, col = 0 to 4
for(int i = 0; i < x.length; i++){ for(int j = 0; j < x[0].length; j++){ System.out.println(x[i][j]); } }
Output:
value of x[0][0] ==> A value of x[0][1] ==> B value of x[0][2] ==> C value of x[0][3] ==> D value of x[0][4] ==> E value of x[1][0] ==> A1 value of x[1][1] ==> B1 value of x[1][2] ==> C1 value of x[1][3] ==> D1 value of x[1][4] ==> E1 value of x[2][0] ==> A2 value of x[2][1] ==> B2 value of x[2][2] ==> C2 value of x[2][3] ==> D2 value of x[2][4] ==> E2
0 comments:
Post a Comment