Scanner class Methods in Java

Java-The for Loop


We discussed about do-while loop in the previous post. In this post, we are going to discuss the for loop. 

 The for Loop

The for loop is another form of looping structure which is most versatile,convenient and popular of the three loops. It is  used in those situations where the programmer already knows how many times the block will get executed. It means it have definite condition.It have different syntax from other loops.
Syntax:
for( initialization; condition; increment/decrement)

    statement(s);

Lets understand the syntax,

  1. for is the keyword to define for loop.
  2. In the parenthesis, initialization is used to initialize a variable which controls the working of loop; condition contains any logical or relational condition to control the execution of loop; increment/decrement is used for incrementing or decrementing the loop variable.
  3. The body of loop contains the statement(s) to be executed repeatedly.

Working of for loop

 Flowchart given below shows the working of do-while loop,
Flowchart of for loop

Working:
  1. The control enters.
  2. Unlike while/do-while loop,in for loop initialization, test condition and increment/decrement expressions can be defined simultaneously in the parenthesis.
  3. The initialization expression defines the loop variable. Example: int i=0;
  4. The test condition is checked, if it is true control executes body of loop.We can put condition like,i<=5
  5. As we know , increment and decrement are used to add or subtract 1 from a variable.These expression change the value of loop variable.For example, i++ will increase the value of i by 1.
  6. After the  increment and decrement, control goes back to for loop and again checks for condition. This process goes on until the condition evaluates to false.
  7. If the condition is false, then the loop does not get executed and control moves to next statement of program.

Example of while loop

  Let's understand for loop with an example.

Example: Program to find factorial of number n using do-while loop. Where 0<n<100.

Program to find factorial of number n using do-while loop.

In the example, 

  1. We first declare two integer variables "n" and "fact".This variable will get value from user.If user give 5 then program will find the factorial of number 5.The "fact" is initialized with 1, and it is used to store the value of factorial of 5 or result.
  2. Next three statements are used to get input from user.In Eclipse, it is important to close the scanner object because it continues to show warning of memory leak until it is not closed.
  3. The for loop, where int i=2; is the initialization expression, i<=n; is condition and i++ is the increment expression.
  4. In body of loop, we have only one statement fact*=i; is used to multiply the loop variable i with fact and store value in fact.
  5. In last, the print statement is used to print the result. Note that  the print statement of loop, if you write it in loop , then output will print each time loop executes.

 Experiment: Write a program to find the sum of odd numbers up to 100. (Hint: 3,5,7,9,11,13....)

Happy Coding!


Comments