Friday 10, February 2012
Welcome Guest, Register | Login  
      Home    |    Tutorials    |    Free Ebooks    |    Free Scripts    |    Articles    |    Blog     |    Forum    |    About Us    |    Contact Us

Control Structure

Control structure includes the looping control structure and conditional or branching control structure.
In a looping control structure, series of statements are repeated. In a conditional or branching control structure, a portion of the program is executed once depending on what conditions occur in the code.

Loops:
The purpose of loop statements is to repeat Java statements more than times. There are several kinds of loop statements in Java.
for loop:

for loop is used to execute a block of code continuously to accomplish a particular condition. For statement consists of tree parts i.e. initialization, condition, and increment/decrement.
 initialization: It is an expression that sets the value of the loop control variable. It executes only once.
 condition:  This must be a boolean expression. It tests the loop control variable against a target value and hence works as a loop terminator. 
 Increment/decrement: It is an expression that increments or decrements the loop control variable.

Example:
int a;
for (a=0; a<=10; a++)
{
System.out.println("a = " +a); 
}

 

while loop:
in while loop the statement(s) will be executed as long as Expression evaluates to true. If there is only a single statement inside the braces, you may omit the braces.
Example:

class counting{
public static void main (String args[]){
int i = 0;   

while (i < =5){

System.out.println("count");

i = i + 1; 

}
}
}

 

do-while loop:
The do-while statement is like the while statement, except that the associated block always gets executed at least once.

 

Syntax:
do {
    statement (s)
} while (Expression);

 

 

Example:
public class MyClass {

  public static void main(String[] args) {
    int i = 0;
    do {
      System.out.println(i);
      i++;
    } while (i < 10);
  }

}

 

if statement:

The if statement is the simple form of control flow statement. It directs the program to execute a certain section of code if and only if the test evaluates to true. That is the if statement in Java is a test of any boolean expression.

 

Syntax:
if (Expression) {
    statement (s)
}

else {

    statement (s)

}

 

Example:
public class MainClass {

  public static void main(String[] args) {
    int a = 3;
    if (a > 3)
      a++;
    else
      a = 3;
  }

}

Example:

class IfElseexamp {

    public static void main(String[] args) {

 

        int testscore = 66;

        char grade;

 

        if (testscore >= 90) {

            grade = 'A';

        } else if (testscore >= 80) {

            grade = 'B';

        } else if (testscore >= 70) {

            grade = 'C';

        } else if (testscore >= 60) {

            grade = 'D';

        } else {

            grade = 'F';

        }

        System.out.println("Grade = " + grade);

    }

}

 

 

The output will be

D

 

The Switch Statement:

The Switch Statement is an alternative to a series of else if is the switch statement. Unlike if-then and if-then-else, the switch statement allows for any number of possible execution paths. A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types. The body of a switch statement is known as a switch block. Any statement immediately contained by the switch block may be labeled with one or more case or default labels. The switch statement evaluates its expression and executes the appropriate case.

 

Switch Statement Syntax:
switch (expression) {
case value_1 :

     statement (s);
     break;
case value_2 :
     statement (s);
     break;
  .
  .
  .
case value_n :
     statement (s);
     break;
default:
     statement (s);
}

 

Example:

class Switchexamp {

    public static void main(String[] args) {

 

        int month = 5;

        switch (month) {

            case 1:  System.out.println("January");

break;

            case 2:  System.out.println("February");            

break;

            case 3:  System.out.println("March");

break;

            case 4:  System.out.println("April");

break;

            case 5:  System.out.println("May");

break;

            case 6:  System.out.println("June");

break;

            case 7:  System.out.println("July");

break;

            case 8:  System.out.println("August");

break;

            case 9:  System.out.println("September");

break;

            case 10: System.out.println("October");

break;

            case 11: System.out.println("November");

break;

            case 12: System.out.println("December");

break;

            default: System.out.println("Wrong Input");

break;

        }

    }

}

 

The output will be  

May

 

 
     
   
Java Language Tutorial
 
  What is JAVA
What is JAVA
  Java an Object Oriented Language
Java an Object Oriented Language
  Java Keywords
Java Keywords
  Data Types
Data Types
  Java Operators
Java Operators
  Control Structure
Control Structure
  Arrays
Arrays
  Java Classes
Java Classes
  Java Constructors
Java Constructors
  Java Methods
Java Methods
  Java Method Overloading
Java Method Overloading
  Java Method Overriding
Java Method Overriding
  Java Nested Classes
Java Nested Classes
  Inheritance in Java
Inheritance in Java
  Interfaces in Java
Interfaces in Java
  Polymorphism in Java
Polymorphism in Java
  Exception Handling in Java
Exception Handling in Java
  Catching Exceptions
Catching Exceptions
  Embedding information in an exception object
Embedding information in an exception object
  The throws clause
The throws clause
  Checked and unchecked exceptions
Checked and unchecked exceptions
  The finally clause
The finally clause
  Applets in Java
Applets in Java
  Running an Applet
Running an Applet
 
 
 
Web Designing Tutorials
  HTML Tutorial
HTML Tutorial
  DHTML Tutorial
DHTML  Tutorial
  CSS Tutorial
CSS Tutorial
  XHTML Tutorial
XHTML Tutorial
 
Programming Languages Tutorials
  C Language Tutorial
C Language Tutorial
  C++ Tutorial
C++ Tutorial
  Java Language Tutorial
Java Language Tutorial
  Data Structure Theory Tutorial
Data Structure Theory Tutorial
 
Server Side Scripting Tutorials
  PHP Tutorial
PHP Tutorial
  SQL Tutorial
SQL Tutorial
  ASP Tutorial
ASP Tutorial
 
Client Side Scripting Tutorials
  JavaScript Tutorial
JavaScript Tutorial
  VBScript Tutorial
VBScript Tutorial
 
 
 
POPULAR E-BOOKS
 
Download develop your own database software  Ebook develop your own database software
   
Download List Building for Beginners Ebook List Building for Beginners
   
Download Top SEO Secrets  Ebook Top SEO Secrets
   
Download Introduction to Complexity Theory  Ebook Introduction to Complexity Theory
   
Download Marathon List Building Ebook Marathon List Building
   
 
Studiesinn.com © 2012 All Rights Reserved.
Website Designed & Developed by TechXprtz