CONTROL STATEMENTS


What is Control Statements?

1.                        Whenever the computer runs a Java program, it goes straight from the first line of code to the last. Control statements allow you to change the computer's control from automatically reading the next line of code to reading a different one.


2.                        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.

DIFFERENT STATEMENTS ARE :


A.        DECISION MAKING STATEMENTS.
               1. IF-THEN
               2.IF-THEN-ELSE
               3.SWITCH

B.        LOOPING STATEMENTS
               1.WHILE
               2.DO-WHILE
               3.FOR

C.        BRANCHING STATEMENTS 
              1.BREAK
              2.CONTINUES.
              3.RETURNS


***       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)
}


***     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. 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);
}



***    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.

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

***    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);

***    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    ****:
1.for loop is used to execute a block of code continuously to accomplish a particular condition. 
2.For statement consists of tree parts i.e. initialization, condition, and increment/decrement.
     
           a. initialization:  It is an expression that sets the value of the loop control variable. 
                             It executes only once.
         
           b.condition : This must be a boolean expression.  
                             It tests the loop control variable against a target value and hence works as a loop terminator. 
   
           c.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); 
}


No comments:

Post a Comment