And &&
while (x >= 6)
while(x>0 && x==5)
Truth table (true =1, false =0)
x && y = result
0 && 0 = 0 false
1 && 0 = 0 false
0 && 1 = 0 false
1 && 1 = 1 true
OR||
while ( x>0 || y == -5 )
Truth table (true =1, false =0)
x || y = 0 result
0 || 0 = 0 false
0 || 1 = 1 true
1 || 0 = 1 true
1 || 1 = 1 true
An example of using or
while (x>6 && y <3 || z ==10)
Below Is a program example that uses AND and OR
#include<stdio.h>
int main(){
int num1 = 120;
int num2 = -13;
int result1, result2, result3;
//test is num1 larger than 100, smaller than 500, and even.
// result 1 should be true (1)
result1 = ((num1 > 100) && (num1 < 500) && ((num1 % 2) == 0));
//test is num2 even or less than 0
result2 = (((num2 % 2)==0) || (num2 < 0));
// test is num2 (positive) or (odd and less than num1)
result3 = ( (num2>0) || (((num2 % 2)!=0) && (num2 < num1)));
printf("Is %d larger than 100 and smaller than 500 and even?\n", num1);
printf("result1: %d\n", result1);
printf("Is %d even or less than 0?\n", num2);
printf("result2: %d\n", result2);
printf("Is %d postive? Or, is it odd and less than %d?\n", num2, num1);
printf("result3: %d\n", result3);
return 0;
}
Output:
Is 120 larger than 100 and smaller than 500 and even?
result1: 1
Is -13 even or less than 0?
result2: 1
Is -13 postive? Or, is it odd and less than 120?
result3: 1