logical operators

i am a novice programmer and i want to learn through practicing.

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

#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)) ?
Last edited on
if((i<=3)&&(i==5))
Can you explain what you are intending for this statement to do?

The problem is that a number cannot be both less than or equal to 3, and equal to 5 at the same time. The statement will always be false.

Most likely you meant || (OR, union) instead of && (AND, intersection).
Last edited on
When posting, please use code tags so that the code is readable!

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

int main() {
	for (int i = 0; i <= 6; i++) {
		if ((i <= 3) || (i == 5))
			continue;

		cout << i << "\t";
	}
}


Note that this could be written (more simply) as :

1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;

int main() {
	for (int i = 0; i <= 6; i++)
		if ((i > 3) && (i != 5))
			cout << i << "\t";
}

you may want to review boolean algebra concepts.
de Morgan’s Theorem ... if you can understand this, you will see why, look for a light-math simple english explain of it (because it seems unlikely you will be able to read boolean algebra notation heavy proofs etc at this time, or circuitry examples which are also notation heavy).

https://brilliant.org/wiki/de-morgans-laws/ isnt too gibberishy.

it has a lovely english example:

What is an equivalent statement to "The lawn needs mowed and the car needs washed, but I will not do both."?

The two propositions are "I will mow the lawn" and "I will wash the car." Simply change the statement to an "or" statement and negate each of these propositions:

"Either I will not mow the lawn or I will not wash the car."

Note that this statement leaves open the possibility that one of the chores is completed, and it is also possible that neither chores are completed.

Last edited on
what is the difference between union and intersection?
union of 2 sets are the elements from both. Intersection of 2 sets are the elements that are common in the sets.
there is overlap between boolean algebra (true false) and sets.
about halfway down is a table that relates the basic operations:
https://eng.libretexts.org/Bookshelves/Computer_Science/Book%3A_Foundations_of_Computation_(Critchlow_and_Eck)/02%3A_Sets_Functions_and_Relations/2.02%3A_The_Boolean_Algebra_of_Sets

not that A looking symbol (upside down V really) is AND and the V is OR, and U is union so upside down U is intersection.
Topic archived. No new replies allowed.