Java 101-3 (Condition and Loops)🔄🧐

Java 101-3 (Condition and Loops)🔄🧐

Conditional Statement

The computer runs on two-digit which are 0 and 1 which represent false and true respectively. So, whenever it comes to programming language they decide with the help of control statements that direct the flow of the program based on a given condition.

So basically after condition and loop, you will be,

Types of conditional statements🕵️‍♂️📈

  1. If statement - if the given condition in 'if' is true it will execute the command given in the {} of if or the first command line present just after it.

     class IfExample 
     {
         public static void main(String args[])
         {
             int i = 10;
             if (i < 15)
                 System.out.println("Inside If block"); // part of if block(immediate one statement after if condition) and thus will get execute only if's condition is true.
                 System.out.println("10 is less than 15"); //will always executes as it is outside of 'if' block
             System.out.println("I am Not in if"); // this is also outside 'if' block
         }
     }
    
     ---------------- OR --------------------
    
     class IfDemo {
         public static void main(String args[])
         {
             int k = 10;
    
             if (k < 11)
             { // both of the statement will execute only if 'if's condition is true.
                 System.out.println("Inside If block"); 
                 System.out.println("10 is less than 15"); 
             } 
             System.out.println("I am Not in if"); // this will always execute
         }
     }
    
  2. If - Else Statement - In this statement, if the condition mentioned in 'If' is true it will execute the command in the 'If' block but if the condition is false it will switch to 'Else' and will execute the command present there. Also, we don't need to specify the condition in Else, as it by default assumes that if If's condition is false it will execute.

     class IfElseExample {
         public static void main(String args[])
         {
             int g = 12;
             if (i < 15)
             {
                 System.out.println(i + " is less than 15")
             } 
             else
             {
                 System.out.println(i + " is more than 15");
             }
    
         }
     } //As If's condition was true it will execute the command given in it and will not enter else loop
    
     --------------or-------------
     class IfElseExample {
         public static void main(String args[])
         {
             int g = 128;
             if (i < 15)
             {
                 System.out.println(i + " is less than 15")
             } 
             else
             {
                 System.out.println(i + " is more than 15");
             }
    
         }
     } //As If's condition is false it will not execute the command given in it and will thus enter the else loop to execute the command given in it.
    
  3. If-Else-If Statement - What if I want to execute a lot of conditional statements and if any of the following is true it would execute the command in it and after that the rest of the ladder is bypassed? For that purpose, we use this statement.

    In this, we can use any number of Else if statement and if any of the condition in Else if and If is not fulfilled then it will execute the last else statement. Also, we could only have one block of If and else.

     class ifelseifExample {
         public static void main(String args[])
         {
             int k=60;
    
             if (k == 100)
                 System.out.println("k is 100");
             else if (k == 80)
                 System.out.println("k is 80");
             else if (k == 60)
                 System.out.println("k is 60");
             else
                 System.out.println("k is not present");
         }
     }  // so this will execute the command of else if which have k==60 as a condition in it and will skip rest of it
    
  4. Switch Statement - This statement provides an easy way to execute different parts of code based on the value of the expression.

     class switsta
     {
         public static void main(String[] args) 
         {
             int n = 20;
    
             /*if(n==1)
                 System.out.println("Monday");
             else if(n==2)
                 System.out.println("Tuesday");
             else if(n==3)
                 System.out.println("Wednesday");
             else if(n==4)
                 System.out.println("Thursday");
             else if(n==5)
                 System.out.println("Friday");
             else if(n==6)
                 System.out.println("Saturday");
             else if(n==7)
                 System.out.println("Sunday");*/
     //alternate version of above.
             switch(n)
             {
                 case 1:  // 1 represents that if n==1
                     System.out.println("Monday");
                     break; // it is imp to use break as otherwise the moment switch finds a true value it will print every other case.
                 case 2:
                     System.out.println("Tuesday");
                     break;  // it ensures that when we get our value it exits the loop
                 case 3:
                     System.out.println("Wednesday");
                     break;
                 case 4:
                     System.out.println("Thursday");
                     break;
                 case 5:
                     System.out.println("Friday");
                     break;
                 case 6:
                     System.out.println("Saturday");
                     break;
                 case 7:
                     System.out.println("Sunday");
                 default:
                     System.out.print("Enter number from 1 to 7 only");
             }
         }
     }
    
  5. Jump Statement - It consists of 3 kinds of statements which are Break, Continue and Return.

    a) Break - As mentioned earlier, this is used to exist a loop and terminate a sequence in a switch statement.

    b) Continue - It is used when we need the loop to repeat itself if a certain condition is true otherwise exit the loop at a particular iteration and execute the rest of the code.

