A complete Introduction to All the Loops in Java - NareshIT

A complete Introduction to All the Loops in Java - NareshIT

Details of All the Loops in Java

Loops are essential for the success of any programing language. In the simplest of programs, we make use of the loops. We have the conditional statements in the form of if, If else. If else if ladder, Nested if, ternary operators, Switch statements, and break. We have the for loops in the form of enhanced for loop, nested for loop. And then we have the while loop, Do while loop, and the infinite loop. We will be discussing all that. And finally, we will end up with the conclusion.? Let’s begin the article.? And you can contact us for your Java training. Naresh I Technologies is the number one computer training institute in Hyderabad and among the top five computer training institutes in India. Contact us anytime for your Java training .?

Conditional Statements

This type of loop has a condition, and a block of program loops based on that conditional statement. We have various types of conditional statements, and we below discuss them one by one:

  1. If:

This is the simplest of the conditional loops. Its syntax is as below:

If(condition)

{

//block of statements

}        

  1. If Else

This is another conditional loop, and it runs one block of code for the true value and another block of code for the zero value. Its syntax is as below:

if(condition)

{

// block of statements

}

Else

{

//block of statements

}        

  1. If else if the ladder

This is a ladder of conditions, and it terminates as any of the condition is reached:

If(condition)

{

//block of statements

}

else if(condition)

{

//block of statements

}

….

Else

{

//block of statements

}

Let’s have a Java program on this:

public class Main

{

public static void main(String[] args) {



int a=2;

if(a==2)

{

            System.out.println("Hello");

}

else if(a==4)

{

    System.out.println("World");

}

else

{

    System.out.println("Hi");

}

}

}         

Output:

Hello

  1. Nested If

In this, we nest another if inside an if statement

Syntax:

If(condition)

{

//block of statement

}

else if(condition)

{

   If(condition)

    {

     //block of statement for a nest if block

     }

}

Else

{

// block of statement for the else block

}

Ternary Operator

This is the conditional loop that requires three operators.

Its syntax:

(condition)?(code for true): (code for block);

Let’s have a program on this:

public class Main

{

public static void main(String[] args) {



int a=2;

int num=(a==1)?2:3;

System.out.println(num);

}

}        

Output:

3

  1. Switch

The syntax for the Switch statement is as below:

Switch(variable)

{

         case value1: 

                                      //block of statement

                                   Break;

           case value:

                                     // block of statement

                                    Break;

           case value3:

                                     //block of statement

                                    Break;

             ……

             default:

                                //block of statement

                                  Break;

}        

Let’s have a program on this:

public class Main

{

public static void main(String[] args) {



int a=2;

switch(a)

{

         case 2: 

                                   a=6;

                                   break;

           case 3:

                                a=7;

                                    break;

           case 4:

                                   a=9;

                                    break;

           default:

                                a=12;

                                  break;

}

        System.out.println(a);       

          

}

}        

Output:

6

https://tally.so/r/mKo9OV

Register here for more : https://tally.so/r/mKo9OV ??????????????????????????

  1. break

This terminates the program at the point where we have the break statement.

The syntax of it as below:

{

// block of statements

break;

}

Let’s have a program below for it:

public class Main

{

public static void main(String[] args) {



int a=2;

for(int i =0;i<=10;i++)

{

    System.out.print(i);

    if(i==8)

    {

        break;

    }

}

}

}        

Output

12345678

If we would have not placed the break in the above code, then it would have printed till 10. However, because of the break, it prints till 8.

  1. for Loop

This is an iteration loop. And it’s syntax is as below:

for (initialization; condition; increment)

{

// block of the statement;

}

Let’s have a program for this:

for(int i =0;i<=10;i++)

{

    System.out.println(i);

    if(i==8)

    {

        break;

    }

}        

  1. Enhanced for Loop

???????????????This is for each loop. And its syntax is as below:

?????????????? For (datatype Val: array)

                {

                     //block of statement

                }

              Let’s have a program on this:

public class Main

{

public static void main(String[] args) {

    

    int a[]= {1,3,5,6,7,12};



for(int b: a)

{

    System.out.println(b);

    if(b==8)

    {

        break;

    }

}



          

}

}        

  1. Nested for Loop

