Skip to main content

Decision Making and Looping

 Introduction


    A computer program is well suited for perform certain operation. Every computer language or computer programe have featuer that instruct the program for repetitive task. The process of repeatedly execution of blocks of statement is known as Looping. The statement of block will execute any number of times, from zero to infinite number.  


Fig. Loop Control structure

    Java supports such a looping feature. In looping, a sequence of program will executed until some condition for the termination of the loop are satisfied. So looping have two segments, Aprogram loop means its body and control statement means its condition.

    The test condition should be carefully stated in order to perform the desired number of loop executions. Java language provides three constructs for looping operation.

  1.     while 
  2.     do
  3.     for


1. While:

        The simplest of all looping structure in java is the While loop. The basic format is as below:

   Initialization;

   while( test condition ){

        body of loop;

    }

    The while loop is entry-controlled loop statement. The test condition is check first, if the condition is true the body of loop will executed. This process will repeated until the condition will get false. For example:

public class WhileStatement {

public static void main(String[] args) {

int number=5;

while(number>=0){

System.out.println("Number is "+number);

number--;

}

}

}


Output:

Number is 5

Number is 4

Number is 3

Number is 2

Number is 1

Number is 0


2. do statement:

    The while loop is check the condition first and if the condition is true then the body of loop statement executed. In some of occasions it might be necessary to execute the body of the loop before test is perform or checking condition, in this setuation we use the do statement. The basic format is below:

Initialization;

do{

     body of loop;

}while(text condition);

On reaching of the do statement, the programe will execute body of loop once and then check the test condition in while. If the condition is true then it will continues to execute the body of loop until while condition get false. For example,
public class DoWhileStatement {
public static void main(String[] args) {
int number=5;
do{
System.out.println("Number is "+number);
number--;
}while(number >= 3);
}
}

Output:
Number is 5
Number is 4
Number is 3

3. For statement:
    The for loop is another entry-controlled loop that provides a more concise loop control structure. The general format is below:
    for( initialization  ;  test conditions  ;  increment/decrement){
        body of loop;
    }
The execution of the for statement is as follow:
  • Initialization is controll variable which is declare first, using assignment statement such as i=0 or count=0. The i and count is called loop-control variable.
  • The value of the control variable is tested using the text condition. The test condition is a relational expression, such as i < 10 that determins that loop will exit when it satisfy this condition until that body of loop will execute.
  • At the time of each iteration of execution we increase/decrease the control variable value using assignment statement such as i=i+1 OR i=i-1.
For example,
public class ForStatement {

public static void main(String[] args) {
for(int i=0; i<5; i=i+1){
System.out.println("Number is "+i);
}
}

}
Output:
Number is 0
Number is 1
Number is 2
Number is 3
Number is 4

Comments

Popular posts from this blog

Introduction of Java

 What is Java? Java is an object-oriented programming language developed by Sun Microsystem in 1995. It is a combination of C and C++ features with some essential and additional concepts. Java is suited for both standalone and web application development. Java can run on any hardware OR software environment which make it platform independent. Java Example:                                                                                           class Simple{     public static void main(String args[]){          System.out.println("Welcome In Java World");     } } In above example the 'Simple' is a Class name, public is access specifier , Main is a method with argument, St...

Java Package

 Introduction      A java package is a group of similar types of classes, interfaces and sub-packages. Package in java can be categorized in two form, built-in package and user-defined package. There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc. Here, we will have the detailed learning of creating and using user-defined packages. Simple example of java package The package keyword is used to create a package in java. //save as Simple.java   package mypack;   public class Simple{    public static void main(String args[]){       System.out.println("Welcome to package");      }   }   How to access package from another package? There are three ways to access the package from outside the package. 1. import package.*; 2. import package.classname; 3. fully qualified name. 1. Using packagename.*      If you use package.* the...

Java Classes, Object and Methods, Constructors

 Defining a class     A class is a user-defined data type or group of object with a template or blueprint that serves to define its properties. It is a logical entity. Once a class type has been define, we can create " Variables " which are similar to basic type declaration. In java, variables are termed as instance of class, which actual object. The basic form class defination is as follows: class classname [extends superclassname]{     [fields declaration]     [method declaration] } The classname and superclassname are valid java identifiers. The keyword extends indicate that superclassname properties extended into classname. It is a pert of inheritance. (We will discuss inheritance in later part.) Everything inside square brackets is optional that means we can use as per the requirements. For example, class Empty{ } In above example, the Empty class is class name. This class does not have any properties and therefor cannot do anything. A java cla...