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

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) {
}

Related

How does the execution occurs in c++? [duplicate]

This question already has answers here:
How does C++ handle &&? (Short-circuit evaluation) [duplicate]
(7 answers)
Closed 6 months ago.
if(condition1 and condition2){
//body
}
If condition1 turns out to be false, will c++ compiler check for condition2 or will it directly return false?
What you described is called short-circuit evaluation and C++ does use it: if condition1 is false, condition2 will not be checked.

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

Explain what for (char c : str) does? [duplicate]

This question already has answers here:
'colon' and 'auto' in for loop c++? need some help understanding the syntax
(3 answers)
colon in for loop in C++
(1 answer)
Closed 3 years ago.
I am asking since I couldnt immediately find an answer on google, I know the answer is simple.
what does for (char c : str) {} do in a for loop?
Thank you!
It iterates the individual characters of str, copying each one to the (local) variable c for use in each iteration of the loop.

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.

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

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)