Which will be faster while(1) or while(2)?

In most Programming Languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The boolean condition is either true or false.

while(1) & while(2)

It is an infinite loop that will run till a break statement is issued explicitly. Neither while(1) nor while(2) but any integer which is non-zero will give a similar effect as while(1). Therefore, while(1), while(2) or while(-255), all will give infinite loop only until and unless we use a break statement.

while(1) or while(2) or while(any non-zero integer)
{ 
    // loop runs infinitely
}

But practically, it is not advisable to use while(1) or while(any non-zero integer) in real-world because it increases the CPU usage and also blocks the code i.e., one cannot come out from the while(1) until the program is closed manually. while(1) can be used at a place where condition needs to be true always.

Below is the code to illustrate while(1) and while(2) in C/C++:



Program 1:

C

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to illustrate while(1)
#include<stdio.h>
  
// Driver Code
int main()
{
    int i = 0;
    while (1) {
  
        printf("%d ", ++i);
        if (i == 5) {
  
            // Break to come out of loop
            break;
        }
    }
    return 0;
}

chevron_right


C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate while(1)
#include <iostream>
using namespace std;
  
// Driver Code
int main()
{
    int i = 0;
    while (1) {
  
        cout << ++i << " ";
        if (i == 5) {
  
            // Break to come out of loop
            break;
        }
    }
    return 0;
}

chevron_right


Output:

1 2 3 4 5

Program 2:

C

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to illustrate while(2)
#include<stdio.h>
  
// Driver Code
int main()
{
    int i = 0;
    while (2) {
  
        printf("%d ", ++i);
        if (i == 5) {
  
            // Break to come out of loop
            break;
        }
    }
    return 0;
}

chevron_right


C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate while(2)
#include <iostream>
using namespace std;
  
// Driver Code
int main()
{
    int i = 0;
    while (2) {
  
        cout << ++i << " ";
        if (i == 5) {
  
            // Break to come out of loop
            break;
        }
    }
    return 0;
}

chevron_right


Output:

1 2 3 4 5

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.