Control Statement in Java - Flow Chart - Professional Programming

Control Statement : 

             If we want to control our program based on some condition, their is the place we must know the concept of Control Statement.
           Naturally all the Control Statement executes based on true or false.
           Their are various types of control statements, 

Types :
  • Decision Making Statement
  • Iteration or Looping statements
  • Jump Statement

Decision Making Statement : 

           Decision making statements in Java refer to those statements that allow the programmer to specify decisions or conditional execution flows in their code. The decision making statements in Java are primarily composed are.
            

Types : 
  • if
  • if-else
  • else-if
  • nested if-else
  • switch
    if Statement :

        This Statement executes only when the condition return true, otherwise this block will not executes.
        
    Syntax :
    
        if(condition which returns only true or false) {
            // statement executes only when the condition return true;
        }

    Example :

        int a = 10, b = 20;

        if(a<b) { // 10 < 20 == condition is true
            // if block will execute
            System.out.println("A is lesser then B.");
        }

    Output : 

            A is lesser than B

    Note : If your using the if statement, there is a probability of getting no output when the condition is false. Because if statement executes only when the condition return true. So have clear thought while using if statement. 


     if-else Statement :

        This Statement have two possibilities which is the condition is return true the if block is executed, if the condition is return false then else block is executed. 
        
    Syntax :  
    
        if(condition which returns only true or false) {
            // statement executes only when the condition return true;
        } else {
            // statement executes only when the condition return false;
        }

   Example :

        int a = 100, b = 20;

        if(a<b) { // 100 < 20 == condition is false
            System.out.println("A is lesser then B.");
        } else {
            // else block is executed
            System.out.println("A is greater then B.");
        }

    Output : 

            A is greater than B

    Note : Their are two probability in if-else condition, if block executed when the condition is true, if the condition is false then else block is executed.

Comment your thought....

Comments

Popular posts from this blog

Finding Second largest number in Array - Java - TCS - Technical Interview

Checking Character only in String - Java 2024 - MNC - Technical Interview

Object Class in Java | Article | Solution Maker - Blog