My if statements always return true [closed] - if-statement

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
if ((x != A) || (x != a )|| ( x != B) || (x != b) || ( x != C) || (x != c) || (x != D) || (x != d) || ( x != F) || (x != f))
{
System.out.println("Points: -1");
return true;
}
else
{
System.out.println("hi");
return false;
}
For some reason I can't get this to return anything else but true.

Take the first two conditions: (x != A) || (x != a ).
What your saying here is, return true if x is either not equal to A or to a.
Assuming x == A, the first condition will return false and the second true. Thus your whole if-condition will return true.
Likewise for x == a. Here the first condition yields true and so does the if-condition.
What you probably mean is to connect all those conditions using AND or && instead of OR (||).

if ((x != 'A') || (x != 'a' ) ....
^ missing quotes
But even then, x can either be 'A' or 'a' or neither. With the OR evalution, you will always end up with TRUE as x will always NOT be one of those other values you are testing.
Do (x != 'A') && (x != 'a' ) ...

yes it will be always true
lets just concider part of your expression ((x != A) || (x != a ) ...
if x=A, then first statement is false, but other one is true, and as you have disjunction so you will always get true.
to get false you need x=A and x=a, which might be bit impossible

Check out this Fiddle http://jsfiddle.net/CtCqe/2/
Using regex might be better, not sure if the input of the user is only a single character or not.
Html
<div id="result">Result</div>
<input type='text'/>
<div id="pressed"></div>
Jquery
$('input').on('change input', function(){
var x = $(this).val();
if((x == 'A') || (x == 'a' )|| ( x == 'B') || (x == 'b') || ( x == 'C') || (x == 'c') || (x == 'D') || (x == 'd') || ( x == 'F') || (x == 'f')){
$('#result').html('False');
$(this).val('');
}else{
$('#result').html('True');
$(this).val('');
}
$('#pressed').html(x);
});

Related

How to Rewrite "IF CONDITION " for ON for 3min and OFF for 10min until 4Hr (240min)

I want shorten the if condition. How can I simplify this?
Where step = 1 to 240 in min
I want 1st 3 min ON= 1, second 10 min OFF = 0, It will repeated until 248 min
I wrote "if condition" which is consist of many condition. How to rewrite the code with simple expression
#include "udf.h"
DEFINE_PROFILE(ON_3min_OFF_10min_4Hr,thread,position)
{
face_t f;
real step,hf_3min,hf_10min;
hf_3min = 1; /*ON */
hf_10min = 0; /*OFF */
step=N_TIME;
begin_f_loop(f,thread)
{
if ((step<=3) || ((step>13) && (step<=16)) || ((step>26) && (step<=29)) || ((step>39) && (step<=42)) || ((step>52) && (step<=55)) || ((step>65) && (step<=68)) || ((step>78) && (step<=81)) || ((step>91) && (step<=94)) || ((step>104) && (step<=107)) || ((step>117) && (step<=120)) || ((step>130) && (step<=133)) || ((step>143) && (step<=146)) || ((step>156) && (step<=159)) || ((step>169) && (step<=172)) || ((step>182) && (step<=185)) || ((step>195) && (step<=198)) || ((step>208) && (step<=211)) || ((step>222) && (step<=225)) || ((step>235) && (step<=238)))
{
F_PROFILE(f,thread,position)=hf_3min;
}
else
{
F_PROFILE(f,thread,position)=hf_10min;
}
}
end_f_loop(f,thread)
}
From the bounds that you are checking, it seems your code is equivalent to:
if ((step - 1) % 13 < 3) {
F_PROFILE(f, thread, position) = hf;
} else {
F_PROFILE(f, thread, position) = hf2;
}
You need to be careful about the missing lower bound on the first condition, and the change of the bounds at the last 2 conditions, in case that's intentional.

Logical OR precedence

I have tried writing a loop that would refrain the user to enter a wrong kind of data (actually a boolean) into the program by using the || operator.
int Entrer()
{
int A;
do
{
cout<<"Entrez 0 ou 1."<<endl;
cin >> A;
}
while (A != (1 || 0));
return A;
}
Can somebody tell me why the program only accepts 1 and no 0 ?
do { ... } while (A != (1 || 0));
It should be while (A != 1 && A != 0);
Otherwise, A != (1 || 0) stands for A != 1 since (1 || 0) is evaluated before !=.
If you want to accept 1 and 0, you need to write the conditional as while(A != 1 && A != 0);. As your conditional written, it will evaluate the (1 || 0) first, and, as 1 is true and 0 is false, will evaluate to A != 1.

Logic Error in TicTacToe game

here's a simple logic error I can't quite wrap my head around:
Why does the following statement always equate to true?
if ( (grid[i][0] && grid[i][1] && grid[i][2]) == ('X' || 'x') ) return true;
It works flawlessly for
if ( (grid[i][0] && grid[i][1] && grid[i][2]) == ('X') return true;
Do it like this:
create a function to check a character of being x:
bool isX(char c)
{
return c == 'X' || c == 'x';
}
and the you can write:
if ( isX(grid[i][0]) && isX(grid[i][1]) && isX(grid[i][2]))
return true;
That's because the expressions
'X' || 'x'
and
grid[i][0] && grid[i][1] && grid[i][2]
use the || and && operators between non-zero integer-typed values (because char is an integer type) and so they both evaluate to true.
To translate into C++ (or almost any other somewhat similar language, for that matter) that you want characters x, y and z to be equal to either of the characters c and C, you must compute
(x == c || x == C) && (y == c || y == C) && (z == c || z == C)
so apply that to your problem.
More importantly though, learn about boolean operators and programming in general before you try to tackle C++.
In the first case, it returns true because none of the grid elements has the value of zero. The && operator produces 0 or 1, depending on the values that you pass.
In the second case, it does not work as expected either: you wouldn't get an 'X' by &&-ing values together.
The proper way of checking if three items are equal to 'X' or not would be as follows:
if (toupper(grid[i][0]) == 'X'
&& toupper(grid[i][1]) == 'X'
&& toupper(grid[i][2]) == 'X') {
return true;
}
To generalize the concept of "win" in TiCTacToe, write a function that returns true when a sequence of three items with a specific step in each direction holds a sequence of a given character, like this:
bool isWin(int r, int c, int dr, int dc, char ch) {
return toupper(grid[r+0*dr][c+0*dc] == ch
&& toupper(grid[r+1*dr][c+1*dc] == ch
&& toupper(grid[r+2*dr][c+2*dc] == ch;
}
bool isWin(char ch) {
return isWin(0,0,0,1,ch)
|| isWin(0,0,1,0,ch)
|| isWin(1,0,0,1,ch)
|| isWin(0,1,1,0,ch)
|| isWin(2,0,0,1,ch)
|| isWin(0,2,1,0,ch)
|| isWin(0,0,1,1,ch)
|| isWin(2,0,-1,1,ch);
}

Conditional not working, returns first case always

The result prints out 'c' 3 times, anyone know why it always meets the first condition?
#include <iostream>
using namespace std;
char x(char y)
{
if (y == 'a' || 'b')
{
return 'c';
}
else if (y == 'c' || 'd')
{
return 'e';
}
else
{
return 'g';
}
}
int main()
{
cout << x('a') << endl;
cout << x('c') << endl;
cout << x('p') << endl;
return 0;
}
You need something of the form
if (y == 'a' || y == 'b')
This is because in this expression
(y == 'a' || 'b')
you are evaluating an OR of y == 'a' and 'b', and since 'b' evaluates to true by virtue of being non-zero, the whole expression evaluates to true.
(y == 'a' || true)
This line:
if (y == 'a' || 'b')
is equivalent to:
if ((y == 'a') || ('b'))
That's because the == operator has higher precedence than the || operator.
Since 'b' is non-zero, it always evaluates as true, and so (y == 'a' || 'b') always evaluates as true.
You need to write this:
if (y == 'a' || y == 'b')
Of course, even if the precedence was the other way around,
if (y == ('a' || 'b'))
would not have been what you intended either!
Please check the operator precedence (priority) here: http://en.cppreference.com/w/cpp/language/operator_precedence
In your case the condition expression is:
(y == 'a' || 'b')
So the “y == 'a'” part is evaluated first which may be true/false depending on the value of "y". Let's call the value of "y=='a'" as "t". And then the expression is evaluated as " t || 'b'" in this case 'b' is actually the ASCII code value of character 'b' (98) which is surely larger than 0, so the result of the boolean expression is always true.
To dismiss any ambiguity caused by operator precedence, I think it's a good habit to use brackets to explicitly express your priority in the evaluation. In your case, as suggested by earlier post, it should be:
if ((y=='a') || (y=='b'))

More than one thing that needs to be true in if statement for it to execute? c++

If I want to make a if statement that requires more than one thing to be true do I need to do it with "else if"? Because I think it looks ugly so I would prefer if I could solve that in one statement.
Here is the code:
if(x == 2 OR 4 OR 6 OR 8 OR 10)
{
something......
}
etc. etc.
return 0;
Will that work?
if (x == 2 || x == 4 || x == 6 || x == 8 || x == 10)
{
...
}
OTOH, if your intent is, "If x is even...":
if (x % 2 == 0)
{
...
}
There isn't much of a better option than this:
if (x == 2 || x == 4 || x == 6 || x == 8 || x == 10)
If you wanted to optimize at the cost of readability:
if (x > 0 && x <= 10 && (x % 2 == 0))
The % will be optimized into a bit-wise AND by the compiler.