Clarification in Programming, If Statement.

Sakuto

Active member
Regular
Joined
Apr 30, 2014
Messages
887
Kin
0💸
Kumi
0💴
Trait Points
0⚔️
Awards
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
Kin
0💸
Kumi
0💴
Trait Points
0⚔️
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.
 
  • Like
Reactions: Sakuto

vipcool7

Active member
Regular
Joined
Aug 10, 2011
Messages
760
Kin
0💸
Kumi
0💴
Trait Points
0⚔️
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

Active member
Regular
Joined
May 11, 2011
Messages
1,678
Kin
0💸
Kumi
0💴
Trait Points
0⚔️
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")
 
Top