Advanced Java Programming

Image
                          Advanced Java Programming Advanced Java Programming :-   Introduction to advance java   As most of us already know that if we want to make normal applications it  can be easily built using core Java concepts. But, when it we need to develop web applications, advanced Java fundamentals, like JSP, Servlets, JDBC etc. needed, so to add capabilities and features of the application advance java is essential for developers. Through the motive of this blog is to explain about Advanced Java, I will be giving you a complete insight into the fundamental concepts of Advance Java. Figure - 1.2 If you want to see complete video on this please  have a look the video below.                              Learn with Resh u Advanced Java Programming Course Figure - 1.3 I hope you understood by the slide why Advanced Java is essential. For your better understanding, I have divided this article int

Loops in java With Example Code

 

LOOPS IN JAVA WITH EXAMPLE CODE




In programming languages, loops are used to execute a set of instructions/functions repeatedly when some conditions become true. There are three types of loops in Java.

  • for loop
  • while loop
  • do-while loop

Java For Loop vs While Loop vs Do While Loop



Java For Loop


The Java for loop is used to iterate a part of the program several times. If the number of iteration is fixed, it is recommended to use for loop.

There are three types of for loops in java.

  • Simple For Loop
  • For-each or Enhanced For Loop
  • Labeled For Loop

Example:

01. Simple For Loop

  1. //Java Program to demonstrate the example of for loop  
  2. //which prints table of 1  
  3. public class ForExample {  
  4. public static void main(String[] args) {  
  5.     //Code of Java for loop  
  6.     for (int i=1;i<=10;i++){  
  7.         System.out.println(i);  
  8.     }  
  9. }  

Output:
1
2
3
4
5
6
7
8
9
10

02. For-each or Enhanced For Loop


  1. //Java For-each loop example which prints the  
  2. //elements of the array  
  3. public class ForEachExample {  
  4. public  static void main(String[] args) {  
  5.     //Declaring an array  
  6.     int arr[]={12,23,44,56,78};  
  7.     //Printing array using for-each loop  
  8.     for(int i:arr){  
  9.         System.out.println(i);  
  10.     }  
  11. }  

Output:

12
23
44
56
78


03. Labeled For Loop

  1. //A Java program to demonstrate the use of labeled for loop  
  2. public class LabeledForExample {  
  3. public static void main(String[] args) {  
  4.     //Using Label for outer and for loop  
  5.     aa:  
  6.         for(int i=1;i<=3;i++){  
  7.             bb:  
  8.                 for(int j=1;j<=3;j++){  
  9.                     if(i==2&&j==2){  
  10.                         break aa;  
  11.                     }  
  12.                     System.out.println(i+" "+j);  
  13.                 }  
  14.         }  
  15. }  
  16. }


Output:

1 1
1 2
1 3
2 1


Java While Loop

The Java while loop is used to iterate a part of the program several times. If the number of iteration is not fixed, it is recommended to use while loop.


  1. public class WhileExample {  
  2. public static void main(String[] args) {  
  3.     int i=1;  
  4.     while(i<=10){  
  5.         System.out.println(i);  
  6.     i++;  
  7.     }  
  8. }  
  9. }  

Output:

1
2
3
4
5
6
7
8
9
10


Java do-while Loop

The Java do-while loop is used to iterate a part of the program several times. If the number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use do-while loop.

The Java do-while loop is executed at least once because condition is checked after loop body.


  1. public class DoWhileExample {  
  2. public static void main(String[] args) {  
  3.     int i=1;  
  4.     do{  
  5.         System.out.println(i);  
  6.     i++;  
  7.     }while(i<=10);  
  8. }  


Output:

1
2
3
4
5
6
7
8
9
10




For Complete Explanation in Hindi Please visit My Youtube Channel 





Thanks For Watching!!!

Comments

Popular posts from this blog

Updating version numbers of modules in a multi-module Maven project

Advanced Java Programming