{"id":4767,"date":"2023-03-20T16:25:49","date_gmt":"2023-03-20T16:25:49","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=4767"},"modified":"2023-03-22T14:24:51","modified_gmt":"2023-03-22T14:24:51","slug":"conditional-statements","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/conditional-statements\/","title":{"rendered":"While Loop"},"content":{"rendered":"<p><strong>While Statement<\/strong><\/p>\n<ol>\n<li>Initialization<\/li>\n<li>Condition<\/li>\n<li>Task<\/li>\n<li>Updation<\/li>\n<\/ol>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n.\r\n.\r\nint cnt = 1; \/\/Initialization\r\n\r\nwhile(cnt &lt; 5) \/\/Condition\r\n{\r\n SOP(&quot;Hello&quot;); \/\/Task\r\n cnt++; \/\/Updation\r\n}\r\n\r\n.\r\n.\r\n<\/pre>\n<p><em>While loop becomes infinite loop if the updating has no effect on while loop condition<\/em><\/p>\n<p><strong>Print the numbers divisible by 4 till N<\/strong><br \/>\n<strong>Approach 1<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.util.Scanner;\r\n\r\npublic class displayWhile {\r\n    public static void main(String&#x5B;] args) {\r\n        Scanner scn = new Scanner(System.in);\r\n        int count = scn.nextInt();\r\n        int cnt = 4;\r\n\r\n        while(cnt &lt; count){\r\n            if(cnt % 4 ==0)\r\n             System.out.println(cnt);\r\n\r\n            cnt++;\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Approach 2<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.util.Scanner;\r\n\r\npublic class displayWhile {\r\n    public static void main(String&#x5B;] args) {\r\n        Scanner scn = new Scanner(System.in);\r\n        int count = scn.nextInt();\r\n        int cnt = 4;\r\n\r\n        while(cnt &lt; count){\r\n            System.out.println(cnt + &quot; &quot;);\r\n            cnt+=4;\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Input<\/strong><\/p>\n<pre>\r\n40\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\n4 8 12 16 20 24 28 32 36 \r\n<\/pre>\n<p><strong>Approach2 is faster and efficient since count in while jumps in multiples of 4<\/strong><\/p>\n<p><strong>Printing perfect Square<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.util.Scanner;\r\n\r\npublic class PerfectSquare {\r\n    public static void main(String&#x5B;] args) {\r\n        Scanner scn = new Scanner(System.in);\r\n        int n = scn.nextInt();\r\n        int cnt = 1;\r\n\r\n        while(cnt*cnt &lt; n){\r\n            System.out.println(cnt*cnt + &quot; &quot;);\r\n            cnt++;\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Input<\/strong><\/p>\n<pre>\r\n30\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\n1 4 9 16 25\r\n<\/pre>\n<p><strong>Printing last character in string<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.util.Scanner;\r\n\r\npublic class PrintIntval {\r\n    public static void main(String&#x5B;] args) {\r\n        Scanner scn = new Scanner(System.in);\r\n        int number = scn.nextInt();\r\n        Character&#x5B;] arrChar = new Character&#x5B;]{};\r\n\r\n        String strCount = String.valueOf(number);\r\n        int index = strCount.length()-1;\r\n\r\n        while(index &gt;= 0 ) {\r\n            System.out.println(strCount.charAt(index));\r\n            index--;\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Input<\/strong><\/p>\n<pre>\r\n1365\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\n5\r\n6\r\n3\r\n1\r\n<\/pre>\n<p><strong>Approach 2<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.util.Scanner;\r\n\r\npublic class PrintIntval {\r\n    public static void main(String&#x5B;] args) {\r\n        Scanner scn = new Scanner(System.in);\r\n        int number = scn.nextInt();\r\n\r\n        while(number&gt; 0 ) {\r\n            System.out.println(number%10);\r\n            number = number\/10;\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Input<\/strong><\/p>\n<pre>\r\n1365\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\n5\r\n6\r\n3\r\n1\r\n<\/pre>\n<pre>\r\n-----------------------------------------------------\r\n n           n>0           n%10         n=n\/10   \r\n----------------------------------------------------\r\n1365          Y             5            136\r\n136           Y             6            13\r\n13            Y             3            1\r\n1             Y             1            0\r\n0             N - Loop Stops \r\n<\/pre>\n<p><strong>Now how will you address if input is 0 por negative and you should get output?<\/strong><\/p>\n<p><strong>To address above.. . <\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class PrintIntval {\r\n    public static void main(String&#x5B;] args) {\r\n        Scanner scn = new Scanner(System.in);\r\n        int number = scn.nextInt();\r\n\r\n        if(number &lt; 0)\r\n            number *= -1;\r\n\r\n        if(number == 0){\r\n            System.out.println(number);\r\n        }else {\r\n            while (number &gt; 0) {\r\n                System.out.println(number % 10);\r\n                number = number \/ 10;\r\n            }\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Input<\/strong><\/p>\n<pre>\r\n-1365\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\n5\r\n6\r\n3\r\n1\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>While Statement Initialization Condition Task Updation . . int cnt = 1; \/\/Initialization while(cnt &lt; 5) \/\/Condition { SOP(&quot;Hello&quot;); \/\/Task cnt++; \/\/Updation } . . While loop becomes infinite loop if the updating has no effect on while loop condition Print the numbers divisible by 4 till N Approach 1 import java.util.Scanner; public class displayWhile&hellip; <a href=\"https:\/\/codethataint.com\/blog\/conditional-statements\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[345],"tags":[],"class_list":["post-4767","post","type-post","status-publish","format-standard","hentry","category-conditional-statements"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4767","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=4767"}],"version-history":[{"count":6,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4767\/revisions"}],"predecessor-version":[{"id":4778,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4767\/revisions\/4778"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=4767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=4767"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=4767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}