{"id":2124,"date":"2020-03-16T11:50:09","date_gmt":"2020-03-16T11:50:09","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=2124"},"modified":"2020-03-17T17:37:25","modified_gmt":"2020-03-17T17:37:25","slug":"control-statements-in-java","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/control-statements-in-java\/","title":{"rendered":"Control Statements in Java"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">You are already familiar with the term statement, which can simply be defined as an instruction given to the computer to perform specific operations. Control Statements in Java are controlling program flow and determining whether the other statements will be executed or not. <a href=\"https:\/\/www.h2kinfosys.com\/blog\/java-variables-and-data-types\/\">Java statement<\/a> is a complete unit of execution, terminated with a semicolon.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">But, imagine you want a statement or a section of code to be executed only under certain conditions that cannot be evaluated until runtime. Or you want a particular segment of code to repeat once for every item in some list.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">They can break up the flow of execution by using decision making, looping, and branching, allowing the application to selectively execute particular segments of code. Control flow statements can be applied to single expressions as well as a block of <a href=\"https:\/\/www.h2kinfosys.com\/courses\/java-online-training-course-details\">Java code<\/a>.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">As you already know, a block of code in Java is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><b>The if-then Statement.\u00a0<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Let&#8217;s suppose we only want to execute a block of code under certain circumstances. The if-then statement accomplishes this by allowing our application to execute a particular block of code if and only if during runtime a boolean expression evaluates to true:<\/span><\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"size-full wp-image-2125 aligncenter\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_8.png\" alt=\"\" width=\"567\" height=\"255\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_8.png 567w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_8-300x135.png 300w\" sizes=\"(max-width: 567px) 100vw, 567px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">For example, let&#8217;s print &#8220;Good Morning&#8221; if the hour of the day is less than 11:<\/span><\/p>\n<p style=\"padding-left: 60px;\"><i><span style=\"font-weight: 400;\">if (hourOfDay &lt; 11)<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">System.out.println(&#8220;Good Morning&#8221;);<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">As you can see from the example, we don&#8217;t need curly braces when there is just one statement. We can also, for example, increment some value, <\/span><i><span style=\"font-weight: 400;\">morningGreetingCount<\/span><\/i><span style=\"font-weight: 400;\">, every time the greeting is printed. We can easily add one more statement to the code block:<\/span><\/p>\n<p style=\"padding-left: 60px;\"><i><span style=\"font-weight: 400;\">if (hourOfDay &lt; 11) {<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">System.out.println(&#8220;Good Morning&#8221;);<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">morningGreetingCount++;<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">}<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">In this case, we added curly braces.<\/span><\/p>\n<p><b>The if-then-else Statement.<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Let&#8217;s imagine that we want to display a different message if it is 11 a.m. or later. We can use for these two if-then statements:<\/span><\/p>\n<p style=\"padding-left: 60px;\"><i><span style=\"font-weight: 400;\">if(hourOfDay &lt; 11) {<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">System.out.println(&#8220;Good Morning&#8221;);<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">}\u00a0<\/span><\/i><\/p>\n<p style=\"padding-left: 60px;\"><i><span style=\"font-weight: 400;\">if(hourOfDay &gt;= 11) {<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">System.out.println(&#8220;Good Afternoon&#8221;);<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">}\u00a0<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">We are performing an evaluation on <\/span><i><span style=\"font-weight: 400;\">hourOfDay <\/span><\/i><span style=\"font-weight: 400;\">twice, so this seems redundant. In some cases, the cost of the boolean expression we\u2019re evaluating could be computationally expensive. Fortunately, Java has a more useful approach in the form of an if-then-else statement. <\/span><\/p>\n<p><img decoding=\"async\" class=\"size-full wp-image-2126 aligncenter\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_9.png\" alt=\"\" width=\"577\" height=\"368\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_9.png 577w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_9-300x191.png 300w\" sizes=\"(max-width: 577px) 100vw, 577px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">Let&#8217;s change our code:<\/span><\/p>\n<p style=\"padding-left: 60px;\"><i><span style=\"font-weight: 400;\">if(hourOfDay &lt; 11) {<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">System.out.println(&#8220;Good Morning&#8221;);<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">} else {<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">System.out.println(&#8220;Good Afternoon&#8221;);<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">}<\/span><\/i><span style=\"font-weight: 400;\">\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Now the boolean evaluation happening only once and our code are truly branching between one of the two possible options. The <\/span><i><span style=\"font-weight: 400;\">else<\/span><\/i><span style=\"font-weight: 400;\"> operator takes a statement or block of statements, in the same way as the if statement does. We can append additional <\/span><i><span style=\"font-weight: 400;\">if-then <\/span><\/i><span style=\"font-weight: 400;\">statements to an <\/span><i><span style=\"font-weight: 400;\">else<\/span><\/i><span style=\"font-weight: 400;\"> block:<\/span><\/p>\n<p style=\"padding-left: 60px;\"><i><span style=\"font-weight: 400;\">if(hourOfDay &lt; 11) {<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">System.out.println(&#8220;Good Morning&#8221;);<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">} else if(hourOfDay &lt; 15) {<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">System.out.println(&#8220;Good Afternoon&#8221;);<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">} else {<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">System.out.println(&#8220;Good Evening&#8221;);<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">}<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">The execution will continue until it finds an if-then statement that evaluates to <\/span><i><span style=\"font-weight: 400;\">true<\/span><\/i><span style=\"font-weight: 400;\">. If neither of the first two expressions is <\/span><i><span style=\"font-weight: 400;\">true<\/span><\/i><span style=\"font-weight: 400;\">, it will execute the else block. Important thing is that in the creation of complex <\/span><i><span style=\"font-weight: 400;\">if-then-else<\/span><\/i><span style=\"font-weight: 400;\"> statements is that order is important. In some cases, we can get the unreachable code.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Ternary Operator. Java language has only one conditional ternary operator in Java. Sometimes better to replace <\/span><i><span style=\"font-weight: 400;\">if-then-else <\/span><\/i><span style=\"font-weight: 400;\">statement with it:<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">&lt;condition&gt; ?\u00a0 &lt;expression1 &gt;: &lt;expression2&gt;<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">The condition is an operand must be a boolean expression, expression1 and expression2 can be any expression that returns a value.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Let&#8217;s look at two snippets of code that are equal:<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">if-then-else <\/span><\/i><span style=\"font-weight: 400;\">statement:<\/span><\/p>\n<p style=\"padding-left: 60px;\"><i><span style=\"font-weight: 400;\">int y = 10;<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">final int x;<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">if(y &gt; 5) {<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">x = 2 * y;<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">} else {<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">x = 3 * y;<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">}\u00a0<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">Ternary operator:<\/span><\/p>\n<p style=\"padding-left: 60px;\"><i><span style=\"font-weight: 400;\">int y = 10;<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">int x = (y &gt; 5) ? (2 * y) : (3 * y);<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">For better readability, it is better to add parentheses around the expressions in ternary operations, but it is certainly not required.<\/span><\/p>\n<p><b>The switch Statement<\/b><\/p>\n<p><span style=\"font-weight: 400;\">A <\/span><i><span style=\"font-weight: 400;\">switch<\/span><\/i><span style=\"font-weight: 400;\"> statement is a complex decision-making operator that evaluates a single value and flow is redirected to the first matching case statement. If no such case statement is found that matches the value, an optional default statement (if there is one) will be called. Here is syntax of switch-case:<\/span><\/p>\n<p><img decoding=\"async\" class=\"size-full wp-image-2127 aligncenter\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_10.png\" alt=\"\" width=\"426\" height=\"361\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_10.png 426w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_10-300x254.png 300w\" sizes=\"(max-width: 426px) 100vw, 426px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">The value for a <\/span><i><span style=\"font-weight: 400;\">case<\/span><\/i><span style=\"font-weight: 400;\"> must have the same data type as the variable in the <\/span><i><span style=\"font-weight: 400;\">switch<\/span><\/i><span style=\"font-weight: 400;\">. The <\/span><i><span style=\"font-weight: 400;\">case<\/span><\/i><span style=\"font-weight: 400;\"> values must be unique, duplications are not allowed.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The <\/span><i><span style=\"font-weight: 400;\">break<\/span><\/i><span style=\"font-weight: 400;\"> statement is optional and it is used inside the switch to terminate a statement sequence. In another case, execution will continue on into the next <\/span><i><span style=\"font-weight: 400;\">case<\/span><\/i><span style=\"font-weight: 400;\">. The <\/span><i><span style=\"font-weight: 400;\">default<\/span><\/i><span style=\"font-weight: 400;\"> can appear anywhere inside the <\/span><i><span style=\"font-weight: 400;\">switch<\/span><\/i><span style=\"font-weight: 400;\"> block and it is optional.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">From JDK7 we can use a string literal\/constant in <\/span><i><span style=\"font-weight: 400;\">switch<\/span><\/i><span style=\"font-weight: 400;\"> statement. The switch statement on Strings can be more expensive than on primitive data types. Better to use switch for strings only in cases controlling data is already in string form. You need to ensure that the expression in any switch statement is not null to prevent a NullPointerException.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The switch statement does a case sensitive comparison. Let&#8217;s look at example:<\/span><\/p>\n<p style=\"padding-left: 60px;\"><i><span style=\"font-weight: 400;\">String animal = &#8220;dog&#8221;;<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">switch (animal) {<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0case &#8220;dog&#8221;:<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(&#8220;Dog is barking&#8221;);<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0case &#8220;cat&#8221;:<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(&#8220;Cat is fluffy&#8221;);<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0case &#8220;bear&#8221;:<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(&#8220;Bear loves honey&#8221;);<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0case &#8220;mouse&#8221;:<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(&#8220;Mouse is small&#8221;);<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0case &#8220;elephant&#8221;:<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(&#8220;Elephant is hude&#8221;);<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0default:<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(&#8220;I don&#8217;t know this animal&#8221;);<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">}<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">The current output will be <\/span><i><span style=\"font-weight: 400;\">&#8220;Dog is barking&#8221;.<\/span><\/i><span style=\"font-weight: 400;\"> If animal variable to <\/span><i><span style=\"font-weight: 400;\">\u201cbear\u201d <\/span><\/i><span style=\"font-weight: 400;\">the output will be<\/span><i><span style=\"font-weight: 400;\"> \u201cBear loves honey\u201d. <\/span><\/i><span style=\"font-weight: 400;\">If animal equals <\/span><i><span style=\"font-weight: 400;\">\u201clion\u201d t<\/span><\/i><span style=\"font-weight: 400;\">he default block will be executed and our output will be<\/span><i><span style=\"font-weight: 400;\"> &#8220;I don&#8217;t know this animal&#8221;.<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">What if we miss all break operators? Answer is easy. All Strings will be printed.<\/span><\/p>\n<p><b><br \/>\nThe while Statement<br \/>\n<\/b><span style=\"font-weight: 400;\">The <\/span><i><span style=\"font-weight: 400;\">while<\/span><\/i><span style=\"font-weight: 400;\"> statement is a\u00a0 repetition control structure, looping statement. This Control Statements in Java executes a part of code (one or more statements) zero or multiple times until the certain termination condition is true. The condition is a boolean expression. Let&#8217;s look at the <\/span><i><span style=\"font-weight: 400;\">while<\/span><\/i><span style=\"font-weight: 400;\"> statement structure:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2128 aligncenter\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_11.png\" alt=\"\" width=\"383\" height=\"211\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_11.png 383w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_11-300x165.png 300w\" sizes=\"(max-width: 383px) 100vw, 383px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">The while loop has similarities with the if-then statement. Both are composed of a condition and a block of statements. The condition is evaluated before each iteration of the loop and exits if the evaluation returns false. The statement block may never be executed, because that while loop may terminate after its first evaluation of the boolean expression. Let&#8217;s create an example<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">int counter = 5;<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">while (counter &gt; 0) {<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">counter&#8211;;<br \/>\n<\/span><\/i><i>System.out.println(counter + \u201c;\u201d);<br \/>\n<\/i><i><span style=\"font-weight: 400;\">}\u00a0<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">This code will print numbers from 5 to 1, semicolon-separated.<br \/>\n<\/span><span style=\"font-weight: 400;\">Consider the following code:\u00a0<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">int x = 2;<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">int y = 5;<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">while(x &lt; 10)<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">y++;\u00a0<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">The loop will never end! The boolean expression will never be modified and always be true. As you see, we got an infinite loop. <\/span><\/p>\n<p><b>The do-while Statement<\/b><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Java language also allows for the creation of a do-while loop. This type of loop guarantees that the statement or block of statements will be executed at least once.\u00a0 The conditional expression in this type of loop is after the statements. Let&#8217;s look at code snippet:<\/span><\/p>\n<p style=\"padding-left: 60px;\"><i><span style=\"font-weight: 400;\">int x = 0;<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">do {<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">x++;<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">} while(false);<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">System.out.println(x);<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">The code statement will be executed even the conditional expression is <\/span><i><span style=\"font-weight: 400;\">false<\/span><\/i><span style=\"font-weight: 400;\">, and the output of this code will be \u201c1\u201d.<\/span><\/p>\n<p><b>The for Statement<\/b><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">The simple <\/span><i><span style=\"font-weight: 400;\">for<\/span><\/i><span style=\"font-weight: 400;\"> loop has the same sections as others that we already discussed, but there are also two new sections: an initialization block and an update statement:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2130 aligncenter\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_12.png\" alt=\"\" width=\"639\" height=\"225\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_12.png 639w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_12-300x106.png 300w\" sizes=\"(max-width: 639px) 100vw, 639px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">For this kind of loop, firstly, the initialization statement is executed, after we check boolean expression. If the condition is true continue, else exit the loop. The body executes and just after this the block of updating the statements is executed.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Here is an example that prints the numbers 0 to 9: <\/span><\/p>\n<p style=\"padding-left: 60px;\"><i><span style=\"font-weight: 400;\">for(int i = 0; i &lt; 10; i++) {<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">System.out.print(i + &#8221; &#8220;);<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">}\u00a0<\/span><\/i><\/p>\n<p style=\"padding-left: 60px;\"><span style=\"font-weight: 400;\">It is also possible to create an infinite loop with this kind of loop:<br \/>\n<\/span><i><span style=\"font-weight: 400;\">for( ; ; ) {<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\u00a0<\/span><\/i> <i><span style=\"font-weight: 400;\">System.out.println(&#8220;Hello World&#8221;);<br \/>\n}<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">We can add multiple expressions\/statements in initialization, condition, updating Control Statements in Java blocks<\/span><i><span style=\"font-weight: 400;\">.<\/span><\/i><\/p>\n<p><b>The for-each Statement<\/b><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i><span style=\"font-weight: 400;\">The <\/span><i><span style=\"font-weight: 400;\">for-each<\/span><\/i><span style=\"font-weight: 400;\"> loop specifically designed for iterating over arrays and Collection objects. Its declaration consists of an initialization section and an object to be iterated over. The structure of the <\/span><i><span style=\"font-weight: 400;\">for-each<\/span><\/i><span style=\"font-weight: 400;\"> loop can be found below:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2131 aligncenter\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_13.png\" alt=\"\" width=\"568\" height=\"316\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_13.png 568w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/03\/Screenshot_13-300x167.png 300w\" sizes=\"(max-width: 568px) 100vw, 568px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">An object to be iterated over has to be a built-in Java array or an object whose class implements java.lang.Iterable. On each loop iteration, the variable on the left-hand side of the statement is assigned a new value from the array or collection on the right-hand side of the statement. For example:<\/span><\/p>\n<p style=\"padding-left: 60px;\"><i><span style=\"font-weight: 400;\">List&lt;String&gt; listOfNumbers = List.of(&#8220;one&#8221;, &#8220;two&#8221;, &#8220;three&#8221;);<\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">for(String number: <\/span><\/i><i><span style=\"font-weight: 400;\">listOfNumbers<\/span><\/i><i><span style=\"font-weight: 400;\">) {<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">System.out.print(number + &#8221; &#8220;);<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">}<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">The output for this code will be \u201cone two three\u201d. This code snippet can be replaced with <\/span><i><span style=\"font-weight: 400;\">for<\/span><\/i><span style=\"font-weight: 400;\"> loop:<\/span><\/p>\n<p style=\"padding-left: 60px;\"><i><span style=\"font-weight: 400;\">List&lt;String&gt; listOfNumbers = List.of(&#8220;one&#8221;, &#8220;two&#8221;, &#8220;three&#8221;);<\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">for(int i=0; i &lt;\u00a0 <\/span><\/i><i><span style=\"font-weight: 400;\">listOfNumbers.size(); i++) {<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">System.out.print(<\/span><\/i><i><span style=\"font-weight: 400;\">listOfNumbers.get(i)<\/span><\/i><i><span style=\"font-weight: 400;\"> + &#8221; &#8220;);<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">}<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">As you can compare, the<\/span><i><span style=\"font-weight: 400;\"> for-each<\/span><\/i><span style=\"font-weight: 400;\"> loop looks more readable.<\/span><\/p>\n<p><b>Nested Loops.<\/b><span style=\"font-weight: 400;\"> Loops can contain other loops. The most common example of nested loop is the iteration over a two-dimensional array:<\/span><\/p>\n<p style=\"padding-left: 60px;\"><i><span style=\"font-weight: 400;\">int sum = 0;<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">int[][] complexArray = {{1,2,3},{4,5,6},{7,8,9}};<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">for(int[] simpleArray : complexArray) {<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0for(int i=0; i &lt; simpleArray.length; i++) {<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 sum += simpleArray[i];<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0}<br \/>\n<\/span><\/i><i><span style=\"font-weight: 400;\">\u00a0 System.out.println(sum);<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">As the output of this code, you will get printed the sum of all elements of the two-dimensional matrix. In this code\u00a0 <\/span><i><span style=\"font-weight: 400;\">for<\/span><\/i><span style=\"font-weight: 400;\"> loop and <\/span><i><span style=\"font-weight: 400;\">for-each<\/span><\/i><span style=\"font-weight: 400;\"> loop are intentionally mixed, it is not convenient. I just\u00a0 wanted to show that the same logic can be written in many ways.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You are already familiar with the term statement, which can simply be defined as an instruction given to the computer to perform specific operations. Control Statements in Java are controlling program flow and determining whether the other statements will be executed or not. Java statement is a complete unit of execution, terminated with a semicolon. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2168,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[407,415,416,417,411,412,418,413,414],"class_list":["post-2124","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorials","tag-control-statements","tag-do-while","tag-for","tag-for-each","tag-if-then","tag-if-then-else","tag-nested-loops","tag-switch","tag-while"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/2124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=2124"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/2124\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/2168"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=2124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=2124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=2124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}