Related Articles

Related Articles

if keyword – Conditional evaluation in Julia
  • Last Updated : 22 Apr, 2020

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:

if-statement-in-julia

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output:

5 is smaller than 10
This is not in if



My Personal Notes arrow_drop_up
Recommended Articles
Page :