Example of while loop using logical operator

C Programming Language Loop control in C Language (Article) Loop control in C Language (Program)

29816

Program:

#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)

This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.