A multiple equality/inequalityconditions checking in if statements [duplicate] - c++

This question already has answers here:
How can I check whether multiple variables are equal to the same value?
(3 answers)
Closed 7 years ago.
I am a newbie to programming in C++ and I have a question regarding if conditions. We are currently learning C++ at school(Using TC, i know it's an old compiler,but yeah). I am currently making a tic-tac-toe program, an undefeatable one. Now, this is my problem.
I want to check the equality of 3 variables, and run the if body only if the 3 variables are not equal to another variable.
Why is this set of code not working?
if(a==b==c!=d)
{
}
Adding parentheses doesn't help, I'm probably doing it wrong.(Please excuse my ignorance)
if((a==b==c)!=d)
{
}
Thanks in advance!
-CaptainAwesome

You have to write each condition individually and combine them using && (logical and):
if(a==b && b==c && c!=d)
{
// ...
}

Because you made this up. You can't do boolean comparisons like this.
Stick to two operands at a time and use && and || to combine results.
I'm not entirely clear on your requirements but start with something like this:
if (a == b && b == c && c != d)

Related

How to approach .startswith() in C++? [duplicate]

This question already has answers here:
Check if one string is a prefix of another
(14 answers)
How do I check if a C++ std::string starts with a certain string, and convert a substring to an int?
(23 answers)
Closed 1 year ago.
I'm new to C++, learning from books and online video tutorials. I've been trying to get the following lines of code to work, without much success. Basically, trying to find a C++ equivalent to python'a .startswith() or "in".
In python, what I'm trying to achieve would look like this:
names = ["John","Mary","Joanne","David"]
for n in names:
if n.startswith("J"):
print(n)
In javascript, the syntax would be quite similar (there are better ways of doing this, but I'm trying to keep the lines of code close to python's):
const names = ["John","Mary","Joanne","David"];
for (let n of names) {
if (n.startsWith("J")) {
console.log(n);
}
So, I assumed things would work similarly in C++, right?
vector <string> names {"John","Mary","Joanne","David"};
for (auto n: names) {
if (n[0] == "J") {
cout << n << endl;
}
As I've just started learning about pointers, I might be getting confused between types / values / addresses, apologies for this.
What's the best way to solve this please? Alternatively, how should I find a way to mimic what "in" does in python, but for C++?
names = ["John","Mary","Joanne","David"]
for n in names:
if "J" in n:
print(n)
Thanks!

what is the specific usecase for !! in C++ [duplicate]

This question already has answers here:
Double Negation in C++
(14 answers)
Closed 2 years ago.
Came across using !! in C++ during condition check
if ( !! (flag != 0 )){..
}
This could be directly used like
if( flag != 0 ){..
}
Is there any specific corner use case in C/C++ or is it just a form of coding style ?
In this case, it's superfluous, but in general this style is used to convert the actual expression value to
an integer type in C.
a boolean type in C++.

The word 'and' at c++ [duplicate]

This question already has answers here:
When were the 'and' and 'or' alternative tokens introduced in C++?
(8 answers)
Closed 5 years ago.
Is the word and equivalent to the && operator?
if (inner > 10 and !id)
{
std::cout << "idle" << std::endl;
}
This code was originally translated from Python.
I was sure that the 'if' line would result in a compilation error. But it does pass.
Visual studio (2015) marks it as an error, but it does compile with g++ (and also on this site https://www.onlinegdb.com/online_c++_compiler) and seems to run as expected.
Is this correct syntax or did I miss something?
Yes, according to:
https://en.wikipedia.org/wiki/Digraphs_and_trigraphs#C++
and is equivalent to && ... another issue is that it is not really widely used and it is one character longer than && ... and we know that C++ programmers try to optimize everything. Even the length of their source code, so don't expect to find it widespread in production code.

C++ which if/else is faster [duplicate]

This question already has answers here:
Ternary operator ?: vs if...else
(14 answers)
Closed 5 years ago.
I have seen two types of if/else, which one is faster?
if(a==b) cout<<"a";
else cout<<"b";
OR
a==b ? cout<<"a" : cout<<"b";
The ternary conditional is an abuse since it's a mere coincidence that decltype(cout<<"a") is a type that can be used in a ternary conditional:
cout << (a == b ? "a" : "b");
would be more palatable, and possibly more tractable than the if, else, which you should otherwise prefer for its clarity.
And trust your compiler to make the optimisations. checking the output assembly if you have any suspicions.
The performance of either would never be catastrophic for your program.
It all comes down to Code readability.
The tertiary operator has its limitation of only one statement either true or false.

What does: "while( (stuff) ? false : (otherstuff)){}" mean? [duplicate]

This question already has answers here:
What does the question mark character ('?') mean in C++?
(8 answers)
Closed 6 years ago.
I have a c++ while loop I'm looking at:
while ((stuff) ? false : (otherstuff))
{
commands;
}
And I don't really understand what it's trying to do with the "? false :" part?
Can any one explain what this means please?
I already tried looking it up but I'm not really getting anything helpful.
It's using the ternary conditional operator to effectively perform the check:
while (!(stuff) && (otherstuff))
If stuff is true, then the first option on the ternary is evaluated (evaluating to false), if it's false, then it evaluates to otherstuff.
It's just a really bad way of writing this:
while (!stuff && otherstuff) {
}