In this, we have the for loop inside the for loop, and we can have any level of nesting. There is no bounding on the nesting level.

for(initialization; condition; increment)

{

   For(                            )

      {

          Inner for loop

      }

}

Let’s have a program on this:

public class Main

{

public static void main(String[] args) {

    

    int a[]= {1,3,5,6,7,12};



for(int b: a)

{

    System.out.println(b);

    for(int k=3;k<12;k++)

    {

        System.out.print(k);

    }

   

}



          

}

}        

Here we have nested the for loop inside the enhanced for loop. And its output is as below:

1???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

345678910113????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

345678910115????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

345678910116????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

345678910117????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

3456789101112???????????????????????????????????????????????????????????????????????????????????????????????????????????????????

34567891011????

  1. While Loop

This is another form of looping and this loop iterates till the condition is reached.?

while( condition)

{

//block of statement

}

Let’s have a program on this:

public class Main

{

public static void main(String[] args) {

    

  int a =12;

  while(a<=16)

  {

      System.out.println(a);

      a++;

  }

}

}while( condition)

{

//block of statement

}

Let’s have a program on this:

public class Main

{

public static void main(String[] args) {

    

  int a =12;

  while(a<=16)

  {

      System.out.println(a);

      a++;

  }

}

}        

  1. Do while Loop

???????????????This is similar to the while loop. However, the condition, in this case, comes at last. And this loops for at least one time always. You can think of its application accordingly.

The syntax of the Do while loop is as below:

Do

{

// block of code

Increment/decrement statement

} while(condition);

Lets have a program on this:

public class Main

{

public static void main(String[] args) {

    

  int a =12;

  do

  {

      System.out.println(a);

      a++;

  } while(a<=16);

}

}        

  1. Infinite Loop

This is not thought of. However, it is a loop that never terminates, as the condition set is never reached. It hence has a condition that is never fulfilled.

?Let’s have a program for this:

public class Main

{

public static void main(String[] args) {

    

  int a=3;

  while(a/0==0)

                    {

                        System.out.println(a);

                      }                

}

}        

The a/0 will result in an infinite loop as it will never be equal to 0.?

And that completes our tutorial on looping. You can use the above loop together, and one inside another in the manner you like.? For more details, and complete Java training you can contact us.?

Naresh I Technologies is the number one computer training institute in Hyderabad and among the top five computer training institutes in India. Contact us anytime for your Java training. You can also opt for Java online training, and from any part of the world. And a big package is waiting for you. And all is yours for a nominal fee affordable for all with any range of budget. Let us have a look at what you will get with this Java training package:

  • You need to pay a nominal fee.
  • You can choose any Java certification, as per your skills and interest.
  • You have the option to select from online and classroom training.
  • A chance to study at one of the best Java training institutes in India?
  • We provide Java training in Hyderabad and USA, and no matter in which part of the world you are, you can contact us.
  • Naresh I technologies cater to one of the best Java training in India.
  • And a lot more is waiting for you.

Contact us anytime for your complete Java online training .

FAQ'S

1. What are loops in Java, and why are they important?

Loops are control flow statements that allow you to execute a block of code repeatedly. They are essential for repetitive tasks and iterative algorithms. Java provides three main types of loops: for, while, and do-while.

2. How do you use a for loop in Java?

A for loop is used to iterate a specific number of times. It has three parts: initialization, condition, and increment/decrement. Here's a basic example:

Java

for (int i = 0; i < 10; i++) {

    System.out.println("Iteration: " + i);

}        

3. When should I use a while loop or a do-while loop?

  • while loop: Use a while loop when you don't know the exact number of iterations in advance. The loop continues as long as the condition remains true.
  • do-while loop: Use a do-while loop when you want to execute the loop body at least once, regardless of the initial condition.

Here's an example of a while loop:

Java

int i = 0;

while (i < 5) {

    System.out.println("Iteration: " + i);

    i++;

}        

And here's an example of a do-while loop:

Java

int i = 0;

do {

    System.out.println("Iteration: " + i);

    i++;

} while (i < 5);        

For More Details Visit : Java Online Training

Register For Free Demo on UpComing Batches : https://nareshit.com/new-batches

要查看或添加评论,请登录