i am a novice programmer and i want to learn through practicing. Code: #include <iostream> using namespace std; int main() { for(int i=0;i<=6;i++) { if((i<=3)&&(i==5)) continue; cout << i << "\t"; } } I created the above code and my required output of this code is 4 6. when i compile and run this code the output is 0 1 2 3 4 5 6 which is not my required output. I can get my required output through || operator. Code: #include <iostream> using namespace std; int main() { for(int i=0;i<=6;i++) { if((i<=3)||(i==5)) continue; cout << i << "\t"; } } In the above code the output is 4 6 which is my required output. why i can't get my required output through this conditional statement if((i<=3)&&(i==5)) ?
You need both the conditions to be true and it is never the case. When i is less than or equal to 3 it is not equal to 5 and when i is equal to 5 it is not less than or equal to 3.
Basically && operator is AND operator return true when both the conditions are true but || operator is OR operator return true when either 1st condition or 2nd condition is true. and in the given question for the output 4 6 you have to use || operator.