Hi people, this blog is going to discuss about very basic points and main concepts you must know before using JAVA language. This third article will be helpful to build the foundation of JAVA programming. In this tutorial we will learn about:-
- Loops
- Break & continue statements
- Switch case
Loops
- For loop
- for loop is a repetition block that needs to be executed a specific number of times.
- This loop is useful when you know how many times a block should be repeated.
Code example
Console
- For each loop / Enhanced for loop
- This loop uses to traverse collection of elements including arrays.
Code example
Console
- While loop
- This is the very basic loop in java.
- While loop repeatedly executes the block as long as the given condition is true.
Code example
Console
- Do while loop
- This one similar to while
- Only differentiation is do while loop is guaranteed to execute at least one time.
- Do while loop check condition at last.
Code example
Console
Break & continue statements
Break
- This uses to terminate the loop immediately.
- After terminate next statement will be executed. (outside the loop)
- Break statement also uses to terminate a case in the switch statement. (look at the next topic)
Code example
Console
Continue
- From this statement loop will jump immediately to the next iteration of the loop.
Code example
Console
Switch case
- The switch statement uses to check equality of set of values for one variable.
- Each value is called a case.
- The variable used in a switch can only be integers, convertible integers (byte, short, char) and strings.
- Using break statement a switch can be terminated after execute a matched case.
- If we not using break statement within cases then all cases will execute until a brake is reached.
- A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.
Code example
Console
Important
- If we check equality we use == for primitive data types and .equals() for object data types.
Code example
HelloWorld .java
HumanBeing.java
Console
JAVA Chapter IÂ Â Â Â Â Â Â Â JAVA Chapter II