Home / Programs / Example of while loop using logical operator
Programming Example

Example of while loop using logical operator

👁 29,910 Views
💻 Practical Program
📘 Step by Step Learning
In this example, we are testing multiple conditions using logical operator inside while loop.

Program Code

#include <stdio.h>
int main()
{
   int i=1, j=1;
   while (i <= 4 || j <= 3)
   {
	printf("%d %d\n",i, j);
	i++;
	j++;
   }
   return 0;
}

Output

1 1
2 2
3 3
4 4
Press any key to continue . . .

Explanation

Just like relational operators (<, >, >=, <=, ! =, ==), we can also use logical operators in while loop. The following scenarios are valid :

 
while(num1<=10 && num2<=10)

-using AND(&&) operator, which means both the conditions should be true.

 
while(num1<=10||num2<=10)

– OR(||) operator, this loop will run until both conditions return false.

 
while(num1!=num2 &&num1 <=num2)

– Here we are using two logical operators NOT (!) and AND(&&).

 
while(num1!=10 ||num2>=num1)

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.