Scanner class Methods in Java

Looping Structures-The while loop

 Looping Structures

We have discussed about decisions control statements that had various choices. Still they are not applicable in the programs where we want to execute some instructions repeatedly. We can use decision control statements in the programs that execute each statement once.Lets understand looping with a general example.

Example: Suppose your math teacher told you to write table of 9 for 5 times. That means you have to write the same thing (table of 9) for 5 times again and again. 

From above example, it is clear that a loop means to do some actions repeatedly (again & again) till some condition is met. In the example , 5 times is the condition. You will write table for 5 times only.  After 5 times, you will stop writing.

Example 1:- Program to print Hello World  on screen for 5 times without a loop.

Program to print 5 times on screen without using loop in java

Example 2: Program to print Hello World  on screen for 5 times using a loop. 

Program to print 5 times on screen using loop in java
In the above program,we
  1. We defined a variable named "count" and assigned it value of 1.
  2. The while loop will check for condition.It will only work till count is 5.When count's value become or exceeds 5, while loop will stop executing statements written in its body .That is what we want. We want to print the Hello World! for 5 times and each time the count increments by count++ statement.
  3. Because count is initialized with 1 , so after increment its values will be 2,3,4,5 . Now count is 5 which means it is printing Hello World for 5th time.After the last execution count will become 6 due to count ++ statement and 6 is not less than 5, so condition will become false & while loop will stop working.
The output is same as in the previous example.In the last program without a loop, we were writting same statement 5 times. It was completely waste of time.So, that's how a loop works. It repeatedly executes the same statements and eliminates the excess code.

Types of loops

There are mainly 3 types of loops:
  1. while 
  2. do while
  3. for
Beside these types we also have Enhanced for loop. Lets discuss each loop in detail.

1) The while Loop

The while loop is the basic looping structure in java.It lets you to execute the statements repeatedly as long as condition evaluates to true.It is mostly used when the programmer does not know in advance how many times the loop will be executed.

Syntax:

while(condition)

    statement(s);

In the above syntax, the conditions can be any relational or logical expression which must be enclosed within  parenthesis. The statements in block of while loop gets executed as long as the condition is true when condition becomes false the loop stops and gives output.

Working of while loop

 Flowchart given below shows the working of while loop,
Flowchart of while loop
Working:
  1. The control enters.
  2. The while loop checks for Test Condition, that we put in parenthesis.
  3. If  condition is true, the statements  in the body of loop will get executed.
  4. If condition is false, the control will jump to the next statement after the loop.

Example of while loop

 We already discussed an example of while loop. That was very simple example. Let's understand it with another and logical example.

Example: Program to print Square of first 3 natural numbers on screen for 5 times using while loop. 

Program to print Square of first 3 natural numbers on screen for 5 times using while loop in java

This program calculates the square of first 3 natural numbers 1, 2,3 which is 1,4,9 respectively. Firstly, we define a variable "a" and assign it value of 1.We used only one print statement and one increment statement.

In the above program,we
  1. We defined a variable named "a" and assigned it value of 1.
  2. The while loop has a condition which checks for the value of a is 3 or more than 3.The condition (a<=3) return true until a is less than and equals to 3. When a get greater than 3, loop will stop working.
  3. Te print statement prints the square of a using a*a formula . At first a =1, so it prints 1*1 = 1.
  4. The increment statement a++; increases the value of a by 1 each time loop executes. The value of a =1, after the increment, it will change to 2 and a=2.
  5. The a=2 now and it goes to condition which checks for a<=3 .Conditions becomes true and it again print 4 as square of 2. This process continues until a=3.

 Experiment: Write a program to print even numbers between 1 and 15 using while loop. Hint: Even numbers are divisible by 2 and we can find even number by adding 2 if count =0).

Happy Coding!



Comments