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.
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.
Everything in Java goes in a class
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.
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.
What goes inside main () Method?
A main () method is the place where magic starts. You can ask JVM to do anything within main() method via statement/instructions and loops.
What is loop?
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.
Loops in Java
There are three different loop mechanism in Java.
1. while Loop
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.
Syntax of while loop:
while (boolean expression) { statement/instructions }
An example of while Loop in Java:
public class While_loop { public static void main(String[] args) { int A = 100; while(A>0) { System.out.println("The Value of A = " +A); A=A-10; } } }
Sample Output
$ java While_loop The Value of A = 100 The Value of A = 90 The Value of A = 80 The Value of A = 70 The Value of A = 60 The Value of A = 50 The Value of A = 40 The Value of A = 30 The Value of A = 20 The Value of A = 10
Anatomy of While_loop Program
// Public Class While_loop public class While_loop { // main () Method public static void main(String[] args) { // declare an integer variable named 'A' and give it the value of 100 int A = 100; // Keep looping as long as the value of A is greater than 0. 'A>0' here is the boolean expression while(A>0) { // Statement System.out.println("The Value of A = " +A); // Post Decrement (by 10) A=A-10; } } }
2. do..while Loop
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.
Syntax of while loop:
do { statement/instructions } while (boolean expression);
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.
An Example of do…while Loop in Java:
public class do_while { public static void main(String[] args) { int A=100; do { System.out.println("Value of A = " +A); A=A-10; } while (A>=50); } }
Sample Output
$ java do_while Value of A = 100 Value of A = 90 Value of A = 80 Value of A = 70 Value of A = 60 Value of A = 50
Anatomy of do_while Program:
// public class do_while public class do_while { // main () Method public static void main(String[] args) { // Declare a Integer Variable 'A' and assign it a value = 100 int A=100; // do...while loop starts do { // execute the below statement without checking boolean expression condition if true or false System.out.println("Value of A = " +A); // Post Decrement (by 10) A=A-10; } // Check condition. Loop the execute only till the value of Variable A is greater than or equal to 50. while (A>=50); } }
3. for Loop
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.
Syntax of for loop:
for (initialization; boolean-expression; update) { statement } An example of the for loop in Java public class for_loop { public static void main(String[] arge) { int A; for (A=100; A>=0; A=A-7) { System.out.println("Value of A = " +A); } } }
Sample Output
$ java for_loop Value of A = 100 Value of A = 93 Value of A = 86 Value of A = 79 Value of A = 72 Value of A = 65 Value of A = 58 Value of A = 51 Value of A = 44 Value of A = 37 Value of A = 30 Value of A = 23 Value of A = 16 Value of A = 9 Value of A = 2
Anatomy of for_loop Program:
// public class for_loop public class for_loop { // main () Method public static void main(String[] arge) { // Declare a Integer Variable A int A; // for loop starts. Here Initialization is A=100, boolean_expression is A>=0 and update is A=A-7 for (A=100; A>=0; A=A-7) { // Statement System.out.println("Value of A = " +A); } } }
The Break and Continue keywords for loops in Java
1. The Break Keyword
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:
public class break { public static void main(String[] args) { int A = 100; while(A>0) { System.out.println("The Value of A = " +A); A=A-10; if (A == 40) { break; } } } }
Sample Output
$ java break The Value of A = 100 The Value of A = 90 The Value of A = 80 The Value of A = 70 The Value of A = 60 The Value of A = 50
The Continue Keyword
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.
Continue Keyword in for loop jumps to the next update statement.
An example of continue in for loop:
public class continue_for_loop { public static void main(String[] arge) { int A; for (A=10; A>=0; A=A-1) { if (A == 2) { continue; } System.out.println("Value of A = " +A); } } }
Sample Output
$ java continue_for_loop Value of A = 10 Value of A = 9 Value of A = 8 Value of A = 7 Value of A = 6 Value of A = 5 Value of A = 4 Value of A = 3 Value of A = 1 Value of A = 0
Did you noticed, it skipped Value of A = 2. It does so by dumping to the next update statement.
2. Continue Keyword in while loop or do…while loop jumps to the boolean expression.
Well you can do it yourself. Its too easy. Just follow the above steps.
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.
A Java program is a sequence of Java instructions that are executed in a certain order. Since the Java instructions are executed in a certain order, a Java program has a start and an end.
Very good and worth reading article for Java beginners to know about Java main method, i was looking for the same and here i have found it perfect, really the main method where magic start.
thanks for sharing, helpful and valuable.
Hey Avishek,
Visited Tecmint after a long time. Cool new look, very elegant.
Which plugin are you using for commands and examples, if you do not mind sharing it.