- Java continue and return keyword
- Control flow diagram of continue statement
- Java Program of continue Statement
- Can I use break and continue statement together in a loop ?
- Labeled continue Statement
- Java Program of labeled continue statement
- Java return Statement
- Who get’s the value that is returned by method ?
- Java Program of return statement
- Java for statement return
- 7 — Statements
- 7.1 Simple Statements
- 7.2 Compound Statements
- 7.3 return Statements
- 7.4 if, if-else, if else-if else Statements
- 7.5 for Statements
- 7.6 while Statements
- 7.7 do-while Statements
- 7.8 switch Statements
- 7.9 try-catch Statements
Java continue and return keyword
Sometime you may require that for a particular repetition of a loop, you don’t want to execute few or all instructions of loop body. To solve such requirements java provides continue keyword which allows programmers to skip the current iteration of a loop and move to the next iteration, which means if you want to skip the execution of code inside a loop for a particular iteration, you can use continue statement. Only the code written after the continue statement will be skipped, code before the continue statement will execute as usual for that iteration.
continue statement can be used only inside for , while and do while loops of java. In case of break statement the control flow exits from the loop, but in case of continue statement, the control flow will not be exited, only the current iteration of loop will be skipped. It is mostly used along with conditional statements( if , else ). The syntax of continue statement is:
Control flow diagram of continue statement
Once continue statement is executed inside for loop, the control flow moves back to for loop to evaluate increment/decrement expression and then condition expression. If continue statement is executed inside while and do while loop, the control flow jumps to condition expression for evaluation.
Java Program of continue Statement
class
ContinueStatement <public static void
main(String [] args) <for
(int
i=1; iif(i==4) <continue
; > System.out.println("i string">"line after for loop"
); > >
i = 1
i = 2
i = 3
i = 5
line after for loop
As you can see in above program, for i==4 the continue statement is executed which skips the current iteration and move to the next iteration, that is why value i = 4 is not printed.
If continue statement is used inside any inner loop, it skips current iteration of inner loop only, the outer loop will be executed as usual.
class
InnerLoppContinueStatement <public static void
main(String [] args) <for
(int
i=1; i"Outer loop i keyword">for(int
j=1; jif(i==2) <continue
; > System.out.println(" j string">"line after outer for loop"
); > >
Outer loop i = 1
j = 1
j = 2
Outer loop i = 2
Outer loop i = 3
j = 1
j = 2
line after outer for loop
Can I use break and continue statement together in a loop ?
Yes you can definitely use both statements in a loop.
Labeled continue Statement
A Labeled continue statement allows programmer to skip the current iteration of that label.
Java Program of labeled continue statement
class
LabeledContinueStatement <public static void
main(String [] args) < firstLable:for
(int
i=1;i<=3;i++) < System.out.println("Outer loop i keyword">for
(int
j=1;j<=3;j++) <if
(i==2) <continue
firstLable; > System.out.println(" j string">"line after end of firstLable "
); > >
Outer loop i = 1
j = 1
j = 2
j = 3
Outer loop i = 2
Outer loop i = 3
j = 1
j = 2
j = 3
line after end of firstLable
In above program as soon as the value of i becomes 2, the continue firstLable; statement is executed which moves the execution flow to outer loop for next iteration, that is why inner loop doesn’t print anything for i=2 .
Java return Statement
As the return keyword itself suggest that it is used to return something. Java return statement is used to return some value from a method. Once a return statement is executed, the control flow exited from current method and returns to the calling method. The syntax of return statement is:
Who get’s the value that is returned by method ?
The caller of the method. This value can be assigned in some variable.
The return statement given above has two forms:
- First that returns a value. To return a value, just place the value or expression after return keyword. for eg. return 10 , return a+b , return «refresh java» etc. The data type of the returned value must match the return type of it’s method declaration.
- Second that doesn’t returns a value. To use, just place return keyword. When the return type of method is declared as void , you can use this form of return as it doesn’t return a value, or you can also leave out to use return keyword for such methods.
Java Program of return statement
class
ReturnStatement <public static void
main(String [] args) <int
sum = sum(10,20); System.out.println("Sum keyword">public static int
sum(int
a,int
b) <return
a+b; > >
In above program, a and b are integers, so a+b will also be an integer which is equivalent to return type of method sum which is also an int .
- continue statement cannot be used outside of a loop, if used it will result in compile time error.
- Use continue statement more precisely inside while and do while block, if not used precisely, it may result as infinite loop
- Ensure all letter of continue keyword is small, can not use Continue, contiNue, CONTINUE etc.
- Ensure all letter of return keyword is small, can not use Return, RETURN etc.
- return keyword can be used anywhere inside method, but not outside method.
Java for statement return
The information on this page is for Archive Purposes Only
7 — Statements
7.1 Simple Statements
Each line should contain at most one statement. Example:
argv++; // Correct argc--; // Correct argv++; argc--; // AVOID!
7.2 Compound Statements
Compound statements are statements that contain lists of statements enclosed in braces » < statements >«. See the following sections for examples.
- The enclosed statements should be indented one more level than the compound statement.
- The opening brace should be at the end of the line that begins the compound statement; the closing brace should begin a line and be indented to the beginning of the compound statement.
- Braces are used around all statements, even single statements, when they are part of a control structure, such as an if-else or for statement. This makes it easier to add statements without accidentally introducing bugs due to forgetting to add braces.
7.3 return Statements
A return statement with a value should not use parentheses unless they make the return value more obvious in some way. Example:
return;return myDisk.size(); return (size ? size : defaultSize);
7.4 if, if-else, if else-if else Statements
The if-else class of statements should have the following form:
if (condition) if (condition) < statements; >else < statements; >if (condition) < statements; >else if (condition) < statements; >else
Note: if statements always use braces, <> . Avoid the following error-prone form:
if (condition) //AVOID! THIS OMITS THE BRACES <>!statement;
7.5 for Statements
A for statement should have the following form:
for (initialization; condition; update)
An empty for statement (one in which all the work is done in the initialization, condition, and update clauses) should have the following form:
for (initialization; condition; update);
When using the comma operator in the initialization or update clause of a for statement, avoid the complexity of using more than three variables. If needed, use separate statements before the for loop (for the initialization clause) or at the end of the loop (for the update clause).
7.6 while Statements
A while statement should have the following form:
An empty while statement should have the following form:
7.7 do-while Statements
A do-while statement should have the following form:
7.8 switch Statements
A switch statement should have the following form:
Every time a case falls through (doesn’t include a break statement), add a comment where the break statement would normally be. This is shown in the preceding code example with the /* falls through */ comment.
Every switch statement should include a default case. The break in the default case is redundant, but it prevents a fall-through error if later another case is added.
7.9 try-catch Statements
A try-catch statement should have the following format:
try catch (ExceptionClass e)
A try-catch statement may also be followed by finally , which executes regardless of whether or not the try block has completed successfully.
try catch (ExceptionClass e) < statements; >finally