Clarification in Programming, If Statement.

Sakuto

Leaf Village Regular 🍃
Regular
Joined
Apr 30, 2014
Messages
887
Reaction score
78
I'm currently trying to help someone fix some bugs in his game and I'd like some clarification on the usage of "If" statements in Java (even if you don't know Java, the function is essentially the same throughout most programming languages).

I am wondering if the if statements can be written like this:

If ("boolean expression")
{
//code
}

As opposed to this:

If ("boolean expression" == true).
{
//code
{

Is the "== true" required when checking a boolean expression (and will the code execute if the boolean expression returns true, but not false?). Rep+ for anybody who can shed some light on this topic!
 

sacmador

Member
Joined
May 13, 2012
Messages
144
Reaction score
9
No you dont need to. If the expression is to be resulted as a boolean value, the first one will be enough. Indeed, in some languages you can even use integers in if's expression blog but for now i am not sure java enabled this one.
 

vipcool7

Leaf Village Regular 🍃
Regular
Joined
Aug 10, 2011
Messages
760
Reaction score
179
No it is not necessary
If statement works only if the condition stated in the bracket is true
In programming languages, a non-zero value is considered true and zero value is considered false
So if i write...
If(3+2)
{
//code
}
the code will execute whereas in
If(5-5)
{
//code
}
the code will not execute
Hope this helps...
 

Itachi2020

Jōnin Strategist 🧠
Regular
Joined
May 11, 2011
Messages
1,678
Reaction score
187
As mentioned above, adding "== true" isn't necessary at all, same applies for checking for False value,

If ("boolean expression" == false)

is equivalent to

If (!"boolean expression")
 

Wabbit

Banned
Legendary
Joined
Nov 15, 2011
Messages
11,335
Reaction score
797
In C
if (x) //if x is true
execute something;

if(x == somevalue)
execute something;
teh above statements are same.it is like if x has some value execute the if block.
 

Konohamaru of Akatsuki

Banned
Regular
Joined
May 24, 2014
Messages
647
Reaction score
82
Not to sound like an ass, but you probably shouldn't be trying to help somebody fix a game if you don't know how boolean expressions work.
 
Top