Understanding Java Class, Main Method and Loops Control in Java – Part 3

[‘

n

In our last post ‘Working and code structure of Java‘ we emphasized in details of working of Java, Java Source File, Java Class File, Class (Public/Private), Method, Statement, Your first Java Program, Compilation and running of Java Program.

n

Here in this java learning series guide, we will understand how java class, main method and loops control works and also we will see basic codes using Java class with main method and loops control.

n

Understanding Java Class Method and Loops Control
Understanding Java Class Method and Loops Control – Part 3

n

Everything in Java goes in a class

n

Everything in Java is an object and class is a blueprint of object. Every piece of code in Java is placed under the curly braces of class. When you compile a Java Program it produces a class file. When you run Java Program you are not running the Program file in actual but the class.

n

When you run a Program in Java Virtual Machine (JVM), it loads the required class and then goes directly to the main () method. The program continues to run till the closing braces of main () method. The program start executing just after the main() method. A class must have a main () method. Not all the class (Private class) requires a main () method.

n

What goes inside main () Method?

n

A main () method is the place where magic starts. You can ask JVM to do anything within main() method via statement/instructions and loops.

n

What is loop?

n

Loop is an instruction or a number of instructions in sequence that keeps on repeating till the condition is reached. Loops are the logical structure of a programming language. Loop logical structure are typically used to do a process, check the condition, do a process, check the condition,….. till condition requirements are met.

n

Loops in Java

n

There are three different loop mechanism in Java.

n

1. while Loop

n

while Loop in Java is a control structure which is used to perform a task repeatedly for a certain number of times, as defined in boolean expression, till the expression test result is true. If the boolean expression text result is false the while loop will be ignored completely without being executed even a single time.

n

Syntax of while loop:

n

while (boolean expression)rn{rntstatement/instructionsrn}rn

n

An example of while Loop in Java:

n

public class While_looprn{rn    public static void main(String[] args)rn    {rn        int A = 100;rn        while(A>0)rn        {rn            System.out.println("The Value of A = " +A);rn            A=A-10;rn        }rn    }rn}rn

n

Sample Output

n

$ java While_loop rnrnThe Value of A = 100rnThe Value of A = 90rnThe Value of A = 80rnThe Value of A = 70rnThe Value of A = 60rnThe Value of A = 50rnThe Value of A = 40rnThe Value of A = 30rnThe Value of A = 20rnThe Value of A = 10rn

n

Anatomy of While_loop Program

n

// Public Class While_looprnpublic class While_looprn{rn    // main () Methodrn    public static void main(String[] args)rn    {rn        // declare an integer variable named 'A' and give it the value of 100rn        int A = 100;rn        // Keep looping as long as the value of A is greater than 0. 'A>0' here is the boolean                 rn           expressionrn        while(A>0)rn        {rnt // Statementrn            System.out.println("The Value of A = " +A);rn            // Post Decrement (by 10)rnt A=A-10;rn        }rn    }rn}rn

n

2. do..while Loop

n

do…while loop is very much similar to the while loop except the fact that it contains a do… before while to ensure that loop execute at least once.

n

Syntax of while loop:

n

do rn{rnstatement/instructionsrn}rnwhile (boolean expression);rn

n

You may see the above syntax which clearly shows that the 'do..' part of the loop executed before checking the boolean expression, if it is true or false. Hence no matter what the result (true/false) of boolean expression, the loop executes. If true it will execute till the condition is satisfied. If false it will be executed once.

n

An Example of do…while Loop in Java:

n

public class do_whilern{rn    public static void main(String[] args)rn    {rn        int A=100;rn        dorn        {rn            System.out.println("Value of A = " +A);rn            A=A-10;rn        }rn        while (A>=50);rn    }rn}rn

n

Sample Output

n

$ java do_while rnrnValue of A = 100rnValue of A = 90rnValue of A = 80rnValue of A = 70rnValue of A = 60rnValue of A = 50rn

n

Anatomy of do_while Program:

n

// public class do_whilernpublic class do_whilern{rn    // main () Methodrn    public static void main(String[] args)rn    {rn        // Declare a Integer Variable 'A' and assign it a value = 100rn        int A=100;rn        // do...while loop startsrn        dorn        {rn            // execute the below statement without checking boolean expression condition if true rn               or falsern            System.out.println("Value of A = " +A);rn            // Post Decrement (by 10)rn            A=A-10;rn        }rn        // Check condition. Loop the execute only till the value of Variable A is greater than or rn           equal to 50.rn        while (A>=50);rn    }rn}rn

n

3. for Loop

n

for_loop in Java is widely used for repetition control. It is used to iterate a task for specific number of times. For loop is used to control how many times the loop needs to execute to perform a task. for loop is only useful if you know how many times you need to execute the loop.

n

Syntax of for loop:

n

for (initialization; boolean-expression; update)rn{rnstatementrn}rnrnAn example of the for loop in Javarnrnpublic class for_looprn{rn    public static void main(String[] arge)rn    {rn        int A;rn        for (A=100; A>=0; A=A-7)rn        {rn            System.out.println("Value of A = " +A);rn        }rn    }rn}rn

n

Sample Output

n

$ java for_loop rnrnValue of A = 100rnValue of A = 93rnValue of A = 86rnValue of A = 79rnValue of A = 72rnValue of A = 65rnValue of A = 58rnValue of A = 51rnValue of A = 44rnValue of A = 37rnValue of A = 30rnValue of A = 23rnValue of A = 16rnValue of A = 9rnValue of A = 2rn

n

Anatomy of for_loop Program:

n

// public class for_looprnpublic class for_looprn{rn    // main () Methodrn    public static void main(String[] arge)rn    {rn        // Declare a Integer Variable Arn        int A;rn        // for loop starts. Here Initialization is A=100, boolean_expression is A>=0 and update is rn           A=A-7rn        for (A=100; A>=0; A=A-7)rn        {rn            // Statement        rn            System.out.println("Value of A = " +A);rn        }rn    }rn}rn

n

The Break and Continue keywords for loops in Java

n

1. The Break Keyword

n

As the name suggest the break keyword is used to stop the entire loop immediately. The break keyword must always be used inside the loop or switch statement. Once the loop breaks by using break; JVM starts executing the very next line of code outside of the loop. An example of break loop in Java is:

n

public class breakrn{rn    public static void main(String[] args)rn    {rn        int A = 100;rn        while(A>0)rn        {rn            System.out.println("The Value of A = " +A);rn            A=A-10;rn            if (A == 40)rn            {rn                break;rn            }rn        }rn    }rn}rn

n

Sample Output

n

$ java break rnrnThe Value of A = 100rnThe Value of A = 90rnThe Value of A = 80rnThe Value of A = 70rnThe Value of A = 60rnThe Value of A = 50rn

n

The Continue Keyword

n

The continue keyword can be used with any loop in Java. Continue keyword ask the loop to jump to the next iteration immediately. However it is interpreted differently by for loop and while/do…while loop.

n

Continue Keyword in for loop jumps to the next update statement.

n

An example of continue in for loop:

n

public class continue_for_looprn{rn    public static void main(String[] arge)rn    {rn        int A;rn        for (A=10; A>=0; A=A-1)rn        {rnt    if (A == 2)rntt{rnt        continue;rntt}rn            System.out.println("Value of A = " +A);rn        }rn    }rn}rn

n

Sample Output

n

$ java continue_for_looprnrnValue of A = 10rnValue of A = 9rnValue of A = 8rnValue of A = 7rnValue of A = 6rnValue of A = 5rnValue of A = 4rnValue of A = 3rnValue of A = 1rnValue of A = 0rn

n

Did you noticed, it skipped Value of A = 2. It does so by dumping to the next update statement.

n

2. Continue Keyword in while loop or do…while loop jumps to the boolean expression.

n

Well you can do it yourself. Its too easy. Just follow the above steps.

n

That’s all for now from my side. Hope I am doing good with Java Series and helping you. Keep Connected for more such posts. Don’t forget to provide us with your valuable feedback in comments below.

n

‘]