C++ if statement explanation confusion [closed]
I am new to C++ and sometimes it is difficult for me to understand complex if statements, could anyone help explain following code to me? Thanks in advance.
Given:
int vis[25], g[25][25], Ty[25][25];
for (int i = 1; i < 30; i++)
{
if(!vis[i] && g[x][i] == 1 && Ty[f][i] != n) {...}
}
Vis is an array and only initialized and there is no any assigned value in it at the moment.
So what does !vis[i] mean?
Does it mean vis[i] !=0 or vis[i]==1 or something else ?
c++ if-statement operator-keyword boolean-operations
closed as unclear what you're asking by Neil Butterworth, Matthieu Brucher, Sid S, Killzone Kid, ead Nov 26 '18 at 9:26
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I am new to C++ and sometimes it is difficult for me to understand complex if statements, could anyone help explain following code to me? Thanks in advance.
Given:
int vis[25], g[25][25], Ty[25][25];
for (int i = 1; i < 30; i++)
{
if(!vis[i] && g[x][i] == 1 && Ty[f][i] != n) {...}
}
Vis is an array and only initialized and there is no any assigned value in it at the moment.
So what does !vis[i] mean?
Does it mean vis[i] !=0 or vis[i]==1 or something else ?
c++ if-statement operator-keyword boolean-operations
closed as unclear what you're asking by Neil Butterworth, Matthieu Brucher, Sid S, Killzone Kid, ead Nov 26 '18 at 9:26
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
6
What does your C++ textbook have to say on the subject?
– Neil Butterworth
Nov 24 '18 at 23:48
4
This is fundamental language construct that any decent textbook will cover. Please grab a good textbook and work through the fundamentals of the language. That will more helpful to you in the long run than getting an answer to this specific question.
– R Sahu
Nov 24 '18 at 23:51
int vis[25]
thenfor (int i = 1; i < 30; i++)
thenvis[i]
is a bug.i
must be between 0 and 24 for an array of 25 elements.
– drescherjm
Nov 24 '18 at 23:55
add a comment |
I am new to C++ and sometimes it is difficult for me to understand complex if statements, could anyone help explain following code to me? Thanks in advance.
Given:
int vis[25], g[25][25], Ty[25][25];
for (int i = 1; i < 30; i++)
{
if(!vis[i] && g[x][i] == 1 && Ty[f][i] != n) {...}
}
Vis is an array and only initialized and there is no any assigned value in it at the moment.
So what does !vis[i] mean?
Does it mean vis[i] !=0 or vis[i]==1 or something else ?
c++ if-statement operator-keyword boolean-operations
I am new to C++ and sometimes it is difficult for me to understand complex if statements, could anyone help explain following code to me? Thanks in advance.
Given:
int vis[25], g[25][25], Ty[25][25];
for (int i = 1; i < 30; i++)
{
if(!vis[i] && g[x][i] == 1 && Ty[f][i] != n) {...}
}
Vis is an array and only initialized and there is no any assigned value in it at the moment.
So what does !vis[i] mean?
Does it mean vis[i] !=0 or vis[i]==1 or something else ?
c++ if-statement operator-keyword boolean-operations
c++ if-statement operator-keyword boolean-operations
asked Nov 24 '18 at 23:44
WeiWei
1
1
closed as unclear what you're asking by Neil Butterworth, Matthieu Brucher, Sid S, Killzone Kid, ead Nov 26 '18 at 9:26
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by Neil Butterworth, Matthieu Brucher, Sid S, Killzone Kid, ead Nov 26 '18 at 9:26
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
6
What does your C++ textbook have to say on the subject?
– Neil Butterworth
Nov 24 '18 at 23:48
4
This is fundamental language construct that any decent textbook will cover. Please grab a good textbook and work through the fundamentals of the language. That will more helpful to you in the long run than getting an answer to this specific question.
– R Sahu
Nov 24 '18 at 23:51
int vis[25]
thenfor (int i = 1; i < 30; i++)
thenvis[i]
is a bug.i
must be between 0 and 24 for an array of 25 elements.
– drescherjm
Nov 24 '18 at 23:55
add a comment |
6
What does your C++ textbook have to say on the subject?
– Neil Butterworth
Nov 24 '18 at 23:48
4
This is fundamental language construct that any decent textbook will cover. Please grab a good textbook and work through the fundamentals of the language. That will more helpful to you in the long run than getting an answer to this specific question.
– R Sahu
Nov 24 '18 at 23:51
int vis[25]
thenfor (int i = 1; i < 30; i++)
thenvis[i]
is a bug.i
must be between 0 and 24 for an array of 25 elements.
– drescherjm
Nov 24 '18 at 23:55
6
6
What does your C++ textbook have to say on the subject?
– Neil Butterworth
Nov 24 '18 at 23:48
What does your C++ textbook have to say on the subject?
– Neil Butterworth
Nov 24 '18 at 23:48
4
4
This is fundamental language construct that any decent textbook will cover. Please grab a good textbook and work through the fundamentals of the language. That will more helpful to you in the long run than getting an answer to this specific question.
– R Sahu
Nov 24 '18 at 23:51
This is fundamental language construct that any decent textbook will cover. Please grab a good textbook and work through the fundamentals of the language. That will more helpful to you in the long run than getting an answer to this specific question.
– R Sahu
Nov 24 '18 at 23:51
int vis[25]
then for (int i = 1; i < 30; i++)
then vis[i]
is a bug. i
must be between 0 and 24 for an array of 25 elements.– drescherjm
Nov 24 '18 at 23:55
int vis[25]
then for (int i = 1; i < 30; i++)
then vis[i]
is a bug. i
must be between 0 and 24 for an array of 25 elements.– drescherjm
Nov 24 '18 at 23:55
add a comment |
2 Answers
2
active
oldest
votes
int vis[25], g[25][25], Ty[25][25];
Declares 3 arrays. "vis" is 1D array of size 25, "g" is 2D array of size 25x25, and same with "Ty".
if(!vis[i] && g[x][i] == 1 && Ty[f][i] != n) {...}
In C++, integers evaluate to "false" in boolean expressions if the value is 0, and "true" for all other values. So in the if statement, the first expression "!vis[i]" will evaluate to true when vis[i] == 0.
The second expression will evaluate true when the value at index [x][i] in 'g' is equal to 1.
The third statement will evaluate to true when the value at index [f][i] in 'Ty' is not equal to some variable 'n', which is presumably defined somewhere in your program like 'x'.
*Note - as drescherjm noted in a comment, the for-loop should only go up to 24, as the size of your arrays is 25 in either direction, so accessing an element outside the range 0-24 (inclusive) is undefined behavior.
add a comment |
- A
int
is onlyfalse
if it's0
. This means with any other number the string is true. - A
!
in front of a logical statement negates it.
So the if check:
the array
vis
on positioni
has to be0
.
!vis[i] == !(vis[i] != 0) == vis[i] == 0
The array value of
g
onx
,i
must be one- The array of
Ty
on positionf
,i
cannot be equal to the value inn
1
Actually 1 means -vis[i] == 0
and it cannot be null asvis[i]
is not a pointer
– Slava
Nov 24 '18 at 23:56
@Slava Oh, right. It is notnull
but it is0
. I'll correct it.
– Darkproduct
Nov 24 '18 at 23:57
1 saysvis[i]
must be equal 0, not the other way around
– Slava
Nov 24 '18 at 23:59
@Slava absolutely right... I don't know what I've thought...
– Darkproduct
Nov 25 '18 at 0:02
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
int vis[25], g[25][25], Ty[25][25];
Declares 3 arrays. "vis" is 1D array of size 25, "g" is 2D array of size 25x25, and same with "Ty".
if(!vis[i] && g[x][i] == 1 && Ty[f][i] != n) {...}
In C++, integers evaluate to "false" in boolean expressions if the value is 0, and "true" for all other values. So in the if statement, the first expression "!vis[i]" will evaluate to true when vis[i] == 0.
The second expression will evaluate true when the value at index [x][i] in 'g' is equal to 1.
The third statement will evaluate to true when the value at index [f][i] in 'Ty' is not equal to some variable 'n', which is presumably defined somewhere in your program like 'x'.
*Note - as drescherjm noted in a comment, the for-loop should only go up to 24, as the size of your arrays is 25 in either direction, so accessing an element outside the range 0-24 (inclusive) is undefined behavior.
add a comment |
int vis[25], g[25][25], Ty[25][25];
Declares 3 arrays. "vis" is 1D array of size 25, "g" is 2D array of size 25x25, and same with "Ty".
if(!vis[i] && g[x][i] == 1 && Ty[f][i] != n) {...}
In C++, integers evaluate to "false" in boolean expressions if the value is 0, and "true" for all other values. So in the if statement, the first expression "!vis[i]" will evaluate to true when vis[i] == 0.
The second expression will evaluate true when the value at index [x][i] in 'g' is equal to 1.
The third statement will evaluate to true when the value at index [f][i] in 'Ty' is not equal to some variable 'n', which is presumably defined somewhere in your program like 'x'.
*Note - as drescherjm noted in a comment, the for-loop should only go up to 24, as the size of your arrays is 25 in either direction, so accessing an element outside the range 0-24 (inclusive) is undefined behavior.
add a comment |
int vis[25], g[25][25], Ty[25][25];
Declares 3 arrays. "vis" is 1D array of size 25, "g" is 2D array of size 25x25, and same with "Ty".
if(!vis[i] && g[x][i] == 1 && Ty[f][i] != n) {...}
In C++, integers evaluate to "false" in boolean expressions if the value is 0, and "true" for all other values. So in the if statement, the first expression "!vis[i]" will evaluate to true when vis[i] == 0.
The second expression will evaluate true when the value at index [x][i] in 'g' is equal to 1.
The third statement will evaluate to true when the value at index [f][i] in 'Ty' is not equal to some variable 'n', which is presumably defined somewhere in your program like 'x'.
*Note - as drescherjm noted in a comment, the for-loop should only go up to 24, as the size of your arrays is 25 in either direction, so accessing an element outside the range 0-24 (inclusive) is undefined behavior.
int vis[25], g[25][25], Ty[25][25];
Declares 3 arrays. "vis" is 1D array of size 25, "g" is 2D array of size 25x25, and same with "Ty".
if(!vis[i] && g[x][i] == 1 && Ty[f][i] != n) {...}
In C++, integers evaluate to "false" in boolean expressions if the value is 0, and "true" for all other values. So in the if statement, the first expression "!vis[i]" will evaluate to true when vis[i] == 0.
The second expression will evaluate true when the value at index [x][i] in 'g' is equal to 1.
The third statement will evaluate to true when the value at index [f][i] in 'Ty' is not equal to some variable 'n', which is presumably defined somewhere in your program like 'x'.
*Note - as drescherjm noted in a comment, the for-loop should only go up to 24, as the size of your arrays is 25 in either direction, so accessing an element outside the range 0-24 (inclusive) is undefined behavior.
edited Nov 25 '18 at 20:56
answered Nov 25 '18 at 0:00
user3150552user3150552
5217
5217
add a comment |
add a comment |
- A
int
is onlyfalse
if it's0
. This means with any other number the string is true. - A
!
in front of a logical statement negates it.
So the if check:
the array
vis
on positioni
has to be0
.
!vis[i] == !(vis[i] != 0) == vis[i] == 0
The array value of
g
onx
,i
must be one- The array of
Ty
on positionf
,i
cannot be equal to the value inn
1
Actually 1 means -vis[i] == 0
and it cannot be null asvis[i]
is not a pointer
– Slava
Nov 24 '18 at 23:56
@Slava Oh, right. It is notnull
but it is0
. I'll correct it.
– Darkproduct
Nov 24 '18 at 23:57
1 saysvis[i]
must be equal 0, not the other way around
– Slava
Nov 24 '18 at 23:59
@Slava absolutely right... I don't know what I've thought...
– Darkproduct
Nov 25 '18 at 0:02
add a comment |
- A
int
is onlyfalse
if it's0
. This means with any other number the string is true. - A
!
in front of a logical statement negates it.
So the if check:
the array
vis
on positioni
has to be0
.
!vis[i] == !(vis[i] != 0) == vis[i] == 0
The array value of
g
onx
,i
must be one- The array of
Ty
on positionf
,i
cannot be equal to the value inn
1
Actually 1 means -vis[i] == 0
and it cannot be null asvis[i]
is not a pointer
– Slava
Nov 24 '18 at 23:56
@Slava Oh, right. It is notnull
but it is0
. I'll correct it.
– Darkproduct
Nov 24 '18 at 23:57
1 saysvis[i]
must be equal 0, not the other way around
– Slava
Nov 24 '18 at 23:59
@Slava absolutely right... I don't know what I've thought...
– Darkproduct
Nov 25 '18 at 0:02
add a comment |
- A
int
is onlyfalse
if it's0
. This means with any other number the string is true. - A
!
in front of a logical statement negates it.
So the if check:
the array
vis
on positioni
has to be0
.
!vis[i] == !(vis[i] != 0) == vis[i] == 0
The array value of
g
onx
,i
must be one- The array of
Ty
on positionf
,i
cannot be equal to the value inn
- A
int
is onlyfalse
if it's0
. This means with any other number the string is true. - A
!
in front of a logical statement negates it.
So the if check:
the array
vis
on positioni
has to be0
.
!vis[i] == !(vis[i] != 0) == vis[i] == 0
The array value of
g
onx
,i
must be one- The array of
Ty
on positionf
,i
cannot be equal to the value inn
edited Nov 25 '18 at 0:02
answered Nov 24 '18 at 23:53
DarkproductDarkproduct
304116
304116
1
Actually 1 means -vis[i] == 0
and it cannot be null asvis[i]
is not a pointer
– Slava
Nov 24 '18 at 23:56
@Slava Oh, right. It is notnull
but it is0
. I'll correct it.
– Darkproduct
Nov 24 '18 at 23:57
1 saysvis[i]
must be equal 0, not the other way around
– Slava
Nov 24 '18 at 23:59
@Slava absolutely right... I don't know what I've thought...
– Darkproduct
Nov 25 '18 at 0:02
add a comment |
1
Actually 1 means -vis[i] == 0
and it cannot be null asvis[i]
is not a pointer
– Slava
Nov 24 '18 at 23:56
@Slava Oh, right. It is notnull
but it is0
. I'll correct it.
– Darkproduct
Nov 24 '18 at 23:57
1 saysvis[i]
must be equal 0, not the other way around
– Slava
Nov 24 '18 at 23:59
@Slava absolutely right... I don't know what I've thought...
– Darkproduct
Nov 25 '18 at 0:02
1
1
Actually 1 means -
vis[i] == 0
and it cannot be null as vis[i]
is not a pointer– Slava
Nov 24 '18 at 23:56
Actually 1 means -
vis[i] == 0
and it cannot be null as vis[i]
is not a pointer– Slava
Nov 24 '18 at 23:56
@Slava Oh, right. It is not
null
but it is 0
. I'll correct it.– Darkproduct
Nov 24 '18 at 23:57
@Slava Oh, right. It is not
null
but it is 0
. I'll correct it.– Darkproduct
Nov 24 '18 at 23:57
1 says
vis[i]
must be equal 0, not the other way around– Slava
Nov 24 '18 at 23:59
1 says
vis[i]
must be equal 0, not the other way around– Slava
Nov 24 '18 at 23:59
@Slava absolutely right... I don't know what I've thought...
– Darkproduct
Nov 25 '18 at 0:02
@Slava absolutely right... I don't know what I've thought...
– Darkproduct
Nov 25 '18 at 0:02
add a comment |
6
What does your C++ textbook have to say on the subject?
– Neil Butterworth
Nov 24 '18 at 23:48
4
This is fundamental language construct that any decent textbook will cover. Please grab a good textbook and work through the fundamentals of the language. That will more helpful to you in the long run than getting an answer to this specific question.
– R Sahu
Nov 24 '18 at 23:51
int vis[25]
thenfor (int i = 1; i < 30; i++)
thenvis[i]
is a bug.i
must be between 0 and 24 for an array of 25 elements.– drescherjm
Nov 24 '18 at 23:55