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

• Last Updated : 21 Jun, 2020

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

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

## C++

 `// C++ program to illustrate while(1)``#include ``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;``}`

Output:

```1 2 3 4 5
```

Program 2:

## C

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

## C++

 `// C++ program to illustrate while(2)``#include ``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;``}`

Output:

```1 2 3 4 5
```

My Personal Notes arrow_drop_up