Jump Statement in Java Programming for Beginners

0
Jump Statement in Java Programming for Beginners
Jump Statement in Java Programming for Beginners

📑 Table of Contents

Jump Statement Definition in Java

Jump Statement: A jump statement in java is a control flow statement that allows break, continue and return the program to jump to a different part of the code. It is used to transfer the control from one point to another point in the program. Jump statements can be used with loops or condition based.

Types of Jump Statement in Java

  • break statement in Java
  • continue statement in Java
  • return statement in Java

Break Statement in Java Definition

Break Statement: A break statement can be used to exit a loop or switch statement prematurely. It is used to transfer the control out of the body of loop. In other word we can say that it terminates the current loop. Break statement are mostly used with loop(for, while, do while) and switch statement.

Break statement is usually used to terminate a case in the switch statement. Break statement in loops to instantly terminates the loop and program control goes to the next statement after the loop.

If break statement is used in nested loop within another loop the break statement will end the execution of the inner loop and Program control goes back to outer loop.

Syntax of Break Statement in Java

for(initialization; condition; increment)
{
 if(condition){
break;
}
body Statements;
}

Explain of Flowchart break Statement 

Step 1: Start
Step 2: Initialize loop variable
Step 3: Check Loop Condition 
        if false->Exit
        if true->Go inside Loop
Step 4: Check if condition(break)
           if true-> execute break
           Exit loop directly
Step 5: increment
Step 6: repeat Loop
Step 7: End

Example of without Using break Keyword

public class Jump{
public static void main(String[] args)
{
for(int i=1;i<=5;i++)
{
System.out.println(i);
}
}
}

============OUTPUT============
1
2
3
4
5


Example of break Statement in Java

public class while_loops{
public static void main(String[] args)
{
for(int i=1;i<=5;i++)
{
 if(i==4)
break;
System.out.println(i);
}
}
}

============OUTPUT============
1
2
3

Continue Statement in Java Definition

Continue Statement: A continue statement can be used to skip the current iteration of a loop and proceed to the next iteration. It is used to skip the next statement and continue the loop. Continue statement are mostly used with loop(for, while, do while).

continue statement works slightly similar to the break statement. The continue restarts the loop with the next value of item. All the line code below continue statement is skips. continue statement skips the test condition and increment value of the variable to execute again and In the while and do-while loops continue skips all the statements and program control goes to at the end of loop for tests condition.

Syntax of Continue Statement in Java

for(initialization; condition; increment)
{
 if(condition){
continue;
}
body Statements;
}

Example of Continue Statement in Java

public class jump{
public static void main(String[] args)
{
for(int i=1;i<=5;i++)
{
if(i==3)
continue;
System.out.println("Your Number is:"+i);
}
}
}

============OUTPUT============
Your Number is:1
Your Number is:2
Your Number is:4
Your Number is:5


Example of Continue Using While Loop in Java

public class jump{
public static void main(String[] args)
{
int i=1;
while(i<=5){
i++;
if(i==3){
continue;
}
System.out.println("Your Number is:"+i);
}
}
}

============OUTPUT============
Your Number is:1
Your Number is:2
Your Number is:4
Your Number is:5


Example of Real Life Continue Statement in Java

public class jump{
public static void main(String[] args)
{
String[] data = {"Jiocoding", "Website", "Facebook", "Java", "Instagram"};
for (int i = 0; i < data.length; i++)
{
if (data[i].equals("Website")) {
continue;
}

System.out.println("Continue Data: " + data[i]);
}
}
}
============OUTPUT============
Continue Data: Jiocoding

Continue Data: Facebook
Continue Data: Java
Continue Data: Instagram

Return Statement in Java Definition

Return Statement: A return can be used to exit from a method and optionally return a value to the method caller. Return statement terminates the current function and transfer the control to the calling function. We can also use return statement to transfer value from one function to another.

Example of While Loop in Java

public class jump{
int addition()
{
 int a=10,b=30;
return (a+b);
}
public static void main(String[] args){
jump j=new jump();
int res=j.addition();
System.out.println("Result is:"+res);
}
}

============OUTPUT============
Result is:40


Conclusion of Jump Statements in Java

Conclusion: A Java jump statements can be used to perform termination of loops or skipping the value specified condition. I can explain everyone topic jump statements if your any query or questions jump statements related than you can contact me by email.

Post a Comment

0 Comments
Post a Comment (0)
To Top