Showing posts with label looping statements. Show all posts
Showing posts with label looping statements. Show all posts

Friday, 30 May 2014

Do-While In Java

Do-While Loop

The do-while loop always executes its body at least once,because its conditional expression is at the bottom of the loop.It repeats the statement or block of code while its controlling expression is true.It can execute the statements at least one  or more times.

Syntax of the while loop:

do
{
   //statements
}while(condition);
Each iteration of the do-while loop first execute the body of the loop and then evaluates the conditional expression.If this expression is true, then loop will repeat otherwise the loop terminates.

Flow Chart Of do-while loop:

Simple program of the while loop:

package org.modi;
public class Test
{
    public static void main(String[] args)
    {
 int i=5;
 do
 {
  System.out.println("Count " + i );
  i--;
 }while(i>0)
   }
}
OUTPUT
Count 5
Count 4
Count 3
Count 2 
Count 1


While in Java

While Loop

The while loop is Java's most fundamental loop statement.It repeats the statement or block of code while its controlling expression is true.In while loop, condition is given before the statement.  It can execute the statements 0 or more times.

Syntax of the while loop:

while(condition)
{
   //statements
}

The condition can be any boolean expression.The body of the loop will be executed as long as the conditional expression is true.
When condition becomes false , control passes to the next line of code immediately following the loop.
If loop have only one statement then , curly braces are not necessary.

Flow Chart of the while loop:

Simple program of the while loop:

public class Test { public static void main(String[] args) { int i=5; while(i>0) { System.out.println("Count " + i ); i--; } } }
OUTPUT
Count 5 Count 4 Count 3 Count 2 Count 1

Some important points about while loop:


1.Since the while loop evaluates its conditional expression at the beginning the loop , the body of the loop        will not execute if condition is false at the beginning.
public class Test
{
 public static void main(String[] args)
 {
  int i=5 , j=10;
  while(i>j)
  {
   System.out.println("While loop body");
  }
  System.out.println("Out of While loop");
 }
}
OUTPUT
Out of While loop
2. The body of the while loop can be empty.This is because a null statement is syntactically valid in java.

public class Test
{
 public static void main(String[] args)
 {
  int i=5 , j=10;
  while(++i<--j)
  System.out.println(i);
 }
}
OUTPUT
8
3.If condition will always true then it will become infinite loop.
while(true) //infinite loop


Friday, 23 May 2014

Java Control Statements

In this post we are going to discuss the control statements.
The control statements are used to control the flow of execution of the program.
Different type of control statements are:
1. Selection statements.
2. Iteration or looping statements.
3. Jump statements.

1.Selection statements:
 Java supports two selection statements if and switch. These  statements allows you to control the flow of your program's execution based upon conditions known only during run time.
For details information on control statements click on below links.

1. If
2. Switch

2. Iteration or Looping statements:
Java supports three Iteration or Looping statements for , while and do while.It executes a block of code or statements till the given condition is true. 
For details information on Iteration or Looping statements click on below links.

1. For loop
2. While loop
3. Do-While loop

3. Jump statements:
Java supports three Jump statements break , continue and return .These statements transfer control to another part of your program.
For details information on Jump statements click on below links.

1. break 
2. continue
3. return