{"id":3443,"date":"2019-06-20T11:42:17","date_gmt":"2019-06-20T11:42:17","guid":{"rendered":"http:\/\/softlect.in\/?p=3443"},"modified":"2019-06-20T11:58:57","modified_gmt":"2019-06-20T11:58:57","slug":"java-control-statements","status":"publish","type":"post","link":"http:\/\/softlect.com\/index.php\/java-control-statements\/","title":{"rendered":"Java Control Statements"},"content":{"rendered":"<h1><a name=\"_Toc179309051\"><\/a><strong>Control Statements<\/strong><\/h1>\n<p>&nbsp;<\/p>\n<p>A programming language uses <em>control <\/em>statements to cause the flow of execution to advance and branch based on changes to the state of a program. Java\u2019s program control statements can be put into the following categories: selection, iteration, and jump. <em>Selection <\/em>statements allow your program to choose different paths of execution based upon the outcome of an expression or the state of a variable. <em>Iteration <\/em>statements enable program execution to repeat one or more statements that is, iteration statements form loops. <em>Jump <\/em>statements allow your program to execute in a nonlinear fashion.<\/p>\n<p><em>\u00a0<\/em><\/p>\n<h2><a name=\"_Toc179309052\"><\/a><strong>Java\u2019s Selection Statements<\/strong><\/h2>\n<p>Java supports two selection statements: <strong>if <\/strong>and <strong>switch<\/strong>. These statements allow you to control the flow of your program\u2019s execution based upon conditions known only during run time.<\/p>\n<h3><a name=\"_Toc179309053\"><\/a>if<\/h3>\n<p>The <strong>if <\/strong>statement is Java\u2019s conditional branch statement. It can be used to route program execution through two different paths. Here is the general form of the <strong>if <\/strong>statement:<\/p>\n<p>&nbsp;<\/p>\n<p>if (<em>condition<\/em>)<\/p>\n<p><em>statement1<\/em>;<\/p>\n<p>else<\/p>\n<p><em>statement2<\/em>;<\/p>\n<p>&nbsp;<\/p>\n<p>Here, each <em>statement <\/em>may be a single statement or a compound statement enclosed in curly braces that is, a <em>block<\/em>. The <em>condition <\/em>is any expression that returns a <strong>boolean <\/strong>value. The <strong>else <\/strong>clause is optional.<\/p>\n<p>The <strong>if <\/strong>works like this: If the <em>condition <\/em>is true, then <em>statement1 <\/em>is executed. Otherwise, <em>statement2 <\/em>\u00a0is executed. In no case will both statements be executed. For example, consider the following:<\/p>\n<p>&nbsp;<\/p>\n<p>int a, b;<\/p>\n<p>\/\/ &#8230;<\/p>\n<p>if(a &lt; b) a = 0;<\/p>\n<p>else b = 0;<\/p>\n<p>&nbsp;<\/p>\n<p>Here, if <strong>a <\/strong>is less than <strong>b<\/strong>, then <strong>a <\/strong>is set to zero. Otherwise, <strong>b <\/strong>is set to zero. In no case are they both set to zero. Most often, the expression used to control the <strong>if <\/strong>will involve the relational operators. It is possible to control the <strong>if <\/strong>using a single <strong>boolean <\/strong>variable, as shown in this code fragment:<\/p>\n<p>&nbsp;<\/p>\n<p>boolean dataAvailable;<\/p>\n<p>\/\/ &#8230;<\/p>\n<p>if (dataAvailable)<\/p>\n<p>ProcessData();<\/p>\n<p>else<\/p>\n<p>waitForMoreData();<\/p>\n<p>&nbsp;<\/p>\n<p>Remember, only one statement can appear directly after the <strong>if <\/strong>or the <strong>else<\/strong>. If you want to include more statements, you\u2019ll need to create a block, as in this fragment:<\/p>\n<p>&nbsp;<\/p>\n<p>int bytesAvailable;<\/p>\n<p>\/\/ &#8230;<\/p>\n<p>if (bytesAvailable &gt; 0) {<\/p>\n<p>ProcessData();<\/p>\n<p>bytesAvailable -= n;<\/p>\n<p>} else<\/p>\n<p>waitForMoreData();<\/p>\n<p>&nbsp;<\/p>\n<p>Here, both statements within the <strong>if <\/strong>block will execute if <strong>bytesAvailable <\/strong>is greater than zero. For example, consider the following code fragment:<\/p>\n<p>&nbsp;<\/p>\n<p>int bytesAvailable;<\/p>\n<p>\/\/ &#8230;<\/p>\n<p>if (bytesAvailable &gt; 0) {<\/p>\n<p>ProcessData();<\/p>\n<p>bytesAvailable -= n;<\/p>\n<p>} else<\/p>\n<p>waitForMoreData();<\/p>\n<p>bytesAvailable = n;<\/p>\n<p>&nbsp;<\/p>\n<p>It seems clear that the statement <strong>bytesAvailable = n; <\/strong>was intended to be executed inside the <strong>else <\/strong>clause, because of the indentation level. The preceding example is fixed in the code that follows:<\/p>\n<p>&nbsp;<\/p>\n<p>int bytesAvailable;<\/p>\n<p>\/\/ &#8230;<\/p>\n<p>if (bytesAvailable &gt; 0) {<\/p>\n<p>ProcessData();<\/p>\n<p>bytesAvailable -= n;<\/p>\n<p>} else {<\/p>\n<p>waitForMoreData();<\/p>\n<p>bytesAvailable = n;<\/p>\n<p>}<\/p>\n<h3><a name=\"_Toc179309054\"><\/a>Nested ifs<\/h3>\n<p>A <em>nested <\/em><strong>if <\/strong>is an <strong>if <\/strong>statement that is inside another <strong>if <\/strong>or <strong>else<\/strong>. Nested <strong>if<\/strong>s are very common in programming. When you nest <strong>if<\/strong>s, the main thing to remember is that an <strong>else <\/strong>statement always refers to the nearest <strong>if <\/strong>statement. Here is an example:<\/p>\n<p>&nbsp;<\/p>\n<p>if(i == 10) {<\/p>\n<p>if(j &lt; 20) a = b;<\/p>\n<p>if(k &gt; 100) c = d; \/\/ this if is<\/p>\n<p>else a = c; \/\/ associated with this else<\/p>\n<p>}<\/p>\n<p>else a = d; \/\/ this else refers to if(i == 10)<\/p>\n<p>&nbsp;<\/p>\n<p>As the comments indicate, the final <strong>else <\/strong>is not associated with <strong>if(j&lt;20)<\/strong>, because it is not in the same block even though it is the nearest <strong>if <\/strong>without an <strong>else<\/strong>. Rather, the final <strong>else <\/strong>is associated with <strong>if(i==10)<\/strong>. The inner <strong>else <\/strong>refers to <strong>if(k&gt;100)<\/strong>, because it is the closest <strong>if <\/strong>within the same block.<\/p>\n<h3><a name=\"_Toc179309055\"><\/a>The if-else-if Ladder<\/h3>\n<p>A common programming construct that is based upon a sequence of nested <strong>if<\/strong>s is the <strong><em>if-else-if <\/em><\/strong><em>ladder<\/em>. It looks like this:<\/p>\n<p>&nbsp;<\/p>\n<p>if(<em>condition<\/em>)<\/p>\n<p><em>statement;<\/em><\/p>\n<p>else if<em>(condition<\/em>)<\/p>\n<p><em>statement<\/em>;<\/p>\n<p>else if(<em>condition<\/em>)<\/p>\n<p><em>statement<\/em>;<\/p>\n<p>&#8230;<\/p>\n<p>else<\/p>\n<p><em>statement<\/em>;<\/p>\n<p>&nbsp;<\/p>\n<p>The <strong>if <\/strong>statements are executed from the top down. As soon as one of the conditions controlling the <strong>if <\/strong>is <strong>true<\/strong>, the statement associated with that <strong>if <\/strong>is executed, and the rest of the ladder is skiped. If none of the conditions is true, then the final <strong>else <\/strong>statement will be executed. The final <strong>else <\/strong>acts as a default condition; that is, if all other conditional tests fail, then the last <strong>else <\/strong>statement is performed. If there is no final <strong>else <\/strong>and all other conditions are <strong>false<\/strong>, then no action will take place. Here is a program that uses an <strong>if-else-if <\/strong>ladder to determine which season a particular month is in.<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Demonstrate if-else-if statements.<\/p>\n<p>class IfElse {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>int month = 4; \/\/ April<\/p>\n<p>String season;<\/p>\n<p>if(month == 12 || month == 1 || month == 2)<\/p>\n<p>season = &#8220;Winter&#8221;;<\/p>\n<p>else if(month == 3 || month == 4 || month == 5)<\/p>\n<p>season = &#8220;Spring&#8221;;<\/p>\n<p>else if(month == 6 || month == 7 || month == 8)<\/p>\n<p>season = &#8220;Summer&#8221;;<\/p>\n<p>else if(month == 9 || month == 10 || month == 11)<\/p>\n<p>season = &#8220;Autumn&#8221;;<\/p>\n<p>else<\/p>\n<p>season = &#8220;Bogus Month&#8221;;<\/p>\n<p>System.out.println(&#8220;April is in the &#8221; + season + &#8220;.&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Here is the output produced by the program:<\/p>\n<p>April is in the Spring.<\/p>\n<h3><a name=\"_Toc179309056\"><\/a>switch<\/h3>\n<p>The <strong>switch <\/strong>statement is Java\u2019s multiway branch statement. It provides an easy way to send execution to different parts of your code based on the value of an expression. As such, it often provides a better alternative than a large series of <strong>if-else-if <\/strong>statements. Here is the general form of a <strong>switch <\/strong>statement:<\/p>\n<p>&nbsp;<\/p>\n<p>switch (<em>expression<\/em>) {<\/p>\n<p>case <em>value1<\/em>:<\/p>\n<p>\/\/ statement sequence<\/p>\n<p>break;<\/p>\n<p>case <em>value2<\/em>:<\/p>\n<p>\/\/ statement sequence<\/p>\n<p>break;<\/p>\n<p>&#8230;<\/p>\n<p>case <em>valueN<\/em>:<\/p>\n<p>\/\/ statement sequence<\/p>\n<p>break;<\/p>\n<p>default:<\/p>\n<p>\/\/ default statement sequence<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>The <em>expression <\/em>must be of type <strong>byte<\/strong>, <strong>short<\/strong>, <strong>int<\/strong>, or <strong>char<\/strong>; each of the <em>values <\/em>specified in the <strong>case <\/strong>statements must be of a type compatible with the expression. Each <strong>case <\/strong>value must be a unique literal that is, it must be a constant, not a variable. Duplicate <strong>case <\/strong>values are not allowed. The <strong>switch <\/strong>statement works like this: The value of the expression is compared with each of the literal values in the <strong>case <\/strong>statements. If a match is found, the code sequence following that <strong>case <\/strong>statement is executed. If none of the constants matches the value of the expression, then the <strong>default <\/strong>statement is executed. However, the <strong>default <\/strong>statement is optional. If no <strong>case <\/strong>matches and no <strong>default <\/strong>is present, then no further action is taken. The <strong>break <\/strong>statement is used inside the <strong>switch <\/strong>to terminate a statement sequence. When a <strong>break <\/strong>statement is encountered, execution branches to the first line of code that follows the entire <strong>switch <\/strong>statement. This has the effect of \u201cjumping out\u201d of the <strong>switch<\/strong>. Here is a simple example that uses a <strong>switch <\/strong>statement:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ A simple example of the switch.<\/p>\n<p>class SampleSwitch {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>for(int i=0; i&lt;6; i++)<\/p>\n<p>switch(i) {<\/p>\n<p>case 0:<\/p>\n<p>System.out.println(&#8220;i is zero.&#8221;);<\/p>\n<p>break;<\/p>\n<p>case 1:<\/p>\n<p>System.out.println(&#8220;i is one.&#8221;);<\/p>\n<p>break;<\/p>\n<p>case 2:<\/p>\n<p>System.out.println(&#8220;i is two.&#8221;);<\/p>\n<p>break;<\/p>\n<p>case 3:<\/p>\n<p>System.out.println(&#8220;i is three.&#8221;);<\/p>\n<p>break;<\/p>\n<p>default:<\/p>\n<p>System.out.println(&#8220;i is greater than 3.&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>The output produced by this program is shown here:<\/p>\n<p>i is zero.<\/p>\n<p>i is one.<\/p>\n<p>i is two.<\/p>\n<p>i is three.<\/p>\n<p>i is greater than 3.<\/p>\n<p>i is greater than 3.<\/p>\n<p>&nbsp;<\/p>\n<p>As you can see, each time through the loop, the statements associated with the <strong>case <\/strong>constant that matches <strong>i <\/strong>are executed. All others are bypassed. After <strong>i <\/strong>is greater than 3, no <strong>case <\/strong>statements match, so the <strong>default <\/strong>statement is executed. The <strong>break <\/strong>statement is optional. If you omit the <strong>break<\/strong>, execution will continue on into the next <strong>case<\/strong>. It is sometimes desirable to have multiple <strong>case<\/strong>s without <strong>break <\/strong>statements between them. For example, consider the following program:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ In a switch, break statements are optional.<\/p>\n<p>class MissingBreak {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>for(int i=0; i&lt;12; i++)<\/p>\n<p>switch(i) {<\/p>\n<p>case 0:<\/p>\n<p>case 1:<\/p>\n<p>case 2:<\/p>\n<p>case 3:<\/p>\n<p>case 4:<\/p>\n<p>System.out.println(&#8220;i is less than 5&#8221;);<\/p>\n<p>break;<\/p>\n<p>case 5:<\/p>\n<p>case 6:<\/p>\n<p>case 7:<\/p>\n<p>case 8:<\/p>\n<p>case 9:<\/p>\n<p>System.out.println(&#8220;i is less than 10&#8221;);<\/p>\n<p>break;<\/p>\n<p>default:<\/p>\n<p>System.out.println(&#8220;i is 10 or more&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>This program generates the following output:<\/p>\n<p>i is less than 5<\/p>\n<p>i is less than 5<\/p>\n<p>i is less than 5<\/p>\n<p>i is less than 5<\/p>\n<p>i is less than 5<\/p>\n<p>i is less than 10<\/p>\n<p>i is less than 10<\/p>\n<p>i is less than 10<\/p>\n<p>i is less than 10<\/p>\n<p>i is less than 10<\/p>\n<p>i is 10 or more<\/p>\n<p>i is 10 or more<\/p>\n<p>&nbsp;<\/p>\n<p>As you can see, execution falls through each <strong>case <\/strong>until a <strong>break <\/strong>statement or the end of the <strong>switch<\/strong> is reached. omitting the <strong>break <\/strong>statement has many practical applications in real programs. The example below uses a <strong>switch <\/strong>to provide a more efficient implementation.<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ An improved version of the season program.<\/p>\n<p>class Switch {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>int month = 4;<\/p>\n<p>String season;<\/p>\n<p>switch (month) {<\/p>\n<p>case 12:<\/p>\n<p>case 1:<\/p>\n<p>case 2:<\/p>\n<p>season = &#8220;Winter&#8221;;<\/p>\n<p>break;<\/p>\n<p>case 3:<\/p>\n<p>case 4:<\/p>\n<p>case 5:<\/p>\n<p>season = &#8220;Spring&#8221;;<\/p>\n<p>break;<\/p>\n<p>case 6:<\/p>\n<p>case 7:<\/p>\n<p>case 8:<\/p>\n<p>season = &#8220;Summer&#8221;;<\/p>\n<p>break;<\/p>\n<p>case 9:<\/p>\n<p>case 10:<\/p>\n<p>case 11:<\/p>\n<p>season = &#8220;Autumn&#8221;;<\/p>\n<p>break;<\/p>\n<p>default:<\/p>\n<p>season = &#8220;Bogus Month&#8221;;<\/p>\n<p>}<\/p>\n<p>System.out.println(&#8220;April is in the &#8221; + season + &#8220;.&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<h3><a name=\"_Toc179309057\"><\/a>Nested switch Statements<\/h3>\n<p>You can use a <strong>switch <\/strong>as part of the statement sequence of an outer <strong>switch<\/strong>. This is called a <em>nested <\/em><strong>switch<\/strong>. Since a <strong>switch <\/strong>statement defines its own block, no conflicts arise between the <strong>case <\/strong>constants in the inner <strong>switch <\/strong>and those in the outer <strong>switch<\/strong>. For example, the following fragment is perfectly valid:<\/p>\n<p>&nbsp;<\/p>\n<p>switch(count) {<\/p>\n<p>case 1:<\/p>\n<p>switch(target) { \/\/ nested switch<\/p>\n<p>case 0:<\/p>\n<p>System.out.println(&#8220;target is zero&#8221;);<\/p>\n<p>break;<\/p>\n<p>case 1: \/\/ no conflicts with outer switch<\/p>\n<p>System.out.println(&#8220;target is one&#8221;);<\/p>\n<p>break;<\/p>\n<p>}<\/p>\n<p>break;<\/p>\n<p>case 2: \/\/ &#8230;<\/p>\n<p>&nbsp;<\/p>\n<p>Here, the <strong>case 1: <\/strong>statement in the inner switch does not conflict with the <strong>case 1: <\/strong>statement in the outer switch. The <strong>count <\/strong>variable is only compared with the list of cases at the outer level. If <strong>count <\/strong>is 1, then <strong>target <\/strong>is compared with the inner list cases. In summary, there are three important features of the <strong>switch <\/strong>statement to note:<\/p>\n<ul>\n<li>The <strong>switch <\/strong>differs from the <strong>if <\/strong>in that <strong>switch <\/strong>can only test for equality, whereas <strong>if <\/strong>can evaluate any type of Boolean expression. That is, the <strong>switch <\/strong>looks only for a match between the value of the expression and one of its <strong>case <\/strong>constants.<\/li>\n<li>No two <strong>case <\/strong>constants in the same <strong>switch <\/strong>can have identical values. Of course, a <strong>switch <\/strong>statement enclosed by an outer <strong>switch <\/strong>can have <strong>case <\/strong>constants in common.<\/li>\n<li>A <strong>switch <\/strong>statement is usually more efficient than a set of nested <strong>if<\/strong>s. When it compiles a <strong>switch <\/strong>statement, the Java compiler will inspect each of the <strong>case <\/strong>constants and create a \u201cjump table\u201d that it will use for selecting the path of execution depending on the value of the expression. Therefore, if you need to select among a large group of values, a <strong>switch <\/strong>statement will run much faster than the equivalent logic coded using a sequence of <strong>if-else<\/strong>s.<\/li>\n<\/ul>\n<h2><strong>\u00a0<\/strong><\/h2>\n<h2><a name=\"_Toc179309058\"><\/a><strong>Iteration Statements<\/strong><\/h2>\n<p>Java\u2019s iteration statements are <strong>for<\/strong>, <strong>while<\/strong>, and <strong>do-while<\/strong>. These statements create what we commonly call <em>loops. <\/em>As you probably know, a loop repeatedly executes the same set of instructions until a termination condition is met.<\/p>\n<h3><a name=\"_Toc179309059\"><\/a>while<\/h3>\n<p>The <strong>while <\/strong>loop is Java\u2019s most fundamental looping statement. It repeats a statement or block while its controlling expression is true. Here is its general form:<\/p>\n<p>&nbsp;<\/p>\n<p>while(<em>condition<\/em>) {<\/p>\n<p>\/\/ body of loop<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>The <em>condition <\/em>can be any Boolean expression. The body of the loop will be executed as long as the conditional expression is true. When <em>condition <\/em>becomes false, control passes to the next line of code immediately following the loop. The curly braces are unnecessary if only a single statement is being repeated. Here is a <strong>while <\/strong>loop that counts down from 10, printing exactly ten lines of \u201ctick\u201d:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Demonstrate the while loop.<\/p>\n<p>class While {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>int n = 10;<\/p>\n<p>while(n &gt; 0) {<\/p>\n<p>System.out.println(&#8220;tick &#8221; + n);<\/p>\n<p>n&#8211;;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>When you run this program, it will \u201ctick\u201d ten times:<\/p>\n<p>tick 10<\/p>\n<p>tick 9<\/p>\n<p>tick 8<\/p>\n<p>tick 7<\/p>\n<p>tick 6<\/p>\n<p>tick 5<\/p>\n<p>tick 4<\/p>\n<p>tick 3<\/p>\n<p>tick 2<\/p>\n<p>tick 1<\/p>\n<p>&nbsp;<\/p>\n<p>Since the <strong>while <\/strong>loop evaluates its conditional expression at the top of the loop, the body of the loop will not execute even once if the condition is false to begin with. For example, in the following fragment, the call to <strong>println( ) <\/strong>is never executed:<\/p>\n<p>&nbsp;<\/p>\n<p>int a = 10, b = 20;<\/p>\n<p>while(a &gt; b)<\/p>\n<p>System.out.println(&#8220;This will not be displayed&#8221;);<\/p>\n<p>&nbsp;<\/p>\n<p>The body of the <strong>while <\/strong>can be empty. This is because a <em>null statement <\/em>one that consists only of a semicolon is syntactically valid in Java. For example, consider the following program:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ The target of a loop can be empty.<\/p>\n<p>class NoBody {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>int i, j;<\/p>\n<p>i = 100;<\/p>\n<p>j = 200;<\/p>\n<p>\/\/ find midpoint between i and j<\/p>\n<p>while(++i &lt; &#8211;j) ; \/\/ no body in this loop<\/p>\n<p>System.out.println(&#8220;Midpoint is &#8221; + i);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>This program finds the midpoint between <strong>i <\/strong>and <strong>j<\/strong>. It generates the following output:<\/p>\n<p>Midpoint is 150<\/p>\n<p>&nbsp;<\/p>\n<p>Here is how the <strong>while <\/strong>loop works. The value of <strong>i <\/strong>is incremented, and the value of <strong>j <\/strong>is decremented. These values are then compared with one another. If the new value of <strong>i <\/strong>is still less than the new value of <strong>j<\/strong>, then the loop repeats. If <strong>i <\/strong>is equal to or greater than <strong>j<\/strong>, the loop stops. Upon exit from the loop, <strong>i <\/strong>will hold a value that is midway between the original values of <strong>i <\/strong>and <strong>j<\/strong>.<\/p>\n<h3><a name=\"_Toc179309060\"><\/a>do-while<\/h3>\n<p>As you just saw, if the conditional expression controlling a <strong>while <\/strong>loop is initially false, then the body of the loop will not be executed at all. However, sometimes it is desirable to execute the body of a <strong>while <\/strong>loop at least once, even if the conditional expression is false to begin with. In other words, there are times when you would like to test the termination expression at the end of the loop rather than at the beginning. Java supplies a loop that does just that: the <strong>do-while<\/strong>. The <strong>do-while <\/strong>loop always executes its body at least once, because its conditional expression is at the bottom of the loop. Its general form is<\/p>\n<p>&nbsp;<\/p>\n<p>do {<\/p>\n<p>\/\/ body of loop<\/p>\n<p>} while (<em>condition<\/em>);<\/p>\n<p>&nbsp;<\/p>\n<p>Each iteration of the <strong>do-while <\/strong>loop first executes the body of the loop and then evaluates the conditional expression. If this expression is true, the loop will repeat. Otherwise, the loop terminates. As with all of Java\u2019s loops, <em>condition <\/em>must be a Boolean expression. Here is a reworked version of the \u201ctick\u201d program that demonstrates the <strong>do<\/strong>&#8211;<strong>while <\/strong>loop. It generates the same output as before.<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Demonstrate the do-while loop.<\/p>\n<p>class DoWhile {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>int n = 10;<\/p>\n<p>do {<\/p>\n<p>System.out.println(&#8220;tick &#8221; + n);<\/p>\n<p>n&#8211;;<\/p>\n<p>} while(n &gt; 0);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>The loop in the preceding program, while technically correct, can be written more efficiently as follows:<\/p>\n<p>do {<\/p>\n<p>System.out.println(&#8220;tick &#8221; + n);<\/p>\n<p>} while(&#8211;n &gt; 0);<\/p>\n<p>&nbsp;<\/p>\n<p>In this example, the expression <strong>(\u2013 \u2013n &gt; 0) <\/strong>combines the decrement of <strong>n <\/strong>and the test for zero into one expression. First, the <strong>\u2013 \u2013<\/strong>n statement executes, decrementing <strong>n <\/strong>and returning the new value of <strong>n<\/strong>. This value is then compared with zero. If it is greater than zero, the loop continues; otherwise it terminates. The <strong>do-while <\/strong>loop is especially useful when you process a menu selection, because you will usually want the body of a menu loop to execute at least once. Consider the following program which implements a very simple help system for Java\u2019s selection and iteration statements:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Using a do-while to process a menu selection<\/p>\n<p>class Menu {<\/p>\n<p>public static void main(String args[])<\/p>\n<p>throws java.io.IOException {<\/p>\n<p>char choice;<\/p>\n<p>do {<\/p>\n<p>System.out.println(&#8220;Help on:&#8221;);<\/p>\n<p>System.out.println(&#8221; 1. if&#8221;);<\/p>\n<p>System.out.println(&#8221; 2. switch&#8221;);<\/p>\n<p>System.out.println(&#8221; 3. while&#8221;);<\/p>\n<p>System.out.println(&#8221; 4. do-while&#8221;);<\/p>\n<p>System.out.println(&#8221; 5. for\\n&#8221;);<\/p>\n<p>System.out.println(&#8220;Choose one:&#8221;);<\/p>\n<p>choice = (char) System.in.read();<\/p>\n<p>} while( choice &lt; &#8216;1&#8217; || choice &gt; &#8216;5&#8217;);<\/p>\n<p>System.out.println(&#8220;\\n&#8221;);<\/p>\n<p>switch(choice) {<\/p>\n<p>case &#8216;1&#8217;:<\/p>\n<p>System.out.println(&#8220;The if:\\n&#8221;);<\/p>\n<p>System.out.println(&#8220;if(condition) statement;&#8221;);<\/p>\n<p>System.out.println(&#8220;else statement;&#8221;);<\/p>\n<p>break;<\/p>\n<p>case &#8216;2&#8217;:<\/p>\n<p>System.out.println(&#8220;The switch:\\n&#8221;);<\/p>\n<p>System.out.println(&#8220;switch(expression) {&#8220;);<\/p>\n<p>System.out.println(&#8221; case constant:&#8221;);<\/p>\n<p>System.out.println(&#8221; statement sequence&#8221;);<\/p>\n<p>System.out.println(&#8221; break;&#8221;);<\/p>\n<p>System.out.println(&#8221; \/\/ &#8230;&#8221;);<\/p>\n<p>System.out.println(&#8220;}&#8221;);<\/p>\n<p>break;<\/p>\n<p>case &#8216;3&#8217;:<\/p>\n<p>System.out.println(&#8220;The while:\\n&#8221;);<\/p>\n<p>System.out.println(&#8220;while(condition) statement;&#8221;);<\/p>\n<p>break;<\/p>\n<p>case &#8216;4&#8217;:<\/p>\n<p>System.out.println(&#8220;The do-while:\\n&#8221;);<\/p>\n<p>System.out.println(&#8220;do {&#8220;);<\/p>\n<p>System.out.println(&#8221; statement;&#8221;);<\/p>\n<p>System.out.println(&#8220;} while (condition);&#8221;);<\/p>\n<p>break;<\/p>\n<p>case &#8216;5&#8217;:<\/p>\n<p>System.out.println(&#8220;The for:\\n&#8221;);<\/p>\n<p>System.out.print(&#8220;for(init; condition; iteration)&#8221;);<\/p>\n<p>System.out.println(&#8221; statement;&#8221;);<\/p>\n<p>break;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>Here is a sample run produced by this program:<\/p>\n<p>Help on:<\/p>\n<ol>\n<li>if<\/li>\n<li>switch<\/li>\n<li>while<\/li>\n<li>do-while<\/li>\n<li>for<\/li>\n<\/ol>\n<p>Choose one:<\/p>\n<p>4<\/p>\n<p>The do-while:<\/p>\n<p>do {<\/p>\n<p>statement;<\/p>\n<p>} while (condition);<\/p>\n<p>&nbsp;<\/p>\n<p>In the program, the <strong>do-while <\/strong>loop is used to verify that the user has entered a valid choice. If not, then the user is reprompted. Since the menu must be displayed at least once, the <strong>do-while <\/strong>is the perfect loop to accomplish this. A few other points about this example: Notice that characters are read from the keyboard by calling <strong>System.in.read( )<\/strong>. This is one of Java\u2019s console input functions. It reads characters from standard input returned as integers, which is why the return value was cast to <strong>char<\/strong>. By default, standard input is line buffered, so you must press ENTER before any characters that you type will be sent to your program. One other point: Because <strong>System.in.read( ) <\/strong>is being used, the program must specify the <strong>throws java.io.IOException <\/strong>clause. This line is necessary to handle input errors.<\/p>\n<h3><a name=\"_Toc179309061\"><\/a>for<\/h3>\n<p>the <strong>for <\/strong>loop is a powerful and flexible loop. Here is the general form of the <strong>for <\/strong>statement:<\/p>\n<p>&nbsp;<\/p>\n<p>for(<em>initialization<\/em>; <em>condition<\/em>; <em>iteration<\/em>) {<\/p>\n<p>\/\/ body<\/p>\n<p>}<\/p>\n<p>If only one statement is being repeated, there is no need for the curly braces. The <strong>for <\/strong>loop operates as follows. When the loop first starts, the <em>initialization <\/em>portion of the loop is executed. Generally, this is an expression that sets the value of the <em>loop control variable, <\/em>which acts as a counter that controls the loop. It is important to understand that the initialization expression is only executed once. Next, <em>condition <\/em>is evaluated. This must be a Boolean expression. It usually tests the loop control variable against a target value. If this expression is true, then the body of the loop is executed. If it is false, the loop terminates. Next, the <em>iteration <\/em>portion of the loop is executed. This is usually an expression that increments or decrements the loop control variable. The loop then iterates, first evaluating the conditional expression, then executing the body of the loop, and then executing the iteration expression with each pass. This process repeats until the controlling expression is false. Here is a version of the \u201ctick\u201d program that uses a <strong>for <\/strong>loop:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Demonstrate the for loop.<\/p>\n<p>class ForTick {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>int n;<\/p>\n<p>for(n=10; n&gt;0; n&#8211;)<\/p>\n<p>System.out.println(&#8220;tick &#8221; + n);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Declaring Loop Control Variables Inside the for Loop<\/p>\n<p>Often the variable that controls a <strong>for <\/strong>loop is only needed for the purposes of the loop and is not used elsewhere. When this is the case, it is possible to declare the variable inside the initialization portion of the <strong>for<\/strong>. For example, here is the preceding program recoded so that the loop control variable <strong>n <\/strong>is declared as an <strong>int <\/strong>inside the <strong>for<\/strong>:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Declare a loop control variable inside the for.<\/p>\n<p>class ForTick {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>\/\/ here, n is declared inside of the for loop<\/p>\n<p>for(int n=10; n&gt;0; n&#8211;)<\/p>\n<p>System.out.println(&#8220;tick &#8221; + n);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>When you declare a variable inside a <strong>for <\/strong>loop, there is one important point to remember: the scope of that variable ends when the <strong>for <\/strong>statement does. That is, the scope of the variable is limited to the <strong>for <\/strong>loop. Outside the <strong>for <\/strong>loop, the variable will cease to exist. If you need to use the loop control variable elsewhere in your program, you will not be able to declare it inside the <strong>for <\/strong>loop. When the loop control variable will not be needed elsewhere, most Java programmers declare it inside the <strong>for<\/strong>. For example, here is a simple program that tests for prime numbers. Notice that the loop control variable, <strong>i<\/strong>, is declared inside the <strong>for <\/strong>since it is not needed elsewhere.<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Test for primes.<\/p>\n<p>class FindPrime {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>int num;<\/p>\n<p>boolean isPrime = true;<\/p>\n<p>num = 14;<\/p>\n<p>for(int i=2; i &lt;= num\/2; i++) {<\/p>\n<p>if((num % i) == 0) {<\/p>\n<p>isPrime = false;<\/p>\n<p>break;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>if(isPrime) System.out.println(&#8220;Prime&#8221;);<\/p>\n<p>else System.out.println(&#8220;Not Prime&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Using the Comma<\/strong><\/p>\n<p>There will be times when you will want to include more than one statement in the initialization and iteration portions of the <strong>for <\/strong>loop. For example, consider the loop in the following program:<\/p>\n<p>&nbsp;<\/p>\n<p>class Sample {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>int a, b;<\/p>\n<p>b = 4;<\/p>\n<p>for(a=1; a&lt;b; a++) {<\/p>\n<p>System.out.println(&#8220;a = &#8221; + a);<\/p>\n<p>System.out.println(&#8220;b = &#8221; + b);<\/p>\n<p>b&#8211;;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>As you can see, the loop is controlled by the interaction of two variables. Since the loop is governed by two variables, it would be useful if both could be included in the <strong>for <\/strong>statement, itself, instead of <strong>b <\/strong>being handled manually. To allow two or more variables to control a <strong>for <\/strong>loop, Java permits you to include multiple statements in both the initialization and iteration portions of the <strong>for<\/strong>. Each statement is separated from the next by a comma. Using the comma, the preceding <strong>for <\/strong>loop can be more efficiently coded as shown here:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Using the comma.<\/p>\n<p>class Comma {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>int a, b;<\/p>\n<p>for(a=1<strong>, <\/strong>b=4; a&lt;b; a++<strong>, <\/strong>b&#8211;) {<\/p>\n<p>System.out.println(&#8220;a = &#8221; + a);<\/p>\n<p>System.out.println(&#8220;b = &#8221; + b);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>In this example, the initialization portion sets the values of both <strong>a <\/strong>and <strong>b<\/strong>. The two comma-separated statements in the iteration portion are executed each time the loop repeats. The program generates the following output:<\/p>\n<p>a = 1<\/p>\n<p>b = 4<\/p>\n<p>a = 2<\/p>\n<p>b = 3<\/p>\n<h3><a name=\"_Toc179309062\"><\/a>Nested Loops<\/h3>\n<p>Like all other programming languages, Java allows loops to be nested. That is, one loop may be inside another. For example, here is a program that nests <strong>for <\/strong>loops:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Loops may be nested.<\/p>\n<p>class Nested {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>int i, j;<\/p>\n<p>for(i=0; i&lt;10; i++) {<\/p>\n<p>for(j=i; j&lt;10; j++)<\/p>\n<p>System.out.print(&#8220;.&#8221;);<\/p>\n<p>System.out.println();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>The output produced by this program is shown here:<\/p>\n<p><strong>&#8230;&#8230;&#8230;.<\/strong><\/p>\n<p><strong>&#8230;&#8230;&#8230;<\/strong><\/p>\n<p><strong>&#8230;&#8230;..<\/strong><\/p>\n<p><strong>&#8230;&#8230;.<\/strong><\/p>\n<p><strong>&#8230;&#8230;<\/strong><\/p>\n<p><strong>&#8230;..<\/strong><\/p>\n<p><strong>&#8230;.<\/strong><\/p>\n<p><strong>&#8230;<\/strong><\/p>\n<p><strong>..<\/strong><\/p>\n<p><strong>.<\/strong><\/p>\n<p>&nbsp;<\/p>\n<h2><a name=\"_Toc179309063\"><\/a><strong>Jump Statements<\/strong><\/h2>\n<p>Java supports three jump statements: <strong>break<\/strong>, <strong>continue<\/strong>, and <strong>return<\/strong>. These statements transfer control to another part of your program.<\/p>\n<h3><a name=\"_Toc179309064\"><\/a>Using break<\/h3>\n<p>In Java, the <strong>break <\/strong>statement has three uses. First, as you have seen, it terminates a statement sequence in a <strong>switch <\/strong>statement. Second, it can be used to exit a loop. Third, it can be used as a form of goto.<\/p>\n<h3><a name=\"_Toc179309065\"><\/a>Using break to Exit a Loop<\/h3>\n<p>By using <strong>break<\/strong>, you can force immediate termination of a loop, bypassing the conditional expression and any remaining code in the body of the loop. When a <strong>break <\/strong>statement is encountered inside a loop, the loop is terminated and program control resumes at the next statement following the loop. Here is a simple example:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Using break to exit a loop.<\/p>\n<p>class BreakLoop {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>for(int i=0; i&lt;100; i++) {<\/p>\n<p>if(i == 10) break; \/\/ terminate loop if i is 10<\/p>\n<p>System.out.println(&#8220;i: &#8221; + i);<\/p>\n<p>}<\/p>\n<p>System.out.println(&#8220;Loop complete.&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>This program generates the following output:<\/p>\n<p>i: 0<\/p>\n<p>i: 1<\/p>\n<p>i: 2<\/p>\n<p>i: 3<\/p>\n<p>i: 4<\/p>\n<p>i: 5<\/p>\n<p>i: 6<\/p>\n<p>i: 7<\/p>\n<p>i: 8<\/p>\n<p>i: 9<\/p>\n<p>Loop complete.<\/p>\n<p>&nbsp;<\/p>\n<p>As you can see, although the <strong>for <\/strong>loop is designed to run from 0 to 99, the <strong>break <\/strong>statement causes it to terminate early, when <strong>i <\/strong>equals 10. The <strong>break <\/strong>statement can be used with any of Java\u2019s loops. For example, here is the preceding program coded by use of a <strong>while <\/strong>loop. The output from this program is the same as just shown.<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Using break to exit a while loop.<\/p>\n<p>class BreakLoop2 {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>int i = 0;<\/p>\n<p>while(i &lt; 100) {<\/p>\n<p>if(i == 10) break; \/\/ terminate loop if i is 10<\/p>\n<p>System.out.println(&#8220;i: &#8221; + i);<\/p>\n<p>i++;<\/p>\n<p>}<\/p>\n<p>System.out.println(&#8220;Loop complete.&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>When used inside a set of nested loops, the <strong>break <\/strong>statement will only break out of the innermost loop. For example:<\/p>\n<p>\/\/ Using break with nested loops.<\/p>\n<p>class BreakLoop3 {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>for(int i=0; i&lt;3; i++) {<\/p>\n<p>System.out.print(&#8220;Pass &#8221; + i + &#8220;: &#8220;);<\/p>\n<p>for(int j=0; j&lt;100; j++) {<\/p>\n<p>if(j == 10) break; \/\/ terminate loop if j is 10<\/p>\n<p>System.out.print(j + &#8221; &#8220;);<\/p>\n<p>}<\/p>\n<p>System.out.println();<\/p>\n<p>}<\/p>\n<p>System.out.println(&#8220;Loops complete.&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>This program generates the following output:<\/p>\n<p>Pass 0: 0 1 2 3 4 5 6 7 8 9<\/p>\n<p>Pass 1: 0 1 2 3 4 5 6 7 8 9<\/p>\n<p>Pass 2: 0 1 2 3 4 5 6 7 8 9<\/p>\n<p>Loops complete.<\/p>\n<p>&nbsp;<\/p>\n<p>As you can see, the <strong>break <\/strong>statement in the inner loop only causes termination of that loop. The outer loop is unaffected. Here are two other points to remember about <strong>break<\/strong>. First, more than one <strong>break <\/strong>statement may appear in a loop. Second, the <strong>break <\/strong>that terminates a <strong>switch statement<\/strong> affects only that <strong>switch <\/strong>statement and not any enclosing loops. <em>\u00a0<\/em><\/p>\n<h3><a name=\"_Toc179309066\"><\/a>Using break as a Form of Goto<\/h3>\n<p>In addition to its uses with the <strong>switch <\/strong>statement and loops, the <strong>break <\/strong>statement can also be employed by itself to provide a form of the goto statement. Java does not have a goto statement, because it provides a way to branch in an unstructured manner. There are, however, a few places where the goto is a valuable construct for flow control. For example, the goto can be useful when you are exiting from a deeply nested set of loops. To handle such situations, Java defines an expanded form of the <strong>break <\/strong>statement. By using this form of <strong>break<\/strong>, you can break out of one or more blocks of code. These blocks need not be part of a loop or a <strong>switch<\/strong>. They can be any block. Further, you can specify precisely where execution will resume, because this form of <strong>break <\/strong>works with a label. As you will see, <strong>break <\/strong>gives you the benefits of a goto without its problems. The general form of the labeled <strong>break <\/strong>statement is shown here:<\/p>\n<p>&nbsp;<\/p>\n<p>break <em>label<\/em>;<\/p>\n<p>&nbsp;<\/p>\n<p>Here, <em>label <\/em>is the name of a label that identifies a block of code. When this form of <strong>break <\/strong>executes, control is transferred out of the named block of code. The labeled block of code must enclose the <strong>break <\/strong>statement, but it does not need to be the immediately enclosing block. This means that you can use a labeled <strong>break <\/strong>statement to exit from a set of nested blocks. But you cannot use <strong>break <\/strong>to transfer control to a block of code that does not enclose the <strong>break <\/strong>statement. To name a block, put a label at the start of it. A <em>label <\/em>is any valid Java identifier followed by a colon. Once you have labeled a block, you can then use this label as the target of a <strong>break <\/strong>statement. Doing so causes execution to resume at the <em>end <\/em>of the labeled block. For example, the following program shows three nested blocks, each with its own label. The <strong>break <\/strong>statement causes execution to jump forward, past the end of the block labeled <strong>second<\/strong>, skipping the two <strong>println( ) <\/strong>statements.<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Using break as a civilized form of goto.<\/p>\n<p>class Break {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>boolean t = true;<\/p>\n<p>first: {<\/p>\n<p>second: {<\/p>\n<p>third: {<\/p>\n<p>System.out.println(&#8220;Before the break.&#8221;);<\/p>\n<p>if(t) break second; \/\/ break out of second block<\/p>\n<p>System.out.println(&#8220;This won&#8217;t execute&#8221;);<\/p>\n<p>}<\/p>\n<p>System.out.println(&#8220;This won&#8217;t execute&#8221;);<\/p>\n<p>}<\/p>\n<p>System.out.println(&#8220;This is after second block.&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>Running this program generates the following output:<\/p>\n<p>Before the break.<\/p>\n<p>This is after second block.<\/p>\n<p>&nbsp;<\/p>\n<p>One of the most common uses for a labeled <strong>break <\/strong>statement is to exit from nested loops. For example, in the following program, the outer loop executes only once:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Using break to exit from nested loops<\/p>\n<p>class BreakLoop4 {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>outer: for(int i=0; i&lt;3; i++) {<\/p>\n<p>System.out.print(&#8220;Pass &#8221; + i + &#8220;: &#8220;);<\/p>\n<p>for(int j=0; j&lt;100; j++) {<\/p>\n<p>if(j == 10) break outer; \/\/ exit both loops<\/p>\n<p>System.out.print(j + &#8221; &#8220;);<\/p>\n<p>}<\/p>\n<p>System.out.println(&#8220;This will not print&#8221;);<\/p>\n<p>}<\/p>\n<p>System.out.println(&#8220;Loops complete.&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>This program generates the following output:<\/p>\n<p>Pass 0: 0 1 2 3 4 5 6 7 8 9 Loops complete.<\/p>\n<p>&nbsp;<\/p>\n<p>As you can see, when the inner loop breaks to the outer loop, both loops have been terminated. Keep in mind that you cannot break to any label which is not defined for an enclosing block. For example, the following program is invalid and will not compile:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ This program contains an error.<\/p>\n<p>class BreakErr {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>one: for(int i=0; i&lt;3; i++) {<\/p>\n<p>System.out.print(&#8220;Pass &#8221; + i + &#8220;: &#8220;);<\/p>\n<p>}<\/p>\n<p>for(int j=0; j&lt;100; j++) {<\/p>\n<p>if(j == 10) break one; \/\/ WRONG<\/p>\n<p>System.out.print(j + &#8221; &#8220;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Since the loop labeled <strong>one <\/strong>does not enclose the <strong>break <\/strong>statement, it is not possible to transfer control to that block.<\/p>\n<h3><a name=\"_Toc179309067\"><\/a>Using continue<\/h3>\n<p>Sometimes it is useful to force an early iteration of a loop. That is, you might want to continue running the loop, but stop processing the remainder of the code in its body for this particular iteration. The <strong>continue <\/strong>statement performs such an action. In <strong>while <\/strong>and <strong>do-while <\/strong>loops, a <strong>continue <\/strong>statement causes control to be transferred directly to the conditional expression that controls the loop. In a <strong>for <\/strong>loop, control goes first to the iteration portion of the <strong>for <\/strong>statement and then to the conditional expression. For all three loops, any intermediate code is bypassed. Here is an example program that uses <strong>continue <\/strong>to cause two numbers to be printed on each line:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Demonstrate continue.<\/p>\n<p>class Continue {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>for(int i=0; i&lt;10; i++) {<\/p>\n<p>System.out.print(i + &#8221; &#8220;);<\/p>\n<p>if (i%2 == 0) continue;<\/p>\n<p>System.out.println(&#8220;&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>This code uses the <strong>% <\/strong>operator to check if <strong>i <\/strong>is even. If it is, the loop continues without printing a newline. Here is the output from this program:<\/p>\n<p>0 1<\/p>\n<p>2 3<\/p>\n<p>4 5<\/p>\n<p>6 7<\/p>\n<p>8 9<\/p>\n<p>&nbsp;<\/p>\n<p>As with the <strong>break <\/strong>statement, <strong>continue <\/strong>may specify a label to describe which enclosing loop to continue. Here is an example program that uses <strong>continue <\/strong>to print a triangular multiplication table for 0 through 9.<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Using continue with a label.<\/p>\n<p>class ContinueLabel {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>outer: for (int i=0; i&lt;10; i++) {<\/p>\n<p>for(int j=0; j&lt;10; j++) {<\/p>\n<p>if(j &gt; i) {<\/p>\n<p>System.out.println();<\/p>\n<p>continue outer;<\/p>\n<p>}<\/p>\n<p>System.out.print(&#8221; &#8221; + (i * j));<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>System.out.println();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>The <strong>continue <\/strong>statement in this example terminates the loop counting <strong>j <\/strong>and continues with the next iteration of the loop counting <strong>i<\/strong>. Here is the output of this program:<\/p>\n<p>0<\/p>\n<p>0 1<\/p>\n<p>0 2 4<\/p>\n<p>0 3 6 9<\/p>\n<p>0 4 8 12 16<\/p>\n<p>0 5 10 15 20 25<\/p>\n<p>0 6 12 18 24 30 36<\/p>\n<p>0 7 14 21 28 35 42 49<\/p>\n<p>0 8 16 24 32 40 48 56 64<\/p>\n<p>0 9 18 27 36 45 54 63 72 81<\/p>\n<h3><a name=\"_Toc179309068\"><\/a>return<\/h3>\n<p>The last control statement is <strong>return<\/strong>. The <strong>return <\/strong>statement is used to explicitly return from a method. That is, it causes program control to transfer back to the caller of the method. As such, it is categorized as a jump statement. At any time in a method the <strong>return <\/strong>statement can be used to cause execution to branch back to the caller of the method. Thus, the <strong>return <\/strong>statement immediately terminates the method in which it is executed. The following example illustrates this point. Here, <strong>return <\/strong>causes execution to return to the Java run-time system, since it is the run-time system that calls <strong>main( )<\/strong>.<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Demonstrate return.<\/p>\n<p>class Return {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>boolean t = true;<\/p>\n<p>System.out.println(&#8220;Before the return.&#8221;);<\/p>\n<p>if(t) return; \/\/ return to caller<\/p>\n<p>System.out.println(&#8220;This won&#8217;t execute.&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>The output from this program is shown here:<\/p>\n<p>Before the return.<\/p>\n<p>&nbsp;<\/p>\n<p>As you can see, the final <strong>println( ) <\/strong>statement is not executed. As soon as <strong>return <\/strong>is executed, control passes back to the caller. the <strong>if <\/strong>statement is used here to trick the compiler for the sake of this demonstration.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Control Statements &nbsp; A programming language uses control statements to cause the flow of execution to advance and branch based on changes to the state of a program. Java\u2019s program&hellip; <\/p>\n","protected":false},"author":1,"featured_media":3469,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[83],"tags":[],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/posts\/3443"}],"collection":[{"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/comments?post=3443"}],"version-history":[{"count":2,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/posts\/3443\/revisions"}],"predecessor-version":[{"id":3482,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/posts\/3443\/revisions\/3482"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/media\/3469"}],"wp:attachment":[{"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/media?parent=3443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/categories?post=3443"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/tags?post=3443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}