Open In App

For loop in Julia

Last Updated : 19 Feb, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

For loops are used to iterate over a set of values and perform a set of operations that are given in the body of the loop. For loops are used for sequential traversal. In Julia, there is no C style for loop, i.e., for (i = 0; i < n; i++). There is a "for in" loop which is similar to for each loop in other languages. Let us learn how to use for in loop for sequential traversals.   For-Loop-Julia
 

Syntax:

for iterator in range
    statements(s)
end

Here, ‘for‘ is the keyword stating for loop, ‘in‘ keyword is used to define a range in which to iterate, ‘end‘ keyword is used to denote the end of for loop.

Example:




# Julia program to illustrate 
# the use of For loop
  
print("List Iteration\n"
l = ["geeks", "for", "geeks"
for i in l
    println(i) 
end
  
# Iterating over a tuple (immutable) 
print("\nTuple Iteration\n"
t = ("geeks", "for", "geeks"
for i in t
    println(i) 
end
  
# Iterating over a String 
print("\nString Iteration\n")     
s = "Geeks"
for i in
    println(i) 
end


Output:
For-loop-Output-01
 
Nested for-loop: Julia programming language allows to use one loop inside another loop. The following section shows an example to illustrate the concept.
Syntax:

for iterator in range
    for iterator in range 
        statements(s) 
        statements(s)
    end
end
 

Example:




# Julia program to illustrate 
# the use of Nested For-Loops
  
# Outer For-loop
for i in 1:5 
  
    # Inner For-loop
    for j in 1:i 
          
        # Print statement
        print(i, " ")
    end
    println() 
end


Output:
For-loop-Output-02



Similar Reads

Julia continue Keyword | Continue iterating to next value of a loop in Julia
Keywords in Julia are predefined words that have a definite meaning to the compiler. These keywords can’t be used to name variables. 'continue' keyword in Julia skips the statement immediately after the continue statement. Whenever the continue keyword is executed, the compiler immediately stops iterating over further values and sends the execution
1 min read
Julia break Keyword | Exiting from a loop in Julia
Keywords in Julia are predefined words that have a definite meaning to the compiler. These keywords can't be used to name variables. 'break' keyword in Julia is used to exit from a loop immediately. Whenever the break keyword is executed, the compiler immediately stops iterating over further values and sends the execution pointer out of the loop. S
1 min read
Julia end Keyword | Marking end of blocks in Julia
Keywords in Julia are reserved words that have a pre-defined meaning to the compiler. These keywords can't be used as a variable name. 'end' keyword in Julia is used to mark the end of a block of statements. This block can be of any type like struct, loop, conditional statement, module, etc. Syntax: block_type block_name Statement Statement end Exa
1 min read
Julia function keyword | Create user-defined functions in Julia
Keywords are the reserved words in Julia which have a predefined meaning to the compiler. These keywords are used to reduce the number of lines in code. Keywords in Julia can't be used as variable names. 'function' keyword is used to create user-defined functions in Julia. These functions are reusable codes that can be called from anywhere in the c
1 min read
Julia local Keyword | Creating a local variable in Julia
Keywords in Julia are reserved words whose value is pre-defined to the compiler and can not be changed by the user. These words have a specific meaning and perform their specific operation on execution.'local' keyword in Julia is used to create a variable of a limited scope whose value is local to the scope of the block in which it is defined. Synt
2 min read
Julia global Keyword | Creating a global variable in Julia
Keywords in Julia are reserved words whose value is pre-defined to the compiler and can not be changed by the user. These words have a specific meaning and perform their specific operation on execution. 'global' keyword in Julia is used to access a variable that is defined in the global scope. It makes the variable where it is used as its current s
2 min read
while loop in Julia
In Julia, while loop is used to execute a block of statements repeatedly until a given condition is satisfied. And when the condition becomes false, the line immediately after the loop in the program is executed. If the condition is false when the while loop is executed first time, then the body of the loop will never be executed. Syntax : while ex
2 min read
Loop Vectorization in Julia
Vectorization is used to speed up the code without using loop. Using such a function can help in minimizing the running time of code efficiently. There are two meanings of the word vectorization in the high-level languages, and they refer to different things. When we talk about vectorized code in Python/Numpy/Matlab/etc., we are usually referring t
6 min read
Getting ceiling value of x in Julia - ceil() Method
The ceil() is an inbuilt function in julia which is used to return the nearest integral value greater than or equal to the specified value x. Syntax: ceil(x) Parameters: x: Specified value. Returns: It returns the nearest integral value greater than or equal to the specified value x. Example 1: # Julia program to illustrate # the use of ceil method
1 min read
Getting floor value of x in Julia - floor() Method
The floor() is an inbuilt function in julia which is used to return the nearest integral value less than or equal to the specified value x. Syntax: floor(x) Parameters: x: Specified value. Returns: It returns the nearest integral value less than or equal to the specified value x. Example 1: # Julia program to illustrate # the use of floor() method
1 min read
Article Tags :