{"id":3448,"date":"2024-05-27T12:12:30","date_gmt":"2024-05-27T12:12:30","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=3448"},"modified":"2024-05-27T12:12:31","modified_gmt":"2024-05-27T12:12:31","slug":"while-loop","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/05\/27\/while-loop\/","title":{"rendered":"While Loop"},"content":{"rendered":"\n<p>In C,\u00a0<strong>while<\/strong>\u00a0is one of the keywords with which we can form loops. The\u00a0<strong>while<\/strong>\u00a0loop is one of the most frequently used types of\u00a0loops in C. The other looping keywords in C are\u00a0<strong>for<\/strong>\u00a0and\u00a0<strong>do-while<\/strong>.<\/p>\n\n\n\n<p>The\u00a0<strong>while<\/strong>\u00a0loop is often called the\u00a0<strong>entry verified loop<\/strong>, whereas the\u00a0<strong>do-while<\/strong>\u00a0loop\u00a0is an\u00a0<strong>exit verified loop<\/strong>. The\u00a0<strong>for<\/strong>\u00a0loop, on the other hand, is an\u00a0<strong>automatic loop<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of C while Loop<\/h2>\n\n\n\n<p>The syntax of constructing a&nbsp;<strong>while<\/strong>&nbsp;loop is as follows \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while(expression){statement(s);}<\/code><\/pre>\n\n\n\n<p>The&nbsp;<strong>while<\/strong>&nbsp;keyword is followed by a parenthesis, in which there should be a Boolean expression. Followed by the parenthesis, there is a block of statements inside the curly brackets.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Flowchart of C while Loop<\/h2>\n\n\n\n<p>The following flowchart represents how the&nbsp;<strong>while<\/strong>&nbsp;loop works \u2212<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.tutorialspoint.com\/cprogramming\/images\/cpp_while_loop.jpg\" alt=\"while loop in C\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">How while Loop Works in C?<\/h2>\n\n\n\n<p>The\u00a0C compiler\u00a0evaluates the expression. If the expression is true, the code block that follows, will be executed. If the expression is false, the compiler ignores the block next to the\u00a0<strong>while<\/strong>\u00a0keyword, and proceeds to the immediately next statement after the block.<\/p>\n\n\n\n<p>Since the expression that controls the loop is tested before the program enters the loop, the&nbsp;<strong>while<\/strong>&nbsp;loop is called the&nbsp;<strong>entry verified loop<\/strong>. Here, the key point to note is that a&nbsp;<strong>while<\/strong>&nbsp;loop might not execute at all if the condition is found to be not true at the very first instance itself.<\/p>\n\n\n\n<p>The&nbsp;<strong>while<\/strong>&nbsp;keyword implies that the compiler continues to execute the ensuing block as long as the expression is true. The condition sits at the top of the looping construct. After each iteration, the condition is tested. If found to be true, the compiler performs the next iteration. As soon as the expression is found to be false, the loop body will be skipped and the first statement after the while loop will be executed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example of while Loop in C<\/h2>\n\n\n\n<p>The following program\u00a0prints the &#8220;Hello World&#8221;\u00a0message five times.<\/p>\n\n\n\n<pre id=\"0\" class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;intmain(){\/\/ local variable definitionint a =1;\/\/ while loop executionwhile(a &lt;=5){printf(\"Hello World \\n\");\n      a++;}printf(\"End of loop\");return0;}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Output<\/h3>\n\n\n\n<p>Here, the&nbsp;<strong>while<\/strong>&nbsp;loop acts as a counted loop. Run the code and check its output \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello World\nHello World\nHello World\nHello World\nHello World\nEnd of loop\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example Explanation<\/h3>\n\n\n\n<p>The variable &#8220;a&#8221; that controls the number of repetitions is initialized to 1, before the&nbsp;<strong>while<\/strong>&nbsp;statement. Since the condition &#8220;a &lt;= 5&#8221; is true, the program enters the loop, prints the message, increments &#8220;a&#8221; by 1, and goes back to the top of the loop.<\/p>\n\n\n\n<p>In the next iteration, &#8220;a&#8221; is 2, hence the condition is still true, hence the loop repeats again, and continues till the condition turns false. The loop stops repeating, and the program control goes to the step after the block.<\/p>\n\n\n\n<p>Now, change the initial value of &#8220;a&#8221; to 10 and run the code again. Now the output will show the following \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>End of loop\n<\/code><\/pre>\n\n\n\n<p>This is because the condition before the&nbsp;<strong>while<\/strong>&nbsp;keyword is false in the very first iteration itself, hence the block is not repeated.<\/p>\n\n\n\n<p>A &#8220;char&#8221; variable represents a character corresponding to its ASCII value. Hence, it can be incremented. Hence, we increment the value of the variable from &#8220;a&#8221; till it reaches &#8220;z&#8221;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using while as Conditional Loop<\/h2>\n\n\n\n<p>You can use a while loop as a conditional loop where the loop will be executed till the given condition is satisfied.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>In this example, the&nbsp;<strong>while<\/strong>&nbsp;loop is used as a&nbsp;<strong>conditional loop<\/strong>. The loop continues to repeat till the input received is non-negative.<\/p>\n\n\n\n<pre id=\"1\" class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;intmain(){\/\/ local variable definition char choice ='a';int x =0;\/\/ while loop executionwhile(x &gt;=0){(x %2==0)?printf(\"%d is Even \\n\", x):printf(\"%d is Odd \\n\", x);printf(\"\\n Enter a positive number: \");scanf(\"%d\",&amp;x);}printf(\"\\n End of loop\");return0;}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Output<\/h3>\n\n\n\n<p>Run the code and check its output \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0 is Even\n\nEnter a positive number: 12\n12 is Even\n\nEnter a positive number: 25\n25 is Odd\n\nEnter a positive number: -1\n\nEnd of loop\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">While Loop with break and continue<\/h2>\n\n\n\n<p>In all the examples above, the\u00a0<strong>while<\/strong>\u00a0loop is designed to repeat for a number of times, or till a certain condition is found. C has\u00a0break\u00a0and\u00a0continue\u00a0statements to control the loop. These keywords can be used inside the\u00a0<strong>while<\/strong>\u00a0loop.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>The&nbsp;<strong>break<\/strong>&nbsp;statement causes a loop to terminate \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while(expr){......if(condition)break;...}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>The&nbsp;<strong>continue<\/strong>&nbsp;statement makes a loop repeat from the beginning \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while(expr){......if(condition)continue;...}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">More Examples of C while Loop<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Example: Printing Lowercase Alphabets<\/h3>\n\n\n\n<p>The following program prints all the lowercase alphabets with the help of a&nbsp;<strong>while<\/strong>&nbsp;loop.<\/p>\n\n\n\n<pre id=\"2\" class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;intmain(){\/\/ local variable definitionchar a ='a';\/\/ while loop executionwhile(a &lt;='z'){printf(\"%c\", a);\n      a++;}printf(\"\\n End of loop\");return0;}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Output<\/h4>\n\n\n\n<p>Run the code and check its output \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>abcdefghijklmnopqrstuvwxyz\nEnd of loop\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example: Equate Two Variables<\/h3>\n\n\n\n<p>In the code given below, we have two\u00a0variables\u00a0&#8220;a&#8221; and &#8220;b&#8221; initialized to 10 and 0, respectively. Inside the loop, &#8220;b&#8221; is decremented and &#8220;a&#8221; is incremented on each iteration. The loop is designed to repeat till &#8220;a&#8221; and &#8220;b&#8221; are not equal. The loop ends when both reach 5.<\/p>\n\n\n\n<pre id=\"3\" class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;intmain(){\/\/ local variable definitionint a =10, b =0;\/\/ while loop executionwhile(a != b){\n      a--;\n      b++;printf(\"a: %d b: %d\\n\", a,b);}printf(\"\\n End of loop\");return0;}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Output<\/h4>\n\n\n\n<p>When you run this code, it will produce the following output \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a: 9 b: 1\na: 8 b: 2\na: 7 b: 3\na: 6 b: 4\na: 5 b: 5\n\nEnd of loop\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">while Vs. do while Loops<\/h2>\n\n\n\n<p>The&nbsp;<strong>do-while<\/strong>&nbsp;loop appears similar to the&nbsp;<strong>while<\/strong>&nbsp;loop in most cases, although there is a difference in its syntax. The&nbsp;<strong>do-while<\/strong>&nbsp;is called the&nbsp;<strong>exit verified loop<\/strong>. In some cases, their behaviour is different. Difference between&nbsp;<strong>while<\/strong>&nbsp;and&nbsp;<strong>do-while<\/strong>&nbsp;loop is explained in the&nbsp;<strong>do-while<\/strong>&nbsp;chapter of this tutorial.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In C,\u00a0while\u00a0is one of the keywords with which we can form loops. The\u00a0while\u00a0loop is one of the most frequently used types of\u00a0loops in C. The other looping keywords in C are\u00a0for\u00a0and\u00a0do-while. The\u00a0while\u00a0loop is often called the\u00a0entry verified loop, whereas the\u00a0do-while\u00a0loop\u00a0is an\u00a0exit verified loop. The\u00a0for\u00a0loop, on the other hand, is an\u00a0automatic loop. Syntax of C while [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[248],"tags":[],"class_list":["post-3448","post","type-post","status-publish","format-standard","hentry","category-02-c"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/3448","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=3448"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/3448\/revisions"}],"predecessor-version":[{"id":3449,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/3448\/revisions\/3449"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=3448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=3448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=3448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}