This question already has answers here:
Is short-circuiting logical operators mandated? And evaluation order?
(7 answers)
Closed 6 years ago.
How is the value of b unchanged?
#include <iostream>
int main()
{
int a = 5, b = 10;
if (++a || ++b)
std::cout << a << b;
system("PAUSE");
return 0;
}
The output is 610. But how?
here's how the 'if' statement works:
if(condition1 || condition2 || condition 3){
//do this
}
now if condition1 is true (which in your code, it is since a!=0), the execution straightaway moves inside the block without checking 2 and 3.
If you wish to increment b as well, try && in place of ||
Related
This question already has answers here:
Value of C define changes unexpectedly
(7 answers)
C++ int with preceding 0 changes entire value
(4 answers)
Closed 1 year ago.
Here I am trying to convert h = 010 to h = 001 (via right shift operator) and then using the 'and' operator with 1, I wanna print yes, but I don't know why it is printing NO.
int h = 010;
h = h>>1;
if(h & 1){
cout<< "YES";
}
else{
cout << "NO";
}
This question already has answers here:
Is (4 > y > 1) a valid statement in C++? How do you evaluate it if so?
(5 answers)
Closed 5 years ago.
I'm working on a simple function to convert ascii codes to array indexes, and I'm have a bit of trouble with the else-if statement.
int getIndex(char character) {
int code = int(character);
int index = -1;
if (code == int(' ')){
index = 26;
} else if (int('a') <= code <= int('z')) {
index = code - int('a');
}
return index;
}
The if condition works fine and fires on spaces, but the else if statement fires in every other case, regardless of whether the character is in the range a to z or not. Is there a problem I can't see with the condition?
c++ doesn't support expressions with multiple comparisons (like, e.g., python does). You need to break the expression in the else if branch into two expression with the logical and (&&) operator between them:
if (code == int(' ')){
index = 26;
} else if (int('a') <= code && code <= int('z')) {
// Here -----------------^
index = code - int('a');
}
return index;
This question already has answers here:
C++ if else if not working properly [duplicate]
(5 answers)
Closed 5 years ago.
No matter what string I add to s[i], I I still get a "yes" as an output while if I remove ||(OR) it works perfectly.
for(int i=0; i<T; i++)
{
if(s[i]=="ccc"||"ccs")
{
cout<<"yes"<<endl;
}
else
{
cout<<"no"<<endl;
}
This is how it's written:
if(s[i] == "ccc" || s[i] == "ccs")
Change your if condition to:
if(s[i] == "ccc" || s[i] == "ccs")
This question already has answers here:
How to convert a single char into an int [duplicate]
(11 answers)
Closed 7 years ago.
This code Find the sum of all digits that occur in a string.
Example
sumUpNumbers("2 apples, 12 oranges") = 5 //2+1+2
Can anyone explain the need for use int('0') in this code!?
int sumUpDigits(std::string inputString) {
int answer = 0;
for (int i = 0; i < inputString.size(); i++) {
if ('1' <= inputString[i] && inputString[i] <= '9') {
answer += int(inputString[i]) - int('0');
}
}
return answer;
}
It converts char into ASCII code to make number out of string
int('9') - int('0') = 9
This question already has answers here:
In C++ what causes an assignment to evaluate as true or false when used in a control structure?
(6 answers)
Closed 9 years ago.
In this, the result is 6. But isn't i=5 considered a non-zero value? If I do i+=5 then it counts it as true. Why is this any different? (also no, I didn't mean to put i==5)
int i=7;
if(i=5) {
cout << ++i;
} else {
cout << --i;
}
The assignment operators like = and += return the value of the object after it's been assigned to. So, if you assign something false or 0, you can get false from the assignment operator.
i=5 evaluates to 5 and that's true in the eyes of if (). But i=0 would evaluate to 0 and that would be considered false by if ().
An assignment returns whatever was assigned. In your example:
int i = 7;
if (i = 5) { // returns 5, which is non-zero, or "true"
cout << ++i; // prints 6, or 5+1
} else {
cout << --i; // would print 4, or 5-1, if it was hit, which it never will
}
You may be confused by pre-increment vs post-increment. For example, consider the following:
int i = 7;
if (i = 5) { // returns 5, which is non-zero, or "true"
cout << i++; // prints 5, i is 6 after this line
} else {
cout << i--; // would print 5, but i is 4 after this line
}
Your code acts like this:
i = 7;
i = 5;
if ( 5 ) // it's true. Isn't it ?
{
i = i + 1; // now i is 6
cout << i;
}