{"id":335,"date":"2015-12-10T15:28:51","date_gmt":"2015-12-10T15:28:51","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?page_id=335"},"modified":"2016-02-25T06:24:14","modified_gmt":"2016-02-25T06:24:14","slug":"while-do-while-in-java","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/java\/while-do-while-in-java","title":{"rendered":"While And Do While In JAVA With Examples &#8211; Complete Tutorials"},"content":{"rendered":"<p>The While is a type of loop which evaluate the condition first. If condition evaluate to true the code inside the block{} will be executed and if it evaluate to false it will jump outside the while loop. The While in JAVA has two parts, first defining a Boolean expression inside parenthesis which will be tested and second is a statement or code which will be executed continually until Boolean expression evaluate to be true.<\/p>\n<hr \/>\n<h4><strong>Syntax Of While:<\/strong><\/h4>\n<p>The syntax of While in JAVA is:<\/p>\n<pre>initialize-value;\r\nwhile(Boolean-Expression){\r\nstatement-or-code; \/\/This will be executed continually until Boolean expression evaluate to true\r\nincrement-decrement-value;\r\n}<\/pre>\n<p><strong>initialize-value &#8211;<\/strong> First initialize the value first before starting while loop. <strong>Boolean-Expression &#8211;<\/strong> Boolean expression will be evaluated. If it comes out to be true then code inside it will executed otherwise compiler jumps outside the loop. <strong>statement-or-code &#8211;<\/strong> The code that you want to execute after Boolean expression evaluate to true. <strong>increment-decrement-value &#8211;<\/strong> increment or decrement the value of initialized variable<\/p>\n<hr \/>\n<h4><strong>Examples Of While In JAVA:<\/strong><\/h4>\n<p>Lets create a simple program to print 1 to 10 number using While loop:<\/p>\n<pre>public class WhileExample {\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\t\r\n\t\tint i=1;\r\n\t\twhile(i&lt;=10){\r\n\t\tSystem.out.println(i);\r\n\t\ti++;\r\n\t\t}\r\n\r\n\t}\r\n\r\n}<\/pre>\n<p>Output:<\/p>\n<pre>1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n<\/pre>\n<hr \/>\n<h4><strong>Do While In JAVA:<\/strong><\/h4>\n<p>The Do While loop will first execute the code inside do{} and then evaluate the while Boolean expression. The statements will continue to execute until Boolean expression evaluate to false. In do while, we first put statement or code that we want to execute inside the do block and then we put the Boolean expression inside While parenthesis just outside do block.<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span> The major difference between While and do While is that statement will be executed atleast once in do while.<\/p>\n<hr \/>\n<h4><strong>Examples Of Do While In JAVA:<\/strong><\/h4>\n<p>We can create the same program to print 1 to 10 number with do while in JAVA:<\/p>\n<pre>public class DoWhileExample {\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\t\r\n\t\tint i=1;\r\n\t\tdo{\r\n\t\tSystem.out.println(i);\r\n\t\ti++;\r\n\t\t} while(i&lt;=10);\r\n\r\n\t}\r\n\r\n}<\/pre>\n<p>Output:<\/p>\n<pre>1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The While is a type of loop which evaluate the condition first. If condition evaluate to true the code inside the block{} will be executed and if it evaluate to false it will jump outside the while loop. The While in JAVA has two parts, first defining a Boolean expression inside parenthesis which will be &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/while-do-while-in-java\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">While And Do While In JAVA With Examples &#8211; Complete Tutorials<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"footnotes":""},"class_list":["post-335","page","type-page","status-publish","hentry"],"psp_head":"<title>While And Do While In JAVA With Examples - Complete Tutorials \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Complete tutorial on While and Do While in JAVA with examples. The While is a type of loop which evaluate the condition first. If condition evaluate to true the code inside the block{} will be executed and if it evaluate to false it will jump outside the while loop.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/while-do-while-in-java\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/335","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/comments?post=335"}],"version-history":[{"count":0,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/335\/revisions"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}