Open In App

if keyword – Conditional evaluation in Julia

Keywords in Julia are reserved words that have a specific meaning and operation to the compiler. These keywords can not be used as a variable name, doing the same will stop the execution process and an error will be raised.

'if' keyword in Julia is used to evaluate a condition. If the condition is true, the code after the if statement will be executed otherwise it will skip the same statement and will proceed with the further evaluation of the code.
Syntax:



if condition
    statement
end

Flowchart:

Example:






# Julia program to illustrate 
# the use of 'if' keyword
  
# function definition
function check_if(x)
      
    # if keyword
    if x > 10
        println(x, " is greater than 10")
    end
    println("This is not in if")
end
  
# function call
check_if(15)

Output:

15 is greater than 10
This is not in if

if keyword can also be used with the else keyword to execute another statement in case the if condition results to be false.

Example:




# Julia program to illustrate 
# the use of 'if' keyword
  
# function definition
function check_if(x)
      
    # if keyword
    if x > 10
        println(x, " is greater than 10")
    else
        println(x, " is smaller than 10")
    end
    println("This is not in if")
end
  
# function call
check_if(15)

Output:

5 is smaller than 10
This is not in if

Article Tags :