c) Return - The return statement is used to explicitly return from a method.

class Blog {

    // Method 1
    // Helper method
    // Since return type of RR method is void
    // so this method should not return any value
    void demofun(double i)
    {
        // Demo condition check
        if (i < 9)

            // See here return need not be last
            // statement but must be last statement
            // in a method to execute
            return;

        else
            ++i;
    }

    // Method 2
    // main driver method
    public static void main(String[] args)
    {
        // Calling the method
        new Blog().demofun (7);

        // Display message to illustrate
        // successful execution of program
        System.out.println("Program executed successfully");
    }
}

Loops🤹‍♂️

A loop is a powerful Statement in a programming language that provides us with the power of executing a command repeatedly till the condition is satisfied.

  1. While loop - A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement.

     class WhileExample
     {
         public static void main(String[] args) 
         {
             //loop ==> while,do while,for     
             // while loop
             int i = 1;
             while(i<=3)
             {
                 System.out.println("hi" + i);  // it will give 'hi 1'          
                 i++;  // then it will increment the value and loop will keep on executing till i<=3
             }
             System.out.println("bye" + i); // as while executing the loop i value is 4 thus it will print 'bye 4'
         }
     }
    
  2. Do-While Loop - It is a type of loop that executes the code similar to a while loop but if the value that is given makes the condition false it will execute the statement inside it as it checks for the condition after executing the statements and therefore is an example of an Exit Control Loop.

     class dowhile
     {
         public static void main(String[] args) 
         {
             int i = 1;
             do
             {
                 System.out.println("hi "+ i + " time");
                 i++;
             }while(i <= 5);   // it will print the statement till i becomes 5 and exit the loop just when i become 6
         }    
     }
    
     ------&&----------
    
     class dowhile
     {
         public static void main(String[] args) 
         {
             int i = 6;
             do
             {
                 System.out.println("hi "+ i + " time");
                 i++;
             }while(i <= 5); // it will print the statement as "hi 6 time" and then exit the loop as i would become 7 which is greater than 5
         }    
     }
    
  3. For loop - It provides a concise way of writing a loop statement as it includes the initialization, condition and increment/decrement in one line thereby providing a shorter, easy-to-debug structure of looping.

     class forloop
     {
         public static void main(String[] args) 
         {      
             for( int i=1; i<=5; i++ )     // in 'for' loop u can inscribe more than one instruction and use ; to segregate command.
             {
                 System.out.println("Day  "+ i);
             }
         }
     }
    

NOTE -

  • For is best for loops that have some definite value.

  • 'While' is best for situations where we don't know what the end is, for example reading the text from a chapter or book or reading a database. But still, it is possible to use For loop.

  • 'Do While' is used when u want to use while and print something even if the value is false for once. But still 'Do while' has the least possible case.


Conclusion👨‍🏫👍

So, Well, this blog was so coding and concept intensive that even my circuits were getting 🔥🤯 from all the keystrokes!

Again thanks for joining me today, guys! 🙌 I hope you learned something new from my tech blog. 🤓 Until next time, take care and keep exploring the amazing world of technology! 🚀💻.

Also, give a heart button if u like it.😁

Did you find this article valuable?

Support Indrajit Mandal by becoming a sponsor. Any amount is appreciated!