Author: admin

  • Java – break statement with Example

    It is one of the jump statement in java.

    This statement is used within the loop or switch cases to terminate that statement or the current loop is immediately stopped and resumes at the next statement followed by a terminated loop. we can also say that it helps to break out from the middle of the loop.

    Flowchart for java break statement:

    Syntax of break:

     break; 

    Example: Java Program to demonstrate break statement:

     public class BreakTest 
     {  
        public static void main(String[] args) 
        {  
        //using for loop  
        for(int i=1; i<=15; i++)
        {  
            if(i==10)
            {  
              break;  //breaking the loop 
            }  
            System.out.println(i);  
        }  
        }  
     }

    Output

     1
     2
     3
     4
     5
     6
     7
     8
     9

  • Java – Loops with Example

    A loop statement allows us to execute a block of code or statement or group of statements as many times according to the user’s need. It is done by evaluating a given condition for true and false. The statement stops looping when the condition is false.
    There are two types of loops :

    1. Entry control loop: In this loop, the conditional is checked then the program is executed. There are two types of entry control loop.

    • for loop. 
    • while loop. 

    2. Entry control loop: In this loop, the program is executed first and then the condition is checked. There is only one type of loop in the exit control loop.

    • do-while loop.

    1. while loop: 

    The while loop is one of the fundamental loop statement in Java. It is an entry-control loop, that is it checks the condition at the beginning of the block. And repeats a statement or block until the condition is true. And the increment of the variable checked in condition checking is done inside the block as shown in the below example.

    Syntax of while loop:

     while(condition)
      {  
        //block of code to be executed  
      } 

    Example: Click here


    2. do…while loop: 

    Sometimes it is necessary that the block or statement must be executed at least once but if the condition is initially at a false state then the block will not be executed. So for the situation where a block of code must be executed at least once a do-while loop comes in play.

    do-while is an exit-control loop that is it checks the condition after the first execution of the block. And it keeps executing the block until the condition state is false.

    Syntax of do..while loop:

    do
     {
      statements..
     }while (condition);

    Example: Click here


    3. for loop: 

    The Java for loop allows the user to iterate a part of the program multiple times. If a user is certain about how many specific numbers of times the loop must be executed then for loop is recommended. It is also an entry-control loop but here flow control contains three steps:

    • initialization = The first step is the initialization of the variable and is executed only once. And need to end with a semicolon(;).
    • condition = Second is condition check, it checks for a boolean expression. If true then enter the block and if false exit the loop. And need to end with a semicolon(;).
    • Increment or Decrement = The third one is increment or decrement of the variable for the next iteration. Here, we need to use the semicolon(;).

    Syntax of for loop:

     for(initialization; condition; Increment or Decrement) 
     {
      // Statements
     } 

    Example: Click here


    4. Enhanced For loop:

    Java 5 added another version of for loop. It is useful to iterate through the elements of a collection or array.

    Syntax of enhanced for loop:

    for(declaration : expression) 
     {
      // Statements
     }

    Declaration – This is declared variable and must be compatible with the elements of collection or array that is executing. And is available only for that block.
    Expression – This is the name of an array you need to loop through.

    Example of enhanced for loop in Java:

    public class ForEachTest 
     {
      public static void main(String args[]) 
      {
        int  num[] = {5, 10, 15, 20, 25, 30};
    
        for(int a : num ) 
        {
            System.out.println( a );
            System.out.println("\n");
        }
      }
     } 

    Output:

     5
     10
     15
     20
     25
     30

    Another example enhanced loop in Java with String:

     public class ForEachTest 
     {
      public static void main(String args[]) 
      {
        String  name[] = {"John", "Sam", "Arya", "Brandon"};
    
        for( String singer : name ) 
        {
            System.out.print( singer );
            System.out.print("\n");
        }
            
      }
     } 

    Output:

     John
     Sam 
     Arya
     Brandon

  • Java – for loop with Syntax, Flowchart and Example.

    The Java for loop allows the user to iterate a part of the program multiple times. If a user is certain about how many specific numbers of times the loop must be executed then for loop is recommended. It is also an entry-control loop but here flow control contains three steps:

    • initialization = The first step is the initialization of the variable and is executed only once. And need to end with a semicolon(;).
    • condition = Second is condition check, it checks for a boolean expression. If true then enter the block and if false exit the loop. And need to end with a semicolon(;).
    • Increment or Decrement = The third one is the increment or decrement of the variable for the next iteration. Here, we need to use the semicolon(;).

    Flowchart of for loop in Java:

    for loop java

    Syntax of for loop:

    for(initialization; condition; Increment or Decrement) 
     {
      // Statements
     } 

    Example of for loop in Java:

     public class ForTest 
     {
      public static void main(String args[]) 
      {
        for(int a = 1; a < 10; a++) 
        {
        System.out.println("value of a : " + a );
        }
      }
     }

    Output:

     value of a : 1
     value of a : 2
     value of a : 3
     value of a : 4
     value of a : 5
     value of a : 6
     value of a : 7
     value of a : 8
     value of a : 9

  • Java – While Loop with Example

    The while loop is one of the fundamental loop statements in Java. It is an entry-control loop that is it checks the condition at the beginning of the block. And repeats a statement or block until the condition is true. And the increment of the variable checked in condition checking is done inside the block as shown in the below example.

    Flowchart for While loop in Java:

    While Loop in Java

    Syntax of while loop:

     while(condition)
      {  
        //block of code to be executed  
      } 

    Example of while loop in Java:

    public class WhileTest
    {
      public static void main(String args[]) 
      {
        int a = 1;
    
        while( a < 10 ) 
        {
            System.out.println("value of a : " + a );
            a++; //increment
            
        }
      }
    }

    Output of while Loop:

     value of a : 1
     value of a : 2
     value of a : 3
     value of a : 4
     value of a : 5
     value of a : 6
     value of a : 7
     value of a : 8
     value of a : 9

  • Java – do while Loop with Example

    Sometimes it is necessary that the block or statement must be executed at least once but if the condition is initially at a false state then the block will not be executed.
    So for the situation where a block of code must be executed at least once, a do-while loop comes in play.

    dowhile is an exit-control loop that is it checks the condition after the first execution of the block. And it keeps executing the block until the condition state is false.

    Flowchart for do-While loop in Java:

    Syntax of do..while loop:

    do
     {
      statements..
     }while (condition);

    Example of a do-while loop in java:

     public class dowhileloopTest
     { 
       public static void main(String args[]) 
       { 
        int a = 1; 
        do
        { 
           //will be executed once
           System.out.println("Value of x:" + a); 
           a++; 
        } 
        while (a < 5); 
      } 
     }

    Output:

     Value of x:1
     Value of x:2
     Value of x:3
     Value of x:4

  • Java – Switch Case Statement with Example

    A switch statement allows a variable to be tested for equality against multiple values. It provides the case for the different blocks of code to be executed.

    Switch expression and case value must be of the same type. There must be at least one case or multiple cases with unique case values. In the end, it can have a default case which is optional that is executed if no cases are matched.

    It can also have an optional break that is useful to exit the switch otherwise it continues to the next case.

    Flowchart for switch statement in java:

    switch statement

    Syntax of switch statements:

     {
        case value1:    
        //code to be executed;    
        break;  //optional  
        case value2:    
        //code to be executed;    
        break;  //optional  
        .
        .
        .
        .    
        case valueN:    
        //code to be executed;    
        break;  //optional 
            
        default:     
        code to be executed if all cases are not matched;    
      }

    Example of a switch-case statement in Java:

      public class SwitchTest 
      {  
        public static void main(String[] args) 
        {  
          int num = 5;  
          //Switch expression  
          switch(num)
          {  
            //Case statements  
            case 3: System.out.println("Number 3");  
            break;  
            case 5: System.out.println("Number 5");  
            break;  
            case 8: System.out.println("Number 8");  
            break;  
            //Default case
            default: System.out.println("Not matched");  
          }  
        }  
      }

    Output switch-case statement:

     Number 5

    For practice you can look at this article: Currency Conversion Program in Java


  • Java – if-else if ladder with Example

    This statement allows the user to have multiple options to check for different conditions. Here, if one of the if or else-if condition is true then that part of the code will be executed and rest will be bypassed.

    If none of the conditions is true then the final else statement at the end will be executed.

    Flowchart diagram for if-else if statement in java:

    if elseif statement

    Syntax of if-else-if ladder statement:

    if(condition1)
    {  
       //code to be executed if condition1 is true  
    }
    else if(condition2)
    {  
      //code to be executed if condition2 is true  
    }  
    else if(condition3)
    {  
      //code to be executed if condition3 is true  
    }  
     ...  
    else
    {  
       //final esle if all the above condition are false 
    }

    Example of nested-if statement in Java:

     public class IfElseIfTest 
     {
       public static void main(String args[]) 
       {
         int num1 = 10, num2 = 20;
    
         if (num1 > num2) {
             System.out.println("num1 is greater");
         }
         else if(num2 > num1){
            System.out.println("num2 is greater");
         }       
         else {
            System.out.println("Both are same");
         }
       }
     }

    Output:

    num2 is greater

  • Java – nested-if statement with Example

    Nested-if statement allows the user to use if block inside the other if block. And the inner if is executed only if the outer if is true in condition.

    Flowchart for nested-if statement in Java:

    nested-if statement

    Syntax of nested if statement:

    if(condition1)
     {    
        //block of code to be executed    
        if(condition2)
        {  
          //block of code to be executed    
        }    
     } 

    Example of a nested-if statement in Java:

     public class NestedIfTest 
     {
        public static void main(String args[]) 
        {
        int num1 = 20;
        int num2 = 30;
    
        if( num1 >= 10 ) 
        {
            if( num2 >= 15 ) 
            {
                System.out.println("a and b both are true");
            }
        System.out.println("code inside the first if statement");
        }
        }
     }

    Output:

     a and b both are true
     code inside the first if statement

  • Java – if-else statement with Example

    There are two-part in an if-else statement. First is the if statement, here it checks for the condition to be true and if it is true then it will execute the code present inside the if-block. But if the condition is false then the code inside the else statement will be executed.

    Flowchart for if-else statement in Java:

    if-else statement

    Syntax of an if-else statement:

     if (condition) 
     {
      //code executed if condition true 
     }
     else
     {
      //code executed if condition false 
     }

    Example of an if-else statement in Java:

    public class IfElseTest 
    { 
      public static void main(String args[]) 
      { 
        int a = 5; 
    
        if (a < 10) 
        {
            System.out.println("If statement executed"); 
        }
        else
        {
            System.out.println("Else statement executed"); 
        }
      } 
    }

    Output:

    If statement executed

  • Java – if statement

    Among the decision making statements, if- statement is the most simple one.

    It checks if the condition is true or not, and if it is true then it executes the block of code inside if otherwise, it will not. The block that is executed is placed within the curly braces({}).

    Flowchart for if statement in java:

    Syntax of if statement in Java:

     if (boolean expression)
     {
      /* if expression is true */
      statements... ; /* Execute statements */
      }

    Example of if statement in Java:

    public class IfTest 
    {
      public static void main(String args[]) 
      {
        int a = 5;
    
        if( a < 10)  //checks for condition
        {
           System.out.println("If statement executed");
        }
      }
    }

    Output:

    If statement executed