0% found this document useful (0 votes)
2 views7 pages

Java 1 (Control Statements - Jump Statements)

Uploaded by

Ayasha Choudhary
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Java 1 (Control Statements - Jump Statements)

Uploaded by

Ayasha Choudhary
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

BCA : 5TH SEM

BC-504 : FUNDAMENTALS OF JAVA PROGRAMMING


RAM GOPAL GUPTA
ASSOCIATE PROFESSOR
DISCLAIMER

Contents of the tutorial, shown image(s) (if shown here) and movie(s) (if shown here)
are just compilation of research work/creation of various authors/organizations.
Contents are collected from various research papers, reports, books, websites and other
sources.

BC-504 : FUNDAMENTALS OF JAVA PROGRAMMING (HTTPS://[Link])


UNIT-1
CONTROL STRUCTURES IN JAVA

3. Jump statements

Break Statement

When a break statement is encountered inside a loop, the loop is immediately terminated and the

program control resumes at the next statement following the loop.

The Java break statement is used to break loop or switch statement. It breaks the current flow of the

program at specified condition. In case of inner loop, it breaks only inner loop.

We can use Java break statement in all types of loops such as for loop, while loop and do-while

loop.

Syntax:
jump-statement;
break;

BC-504 : FUNDAMENTALS OF JAVA PROGRAMMING (HTTPS://[Link])


Example:
1. //Java Program to demonstrate the use of break statement
2. //inside the for loop.
3. public class BreakExample {
4. public static void main(String[] args) {
5. //using for loop
6. for(int i=1;i<=10;i++){
7. if(i==5){
8. //breaking the loop
9. break;
10. }
11. [Link](i);
12. }
13. }
14. }

Example:
1. public class SwitchExample {
2. public static void main(String[] args) {
3. //Declaring a variable for switch expression
4. int number=20;
5. //Switch expression
6. switch(number){
7. //Case statements
8. case 10: [Link]("10");
9. break;
10. case 20: [Link]("20");
11. break;
12. case 30: [Link]("30");
13. break;
14. //Default case statement
15. default:[Link]("Not in 10, 20 or 30");
16. }
17. }
18. }

BC-504 : FUNDAMENTALS OF JAVA PROGRAMMING (HTTPS://[Link])


Continue Statement

The continue statement is used in loop control structure when you need to jump to the next iteration

of the loop immediately. It can be used with for loop or while loop.

The Java continue statement is used to continue the loop. It continues the current flow of the

program and skips the remaining code at the specified condition. In case of an inner loop, it

continues the inner loop only.

We can use Java continue statement in all types of loops such as for loop, while loop and do-while

loop.

Syntax:
jump-statement;
continue;

BC-504 : FUNDAMENTALS OF JAVA PROGRAMMING (HTTPS://[Link])


Example:
1. //Java Program to demonstrate the use of continue statement
2. //inside the for loop.
3. public class ContinueExample {
4. public static void main(String[] args) {
5. //for loop
6. for(int i=1;i<=10;i++){
7. if(i==5){
8. //using continue statement
9. continue;//it will skip the rest statement
10. }
11. [Link](i);
12. }
13. }
14. }

BC-504 : FUNDAMENTALS OF JAVA PROGRAMMING (HTTPS://[Link])


Return Statement

The return statement is used to explicitly return from a method. That is, it causes a program control

to transfer back to the caller of the method.

Syntax:
jump-statement;
return;

Example:
//Java Program to demonstrate the use of return statement
public class ReturnExample {
public static void main(String[] args) {
//for loop
boolean t=true;
[Link](“before the return.”);
if(t){
//using return statement
return; // Compiler will bypass every statement after return
}
[Link](“This won’t execute”);
}
}

BC-504 : FUNDAMENTALS OF JAVA PROGRAMMING (HTTPS://[Link])

You might also like