DEV Community

Cover image for Program Control Statements 3
Kumar Sanskar
Kumar Sanskar

Posted on

Program Control Statements 3

So it's day nine of my learning java journey and today I will discuss the topic of loops in program control statements.

Loops in general understanding means to repeatedly doing the same task over and over again for example writing Sorry hundred times to your professor for not submitting assignment on timeπŸ˜‚πŸ˜‚.
Alt Text

Beware!

There are three general type of loop control structure in case of every programming language. They are -
i. For loop
ii. while loop
iii. do-while loop
but in java in addition to these we also have an enhanced for loop which is another variant of for loop which will be discussed after arrays so it is more understandable as per the book I am following.

for loop -

  • it is pretty easy in understanding and I suppose the syntax and example provoided will be self explanatory for it.
  • syntax -
   for(*intialization* ; *condition* ; *iteratation*)
   {
       statements
   }
Enter fullscreen mode Exit fullscreen mode
  • example -
public class UsingForLoop {
    public static int NUMBER_ITERATIONS = 5;

    /*
     *here we demonstrate the of use the for loop for printing input name five times.
     */
    public static void main(String[] args) {
        System.out.println("Please enter your name: ");
        Scanner scannerObject = new Scanner(System.in);
        String studentName = scannerObject.next();

        for (int NUMBER_ITERATORS = 0; NUMBER_ITERATORS < 5; NUMBER_ITERATORS++) {
            System.out.println("Your name is: " + studentName);

        }
    }
}
Enter fullscreen mode Exit fullscreen mode
  • it is also called as entry controlled loop because the condition is checked at the beginning of the loop and as soon as condition evaluates to 'false' the loop is terminated.

  • Infinite for loop -

   for(;;)
   {
     statements
    }
Enter fullscreen mode Exit fullscreen mode

while loop -

  • it is a type of loop control structure where condition is checked in the beginning and loop executes or runs till the condition holds 'true'.

  • syntax -

     while(condition)
     {
       statements  
      }
Enter fullscreen mode Exit fullscreen mode
  • example -
    public class UsingWhileLoop {
    /*
     *her ewe use while loop to print squares of number from 1 to 100.
     */
    public static void main(String[] args) {
        int i = 1;
        while (i <= 100) {
            System.out.println(i + "^2 =" + i * i);
            i++;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

do while loop -

  • it is also a loop control structure but unlike for and while loop it is exit control in nature that is it will be executed at-least even when the condition is 'false'.
  • syntax -
   do{
    statements
   }while(condition);
Enter fullscreen mode Exit fullscreen mode
  • exapmle -
public class usingDoWhileLoop {
    /*
     *here we use do while loop to add user input numbers till zero is input by user.
     */

    public static void main(String[] args) {
        int sum = 0;
        int number = 0;

        Scanner scannerObject = new Scanner(System.in);


        do {
            System.out.println("input a number: ");
            number = scannerObject.nextInt();
            sum += number;
        } while (number != 0);
        System.out.println("sum= " + sum);
    }
}
Enter fullscreen mode Exit fullscreen mode
  • another type of program control structures apart form the loops are break and continue, they are statements and can even be used to alter the normal execution of program like break is used in switch statements.

That's the end of the program control structures although I left out the example for break and continue I will update it as soon as possible. Till then keep learning, keep growing and feel free to suggest any changes in post.

Top comments (0)