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.
 
While-loop-Julia
 
Syntax :

while expression

    statement(s)

end

Here, ‘while‘ is the keyword to start while loop, ‘expression‘ is the condition to be satisfied, and ‘end‘ is the keyword to end the while loop.

Note: A block of code is the set of statements enclosed between the conditional statement and the ‘end‘ statement.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Julia program to illustrate 
# the use of while loop
  
# Declaring Array
Array = ["Geeks", "For", "Geeks"]
  
# Iterator Variable
i = 1
  
# while loop
while i <= length(Array)
  
    # Assigning value to object
    Object = Array[i]
      
    # Printing object
    println("$Object")
      
    # Updating iterator globally
    global i += 1
      
# Ending Loop
end

chevron_right


Output:
While-loop-Output-01
 
Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Julia program to generate 
# the Fibonacci sequence 
  
# The length of Fibonacci sequence
length = 15
  
# The first two values
a = 0
b = 1
  
# Iterator Value
itr = 0
  
# while loop condition
while itr < length
  
   # Printing fibonacci value
   print(a, ", ")
     
   # Updating value
   c = a + b
     
   # Modify values
   global a = b
   global b = c
     
   # Updating iterator
   global itr += 1
  
# End of while loop
end

chevron_right


While-loop-Output-02




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.