{"id":2252,"date":"2016-06-06T08:09:31","date_gmt":"2016-06-06T08:09:31","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=2252"},"modified":"2021-03-20T16:11:27","modified_gmt":"2021-03-20T16:11:27","slug":"java-program-for-do-while-loop","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/java-program-for-do-while-loop\/","title":{"rendered":"Java program for do while loop"},"content":{"rendered":"<h3>Program Description<\/h3>\n<p>Write a java program for do while demo. In this page, we will write a simple java program to demonstrate the do-while-loop.<\/p>\n<h3>Syntax<\/h3>\n<p>General syntax of the do-while is shown below. The expression evaluation is done after the loop. So the do-while is executed at least once irrespective of the loop condition.<\/p>\n<p><strong>do<\/strong><\/p>\n<p><strong>{<\/strong><\/p>\n<p><strong>\/\/statements<\/strong><\/p>\n<p><strong>} while (Boolean_expression);<\/strong><\/p>\n<h3>Java Program<\/h3>\n<p>Lets write a simple program to display a menu to user and display it until user choose to quit the loop. We will make use of the do while loop in the program.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.util.Scanner;\r\n\/\/------------------------------------\r\n\/\/ DoWhileDemo.java\r\n\/\/ www.TestingDocs.com\r\n\/\/ Program to demo do while loop\r\n\/\/-------------------------------------\r\npublic class DoWhileDemo {\r\n  public static void main(String[] args) {\r\n    int choice; \/\/ to take choice input\r\n    Scanner keyboard= new Scanner(System.in);\r\n    do {\r\n      System.out.println(\"~~~~~~~~ Menu ~~~~~~~~\");\r\n      System.out.println(\"1. Option 1\");\r\n      System.out.println(\"2. Option 2\");\r\n      System.out.println(\"3. Quit\");\r\n      System.out.println(\"Enter choice =\");\r\n      choice=keyboard.nextInt();\r\n      if(choice==1) {\r\n        System.out.println(\"User entered 1\");\r\n      }\r\n      if(choice==2) {\r\n        System.out.println(\"User entered 2\");\r\n      }\r\n    }while(choice != 3);\r\n  }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3>Program Output<\/h3>\n<p>~~~~~~~~ Menu ~~~~~~~~<br \/>\n1. Option 1<br \/>\n2. Option 2<br \/>\n3. Quit<br \/>\nEnter choice =<br \/>\n1<br \/>\nUser entered 1<br \/>\n~~~~~~~~ Menu ~~~~~~~~<br \/>\n1. Option 1<br \/>\n2. Option 2<br \/>\n3. Quit<br \/>\nEnter choice =<br \/>\n2<br \/>\nUser entered 2<br \/>\n~~~~~~~~ Menu ~~~~~~~~<br \/>\n1. Option 1<br \/>\n2. Option 2<br \/>\n3. Quit<br \/>\nEnter choice =<br \/>\n3<\/p>\n<h3>Screenshot<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2293\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Do-while-Java-Program-Output.png\" alt=\"Do while loop\" width=\"1740\" height=\"1029\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Do-while-Java-Program-Output.png 1740w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Do-while-Java-Program-Output-300x177.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Do-while-Java-Program-Output-1024x606.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Do-while-Java-Program-Output-768x454.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Do-while-Java-Program-Output-1536x908.png 1536w\" sizes=\"auto, (max-width: 1740px) 100vw, 1740px\" \/><\/p>\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-2252\" data-postid=\"2252\" class=\"themify_builder_content themify_builder_content-2252 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>Program Description Write a java program for do while demo. In this page, we will write a simple java program to demonstrate the do-while-loop. Syntax General syntax of the do-while is shown below. The expression evaluation is done after the loop. So the do-while is executed at least once irrespective of the loop condition. do [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-2252","post","type-post","status-publish","format-standard","hentry","category-java-programs","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=2252"}],"version-history":[{"count":4,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2252\/revisions"}],"predecessor-version":[{"id":19643,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2252\/revisions\/19643"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=2252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=2252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=2252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}