Java Looping Basic Java part two.

Java has three main standard looping process. Which are,

  • While.
  • Do-while.
  • For.

Why needs loops.

While programming, sometimes, there occurs a situation when we need to execute a block of code several numbers of times.

When some condition is true inside of this lops do everything the inside the loop back.

Loop is bounded by pair of curly braces.

What every you want to repeat you must do inside that curly brace. And loop is a condition test that expression result of Boolean value in other word return.

the true of false.

Elements of java loop.

There have four elements of the loop as follows,

  • Initialization Expression(s)
  • Test Expression (Condition)
  • Update Expression(s)
  • Body of the loop.
Initialization Expression(s).

Before entering a loop, we must initialize its control variable. The initialization of the control variable takes place under initialization expression.

Test Expression.

The test expression is an expression and truth (Boolean) value decides whether the loop body will be executed or not.

Update Expression.

The update expression(s) changes the values of the loop variables.

Body of the loop.

The statements which execute repeatedly.

We can do the simple Boolean test by using the value of variables and comparison operators.

< Less than.

>Greater Than   

== equality.

The single equal sign is a assignment operator and not the comparison operator.

public class MyfirstApplication
{
public static void main(String args[])
{
int i,sum;
for(i = 1 , sum = 0; i <= 10; ++i)
{
System.out.println("The value of i is: "+i) ;
sum = sum + i;
}
System.out.println("The sum of first 10 numbers is: " +sum) ;
}
}

Special point.

  • Statement end in semicolon.
  • Code blocks have two pair of curly braces. {}
  • Declare the variables inside the code block.
  • The assignment operators are one equal sign. (=)
  • The two equal sign for equal operators. (==)
  • If the loop condition test is false, the while code block is not run.
  • Put the Boolean test inside the parentheses. Like this. While (x == 4) {}

Declaration of variables inside loops.

When we declare any variable inside for loop, we cannot access the variable after the loop statement is over.

Java Looping Basic Java part two

Java basic tutorial Basic part one..

Leave a Reply

Your email address will not be published. Required fields are marked *