For Versus While

For Versus While


/*Program 1 --> For loop*/
for (<init - stmnt>;<boolean - expr>;<incr - stmnt>) {
<body-statements>
}
 
/*Program 2 --> While loop*/
<init - stmnt>;
while (<boolean - expr>) {
<body-statements>
<incr-stmnt>
}

Solution :

When the body of a loop contains a “continue” statement, it can significantly affect the behavior of the loop. In your examples, “Program 1” and “Program 2” appear to have different outcomes due to the placement of the “continue” statement:

  • In “Program 1,” it seems that the “continue” statement is within a loop that executes three times. In this case, “loop” will be printed three times, and the loop will continue to execute.
  • In “Program 2,” it suggests that the “continue” statement is within a loop that lacks an appropriate condition to terminate. As a result, the “continue” statement might cause the loop to run indefinitely, resulting in an infinite loop.

The key takeaway is that the placement and conditions surrounding the “continue” statement within a loop are crucial in determining the loop’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.

Example: Using for Loop

Syntax:

 for(initialization ; condition ; increment/decrement)
    {  
        //statement 
    }     

#include <stdio.h>
 
int main()
{
    int  sum=0, i;
    for(i=1;i<=5;i++)
    {
        sum=sum+i;
    }
     
      printf("SUM = %d" , sum);
     
      return 0;
}

Output

SUM = 15

Example: Using While Loop

Syntax

while(condition)
    {  
        //code for execution
    }
// Example:
 
#include<stdio.h>
 
int main()
{
    int no=1, sum=0;
   
    while(no<=5)
    {
        sum=sum+no;
        no++;
    }
         
      printf("SUM = %d" , sum);
 
      return 0;
}

Output

SUM = 15

FAQ- For Versus While

Q1.Is there a difference between for and while?

Ans. For Loop: It’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.
While Loop: In contrast, the “while” loop is employed when you need to continue looping until a specific condition is met. It doesn’t rely on a predetermined number of iterations but rather keeps executing as long as the specified condition remains true.

Q2. Can for and while be used together?

Ans. In a “for” 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.
In a “while” loop, the condition is evaluated before each iteration. If it’s initially false, the loop doesn’t run. If true, it runs, and the condition is checked before each subsequent iteration.
Both loops can use “return” statements to exit the function and terminate the loop, passing control to the calling code.

Q3. Which is better to use while or for?

Ans.When you have a definite number of iterations in mind, it’s best to use a “for” loop.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment