***Welcome to ashrafedu.blogspot.com ***This website is maintained by ASHRAF***

posts

    Decision-Making statements:

    Decision-making statements decide which statement to execute and when. Decision-making statements evaluate the Boolean expression and control the program flow depending upon the result of the condition provided.

    There are two types of decision-making statements in Java,

    If statement and switch statement.

    1) If Statement:

    The "if" statement is used to evaluate a condition. The control of the program is diverted depending upon the specific condition. The condition of the If statement gives a Boolean value, either true or false.

    In Java, there are four types of if-statements given below.

    1. Simple if statement
    2. if-else statement
    3. if-else-if ladder
    4. Nested if-statement

    1) Simple if statement:

    It is the most basic statement among all control flow statements in Java. It evaluates a Boolean expression and enables the program to enter a block of code if the expression evaluates to true.

    Syntax:

    if(condition) {    

    statement 1; //executes when condition is true   

    }    

    2) if-else statement

    The if-else statement is an extension to the if-statement, which uses another block of code, i.e., else block. The else block is executed if the condition of the if-block is evaluated as false.

    Syntax:

    if(condition) {    

    statement 1; //executes when condition is true   

    }  

    else{  

    statement 2; //executes when condition is false   

    }  

    3) if-else-if ladder:

    The if-else-if statement contains the if-statement followed by multiple else-if statements.

    In other words, we can say that it is the chain of if-else statements that create a decision tree where the program may enter in the block of code where the condition is true. An else statement can also be defined at the end of the chain.

    syntax:

    if(condition 1) {    

    statement 1; //executes when condition 1 is true   

    }  

    else if(condition 2) {  

    statement 2; //executes when condition 2 is true   

    }  

    else {  

    statement 2; //executes when all the conditions are false   

    }  

    4. Nested if-statement

    In nested if-statements, the if statement can contain a if or if-else statement inside another if or else-if statement.

    Syntax:

    if(condition 1) {    

    statement 1; //executes when condition 1 is true   

    if(condition 2) {  

    statement 2; //executes when condition 2 is true   

    }  

    else{  

    statement 2; //executes when condition 2 is false   

    }  

    }  

    Switch Statement:

    Switch statements are similar to if-else-if statements. The switch statement contains multiple blocks of code called cases and a single case is executed based on the variable which is being switched. The switch statement is easier to use instead of if-else-if statements. It also enhances the readability of the program.

    syntax:

    switch (expression){  

        case value1:  

         statement1;  

         break;  

        .  

        .  

        .  

        case valueN:  

         statementN;  

         break;  

        default:  

         default statement;  

    }  

    No comments:

    Post a Comment