____  ___    _  _     _   _ _____     _______
 / ___|/ _ \  | || |   | | | |_ _\ \   / / ____|
| |  _| | | | | || |_  | |_| || | \ \ / /|  _|
| |_| | |_| | |__   _| |  _  || |  \ V / | |___
 \____|\___/     |_|   |_| |_|___|  \_/  |_____|

 --- A GOPHER-LIKE INTERFACE FOR HIVE BLOCKCHAIN ---

Ruby Programming Tutorial - Lesson 11 - Loops in Ruby :: For...in Loop

BY: @bilal-haider | CREATED: March 3, 2018, 9:09 p.m. | VOTES: 24 | PAYOUT: $5.68 | [ VOTE ]

[IMAGE: https://steemitimages.com/DQmQoFzXnhGvq1d7JDwMHCkeBDfsakqNWfcGC7X3TiVJjrG/Capture.JPG]

For...in loop

In this article, we are going to learn about "For" loop its well known loop, and used in almost every programming language. Here is how it works in Ruby

# For loop is used to do something repeatedly 


for i in 0..5
   puts "This is Value of i: #{i} "
end

In this example we are printing a message for 6 times, which is the loop starts at 0 and ends at 5 ..
Prints message containing the value of "i" ..

[IMAGE: https://steemitimages.com/DQmbvETsqb4oTaCND3Q8vXkYU5RTUnQskdfi4gJ89Ja2AZi/image.png]

Lets take another example.
and use a conditional statement inside for...in loop.


for i in 0..10
   if i > 2 then
      break
   end
   puts "Value of 'I' is: #{i}"
end

[IMAGE: https://steemitimages.com/DQmPyDLajshuXsJGR67Z8Ceet6GHAwbgig22tMHoQPwEvZq/image.png]

Note in this example. we have created a loop that had to print a message 11 times.. because
we specifed, it to go from 0 to 10 ..
but it prints only 3 lines.. that because of conditional statement and the "break" statement.

Break statement is used to break a loop at any point of time ...

now the fun example :)

Give vote up on bilal's latest 11 posts :) only if Steem power is greater than 10,000

steem_power         = 50000
account_name        = "bilal-haider"
vote_on_all_posts   = true



for i in 0..10
   if vote_on_all_posts && account_name == "bilal-haider" && steem_power >= 10000 then
      puts "Vote given for post #{i}"
   end
end

[IMAGE: https://steemitimages.com/DQmcfm5vTcBHLaD64hnkquV6LnRjxZGekFMXGTAseynBXox/image.png]

In this lecture you guys have learned on how to use for .. in loop. and also learned how to write break statement
, combining the knowledge of writing conditional statements with loops :)

You guys are becoming an awesome ruby programmer as time is progressing.

TAGS: [ #ruby ] [ #rubyprogramming ] [ #programming ] [ #rubylesson ] [ #dev ]

Replies

@buildawhale | March 6, 2018, 12:39 a.m. | Votes: 0 | [ VOTE ]

You got a 1.40% upvote from @buildawhale courtesy of @bilal-haider!
If you believe this post is spam or abuse, please report it to our Discord #abuse channel.

If you want to support our Curation Digest or our Spam & Abuse prevention efforts, please vote @themarkymark as witness.

@ipromote | March 6, 2018, 8:57 a.m. | Votes: 0 | [ VOTE ]

You got a 3.05% upvote from @ipromote courtesy of @bilal-haider!
If you believe this post is spam or abuse, please report it to our Discord #abuse channel.

If you want to support our Curation Digest or our Spam & Abuse prevention efforts, please vote @themarkymark as witness.

[ BACK TO TRENDING ] [ BACK TO MENU ]
CMD>