📜  Julia end 关键字 |在 Julia 中标记块的结尾

📅  最后修改于: 2021-11-25 04:42:36             🧑  作者: Mango

Julia 中的关键字是对编译器具有预定义含义的保留字。这些关键字不能用作变量名。

'end'关键字用于标记语句块的结束。该块可以是任何类型,如结构、循环、条件语句、模块等。

句法:

block_type block_name
    Statement
    Statement
end

例子:

# Julia program to illustrate
# the use of 'end' keyword
  
# Defining a function
function fn()
      
    # Defining for-loop
    for i in 1:5
      
        # Using if-else block
        if i % 2 == 0
            println(i, " is even");
        else
            println(i, " is odd");
              
        # Marking end of if-else
        end
          
    # Marking end of for-loop
    end
      
# Marking end of function    
end
   
# Function call
fn()

输出:

1 is odd
2 is even
3 is odd
4 is even
5 is odd

例子:

# Julia program to illustrate
# the use of 'end' keyword
  
# Defining Array
Array1 = Array([1, 2, 3, 4, 5])
  
# Printing last element using 'end' keyword
println("Last element of Array: ", Array1[end])

输出:

Last element of Array: 5