Question:
C++ newbie: what is wrong with my if/else bool statement?
Antst
2011-06-09 05:04:55 UTC
Hello all:

I am trying to write an if/else statement using a bool expression. But there is a problem. It seems like NEITHER the if or the else is being evaluated. Am I making a silly mistake? Any advice would be appreciated.

start = false;
if( start = false )
{
// Do this
// int result = 2;
}
else
{
// Do that
// int result = 4;
}

The error is:
result was not declared in this scope

What am I doing wrong? Thanks.
Six answers:
JoelKatz
2011-06-09 05:16:00 UTC
As others pointed out, '=' is for assignment. For comparison, use '=='. Also, you don't show where you declared 'start'. I assume it's elsewhere. (You need a 'bool start;' somewhere.)



Also, you commented it out, but you have two declarations for 'result', one in the 'if' and one in the 'else'. Each declaration creates a distinct variable, each called 'result'. So you can't do this:



if(j==false)

{

int result=2;

}

else

{

int result=3;

}

cout << result; // illegal: Which 'result'? neither is in scope



But you can do this:



int result;

if(j==2)

{

result=2;

}

else

{

result=3;

}

cout << result; // legal. There is only one result



By the way, just like in English you wouldn't say "two plus two is four is true", you would just say "two plus two is four", don't use 'j==false'.



Awful: if (j!=false)

Okay: if (j==true)

Best: if (j)



Awful: if (j==false)

Okay: if (j!=true)

Best: if (!j)
anonymous
2011-06-09 06:38:12 UTC
When you are using a boolean in an If statement you can either use the



if(start == false) version



or you can simplify it by removing the == false part and just go with



if(!start) instead.



The ! is a If NOT clause that makes the If statment read the opposite value of a boolean variable, so if start was true then !start is equal to false, the same can be done in reverse.



Anything in if(start) will only be read if start is equal to true, and anything in if(!start) is only read if start is equal to false.



So:



start = false;

if(!start)

{

// Do this

// int result = 2;

}

else

{

// Do that

// int result = 4;

}
anonymous
2011-06-09 05:16:02 UTC
In the first line if you are declaring a variable then you should specify type (don't worry if you have already declared it somewhere)



bool start = false; //Is it really bool or boolean I am not sure :D?



Equal comparison needs always '==' instead of '=' (without quotes)



if (start == false) //correct

if(start=false)//It is assignment but I think its a valid statment since false will be assigned to start and because its false so the condition is false and else part will be executed.



and since you are using same variable 'results' in multiple low level scopes (conditions) so you have to declare it before if statement to make it visible through inner scope



like



boolean start = false;

int result = 0;



then inside the if body only use result

e.g

result = 0;
cja
2011-06-09 05:10:38 UTC
You set start = false, then you do it again in your 'if' statement:



  if (start = false)



That needs to be:



  if (start == false)



If you declare 'result' inside your 'if' or 'else' block, that variable is not visible to code you have outside those blocks. Declare 'result' before the 'if' statement.
?
2016-10-06 08:29:58 UTC
possibly it is through fact it wasn't certainly in question form. possibly in case you will pronounced, i'd choose to renowned extra approximately diverse accents. Can all and sundry tell me theirs & approximately it & supply me a reliable occasion of the variety you will say some thing in contrast to somebody else? besides, do you recognize if somebody reported you? somebody would have basically been indignant via the "ahhd jist loove to heeeaar from y'all all mah superb to ya paw." & reported it.
Zarn
2011-06-09 05:05:43 UTC
Try something like:

if (start == false)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...