kqvanity
kqvanity

Reputation: 100

what does "the condition is met" exactly mean in programming languages?

A conditional statement is based on concept of if/then/else ; if a condition is met , then your code executes one or more statements , else your code does something different

I encountered this statement and don't understand what "meeting the condition" means.

Upvotes: 0

Views: 3703

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1500525

The part in brackets in the if statement is the condition. For example, in

if (x == 5)

the condition is x == 5. The condition being "met" basically means "if the expression evaluates to true" (so here, if the value of x is 5). In Javascript it's a little more complicated than that, because the condition doesn't have to be an actual Boolean expression. Based on the result of the expression, the following evaluation takes place:

  • Undefined => false
  • Null => false
  • Boolean => the value (so true => true, false => false)
  • Number => true for any number except 0, -0, or NaN, false for those
  • String => true for any non-empty string, false otherwise
  • Symbol => true
  • BigInt => true for any non-zero value
  • Object => true

If the result of that is true, then the condition is met and the body of the if statement is executed. Otherwise, the body of the else part is executed if there is one.

Upvotes: 4

Chico3001
Chico3001

Reputation: 1963

the if instruction is evaluating whatever is inside the parenthesis, if the condition inside the parenthesis is true then the if statemen is executed, otherwise the else statement is executed

var = 5;
if( var === 5)
{
  alert("this is shown because var equals 5")
}
else
{
  alert("this will never show unless you change var or the condition inside the if parenthesis")
}

Upvotes: 0

Related Questions