Saturday 4 March 2017

Control Structures in Java

By,
Praveen

Generally the flow of any program is in a sequence. But in some situations it becomes necessary to make decisions and change the flow of the program according to the conditions.
Decision making is a particular condition has occurred or not and then direct to execute certain statements accordingly.
When a program breaks the sequential flow and jumps to another part of the code, it is called branching.
Looping is the process of repeatedly executing a block of statements until some conditions for the termination of the loop are satisfied.

1. Simple If Statement:
It is a two way decision statement. Depending on whether of the value of expression is true or false, it transfers the control to particular statements.

If(expression)
{
    True statement;
}

2.If..Else Statement :
If (expression)
{
    True statement;
}
Else
{
    False statement;
}

Example:  
If (run%2==0)
{
    System.out.println(“number” +num+”is even”);
}
Else
{
    System.out.println(“number” +num+”is odd”);
}

3. Nested If… else :
It have many if..else statements in another another if…else

If(exp.)
{
    If (exp2)
    {
        True Stat1;
    }
     Else
    {   
        Flase stst;
    }
}
Else
{
    false stat.
}

4. Ternary operator :
The ? :  Operator is called conditional or ternary operator. It takes three operators.
Syntax :
Condition  ? exp.1 : exp.2 ;

In this statement if condition is true then exp.1 is run otherwise exp.2 executed.
Example :
Class Ternary
{
    Public static void main (String args[])
    {
        Int i=10, j=20,a=0;
                                    a = I < j ? I : j ;
        System.out.println(“The minimum value is” + a);
    }
}

5.The while statement :
It is entry –controlled loop statement. The condition is evaluated and if it is true, the body of the loop is executed. After execution of the body, the condition is again evaluated and if it is true, the body of the loop is executed again. this process continues until the condition becomes   false and the control is transferred out of the loop.

Class Whitest
{
    Public static viod main main(String Args[])
    {
        Int max=20, prev=0, next=1;
        Int sum;
        System.out.println(“the Fibonacci no  is ” +prev);
        While(next<=max)
        {
            System.out.println(next)
            Sum=prev+next;
            Prev=next;
            Next=sum;
        }
    }
}

6.The do-while Statement :
The general form is
Do
{
    body
}
While (test condition);

It is an exit-controlled loop statement .the body of the loop of do statement is compulsory executed for the first time. After executing once, at the end of the loop , the test condition in the while statement is evaluated. iF the condition , is true,the body of the loop is executed again and this process continues until the condition is true.when the condition becomes false the loop execution is terminated and the control goes to the statement that appears after the while statement.

No comments:

Post a Comment