Skip to content
Jamiras edited this page Jan 4, 2022 · 2 revisions

if statements allow arbitrary execution of code. They are typically used inside functions or loops to change behavior based on parameter or loop index values.

If a condition is true, the following block of code is executed. If it is not true, it skips the following block of code. else may follow a block of code guarded by an if to be evaluated if the condition is not true. else if may be used to make an alternate requirement for the next block of code.

if (condition) 
{ 
    code 
}
if (condition) 
{
   code 
}
else 
{
   code
}
if (condition) 
{
   code
}
else if (condition)
{ 
   code 
}
else 
{ 
   code 
}

If a block of code is a single statement, the enclosing brackets are not needed.

if (condition) 
    code

Boolean constants

true and false are constants that can be used in if statements. They may be returned by functions or stored in dictionaries.

if (func())
    code
if (dict[key])
    code