If, If..else Statement in Java with Examples
When we need to execute a set of statements based on a condition then we need to use control flow statements
four types of control statements that you can use in java programs based on the requirement
a) if statement
b) nested if statement
c) if-else statement
d) if-else-if statement
Switch Case statement in Java with example
Switch case statement is used when we have number of options (or choices) and we may need to perform a different task for each choice.
The syntax of Switch case statement looks like this –
switch (variable or an integer expression)
{
case constant:
//Java code
;
case constant:
//Java code
;
default:
//Java code
;
}
For loop in Java with example
Loops are used to execute a set of statements repeatedly until a particular condition is satisfied. In Java we have three types of basic loops: for, while and do-while. In this tutorial we will learn how to use “for loop” in Java.
Syntax of for loop:
for(initialization; condition ; increment/decrement)
{
statement(s);
}
Infinite for loop
The importance of Boolean expression and increment/decrement operation co-ordination:
class ForLoopExample2 {
public static void main(String args[]){
for(int i=1; i>=1; i++){
System.out.println("The value of i is: "+i);
}
}
}
// infinite loop
for ( ; ; ) {
// statement(s)
}
While loop in Java with examples
loops are used to execute a set of statements repeatedly until a particular condition is satisfied.
Syntax of while loop
while(condition)
{
statement(s);
}
Note: The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely.
Infinite while loop
class WhileLoopExample2 {
public static void main(String args[]){
int i=10;
while(i>1)
{
System.out.println(i);
i++;
}
}
}
This loop would never end, its an infinite while loop. This is because condition is i>1 which would always be true as we are incrementing the value of i inside while loop.
Here is another example of infinite while loop:
while (true){
statement(s);
}
do-while loop in Java with example
do-while loop is similar to while loop, however there is a difference between them: In while loop, condition is evaluated before the execution of loop’s body but in do-while loop condition is evaluated after the execution of loop’s body.
Syntax of do-while loop:
do
{
statement(s);
} while(condition);
How do-while loop works?
First, the statements inside loop execute and then the condition gets evaluated, if the condition returns true then the control gets transferred to the “do” else it jumps to the next statement after do-while.
Java for-each Loop
for-each Loop Sytnax
The syntax of the Java for-each loop is:
for(dataType item : array) {
...
}
Here,
- array – an array or a collection
- item – each item of array/collection is assigned to this variable
- dataType – the data type of the array/collection
Example 1: Print Array Elements
// print array elements
class Main {
public static void main(String[] args) {
// create an array
int[] numbers = {3, 9, 5, -5};
// for each loop
for (int number: numbers) {
System.out.println(number);
}
}
}
Output
3 9 5 -5
Here, we have used the for-each loop to print each element of the numbers array one by one.
- In the first iteration, item will be 3.
- In the second iteration, item will be 9.
- In the third iteration, item will be 5.
- In the fourth iteration, item will be -5.
Continue Statement in Java with example
Continue statement is mostly used inside loops. Whenever it is encountered inside a loop, control directly jumps to the beginning of the loop for next iteration, skipping the execution of statements inside loop’s body for the current iteration. This is particularly useful when you want to continue the loop but do not want the rest of the statements(after continue statement) in loop body to execute for that particular iteration.
Syntax:
continue word followed by semi colon.
continue;
Example: continue statement inside for loop
public class ContinueExample {
public static void main(String args[]){
for (int j=0; j<=6; j++)
{
if (j==4)
{
continue;
}
System.out.print(j+" ");
}
}
}
Output:
0 1 2 3 5 6
Break statement in Java with example
The break statement is usually used in following two scenarios:
a) Use break statement to come out of the loop instantly. Whenever a break statement is encountered inside a loop, the control directly comes out of loop and the loop gets terminated for rest of the iterations. It is used along with if statement, whenever used inside loop so that the loop gets terminated for a particular condition.
The important point to note here is that when a break statement is used inside a nested loop, then only the inner loop gets terminated.
b) It is also used in switch case control. Generally all cases in switch case are followed by a break statement so that whenever the program control jumps to a case, it doesn’t execute subsequent cases (see the example below). As soon as a break is encountered in switch-case block, the control comes out of the switch-case body.
Programmes
- Write a program to print numbers from 1 to 10.
- Write a program to calculate the sum of first 10 natural number.
- Write a program that prompts the user to input a positive integer. It should then print the multiplication table of that number.
- Write a program to find the factorial value of any number entered through the keyboard.
- Two numbers are entered through the keyboard. Write a program to find the value of one number raised to the power of another. (Do not use Java built-in method)
- Write a program that prompts the user to input an integer and then outputs the number with the digits reversed. For example, if the input is 12345, the output should be 54321.
- Write a program that reads a set of integers, and then prints the sum of the even and odd integers.
- Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number.
- Write a program to enter the numbers till the user wants and at the end it should display the count of positive, negative and zeros entered.