{"id":2019,"date":"2024-05-10T07:06:38","date_gmt":"2024-05-10T07:06:38","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=2019"},"modified":"2024-05-10T07:06:38","modified_gmt":"2024-05-10T07:06:38","slug":"for-versus-while","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/for-versus-while\/","title":{"rendered":"For Versus While"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#for-versus-while\">For Versus While<\/a><\/li><li ><a href=\"#example-using-for-loop\">Example: Using for Loop<\/a><\/li><li ><a href=\"#example-using-while-loop\">Example: Using While Loop<\/a><\/li><li ><a href=\"#faq-for-versus-while\">FAQ- For Versus While<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"for-versus-while\">For Versus While<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/*Program 1 --&gt; For loop*\/\nfor (&lt;init - stmnt&gt;;&lt;boolean - expr&gt;;&lt;incr - stmnt&gt;) {\n&lt;body-statements&gt;\n}\n \n\/*Program 2 --&gt; While loop*\/\n&lt;init - stmnt&gt;;\nwhile (&lt;boolean - expr&gt;) {\n&lt;body-statements&gt;\n&lt;incr-stmnt&gt;\n}<\/code><\/pre>\n\n\n\n<p>Solution :<\/p>\n\n\n\n<p>When the body of a loop contains a &#8220;continue&#8221; statement, it can significantly affect the behavior of the loop. In your examples, &#8220;Program 1&#8221; and &#8220;Program 2&#8221; appear to have different outcomes due to the placement of the &#8220;continue&#8221; statement:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In &#8220;Program 1,&#8221; it seems that the &#8220;continue&#8221; statement is within a loop that executes three times. In this case, &#8220;loop&#8221; will be printed three times, and the loop will continue to execute.<\/li>\n\n\n\n<li>In &#8220;Program 2,&#8221; it suggests that the &#8220;continue&#8221; statement is within a loop that lacks an appropriate condition to terminate. As a result, the &#8220;continue&#8221; statement might cause the loop to run indefinitely, resulting in an infinite loop.<\/li>\n<\/ul>\n\n\n\n<p>The key takeaway is that the placement and conditions surrounding the &#8220;continue&#8221; statement within a loop are crucial in determining the loop&#8217;s behavior. It can either skip the remaining code in a specific iteration and continue to the next iteration or lead to unintended consequences like an infinite loop if not used carefully.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-using-for-loop\">Example: Using for Loop<\/h2>\n\n\n\n<p>Syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> for(initialization ; condition ; increment\/decrement)\n    {  \n        \/\/statement \n    }     <\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\n#include &lt;stdio.h&gt;\n \nint main()\n{\n    int  sum=0, i;\n    for(i=1;i&lt;=5;i++)\n    {\n        sum=sum+i;\n    }\n     \n      printf(\"SUM = %d\" , sum);\n     \n      return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SUM = 15\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-using-while-loop\">Example: Using While Loop<\/h2>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while(condition)\n    {  \n        \/\/code for execution\n    }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Example:\n \n#include&lt;stdio.h&gt;\n \nint main()\n{\n    int no=1, sum=0;\n   \n    while(no&lt;=5)\n    {\n        sum=sum+no;\n        no++;\n    }\n         \n      printf(\"SUM = %d\" , sum);\n \n      return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SUM = 15\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-for-versus-while\">FAQ- For Versus While<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1695211858657\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1.Is there a difference between for and while?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. <strong>For Loop:<\/strong> It&#8217;s used when you know the exact number of iterations required in advance. You specify the ending point in the loop initialization, making it suitable for situations where the number of iterations is known beforehand.<br \/><strong>While Loop:<\/strong> In contrast, the &#8220;while&#8221; loop is employed when you need to continue looping until a specific condition is met. It doesn&#8217;t rely on a predetermined number of iterations but rather keeps executing as long as the specified condition remains true.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1695211871807\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. Can for and while be used together?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In a &#8220;for&#8221; loop, the loop condition is checked before each iteration. If it becomes false, the loop ends, and control goes to the code after the loop.<br \/>In a &#8220;while&#8221; loop, the condition is evaluated before each iteration. If it&#8217;s initially false, the loop doesn&#8217;t run. If true, it runs, and the condition is checked before each subsequent iteration.<br \/>Both loops can use &#8220;return&#8221; statements to exit the function and terminate the loop, passing control to the calling code.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1695211902854\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. Which is better to use while or for?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans.When you have a definite number of iterations in mind, it&#8217;s best to use a &#8220;for&#8221; loop.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>For Versus While Solution : When the body of a loop contains a &#8220;continue&#8221; statement, it can significantly affect the behavior of the loop. In your examples, &#8220;Program 1&#8221; and &#8220;Program 2&#8221; appear to have different outcomes due to the placement of the &#8220;continue&#8221; statement: The key takeaway is that the placement and conditions surrounding &#8230; <a title=\"For Versus While\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/for-versus-while\/\" aria-label=\"More on For Versus While\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":2021,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[342],"class_list":["post-2019","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-for-versus-while","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2019","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=2019"}],"version-history":[{"count":7,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2019\/revisions"}],"predecessor-version":[{"id":10621,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2019\/revisions\/10621"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/2021"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=2019"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=2019"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=2019"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}