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:-
- Break & continue statements
- Switch case
- 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.
- For each loop / Enhanced for loop
- This loop uses to traverse collection of elements including arrays.
- While loop
- This is the very basic loop in java.
- While loop repeatedly executes the block as long as the given condition is true.
- 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.
Break & continue statements
- 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)
- From this statement loop will jump immediately to the next iteration of the loop.
- 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.
- If we check equality we use == for primitive data types and .equals() for object data types.