While Loop
Loops are used to do something repeatedly ..
e.g Get all the users data .. and display it ...
Loops can also be endless .. where you constantly check for something ...
e.g Look at all the transactions, when your find "bilal-haider's" transaction, send me a notification
Loops add more power to your programming skills,
Lets take a coding example :
You are given an initial balance of 5000, you need to do hard work.. until you reach 100,000
Every Time you do hardwork, display a message .. and your balance will be increased by 1000
## Do hardwork, increase your balance by 1000, until 100,000
name = "Bilal Haider"
balance = 5000
age = 27
while balance < 100000
puts "Need to do More Work!!"
balance += 1000 # balance = balance + 1000
print "Your balance Increased : "
puts balance
end
Note: While loop starts with a keyword "while" and ends with the keyword "end"
soon after the keyword, we put our conditional statement .. which can be consisting upon, comparisons, logical operations .. or both.
after conditional statement, there comes the body ..
and code written in between the body of while loop. executes over and over again.. until the conditional statement is true..
While loop comes with few variations..
like in this example. a little key word "do" is added..
which defines the start of the body section of the loop...
I like this notation more than earlier one..
Ruby while modifier
Here is another variation of while loop.
This loop's body is surrounded by Begin and End statements ..
and conditional statement is written at the end of the loop
name = "Bilal Haider"
balance = 5000
age = 27
begin
puts "BEGIN to do More Work!!"
balance += 1000 # balance = balance + 1000
print "Your balance Increased : "
puts balance
end while balance < 10000
Its time for a fun example :)
While I am not rich .. I need to keep pushing myself forward :)
In the following fun program example. we want us to become rich.
we set a target for us ..
and we define that we are going to charge 1000$ for each ruby programming project ..
once the project is completed we increase our balance by 1000 .. until we reach our total earning target ..
isn't it fun ?
## Become rich program :D
name = "Bilal Haider"
$balance = 1000
age = 27
$coding_project = 0
$project_price = 1000
$richness_target = 10000
# Total Projects need to done ?
$coding_projects_to_be_done = $richness_target / $project_price
begin
puts "I am not Rich yet .. "
$balance += $project_price
$current_project = $balance / $project_price
puts "Project #$current_project is Completed !! "
puts "Earned #$project_price$ "
end while !($balance >= $richness_target)
puts "WOW I completed #$coding_projects_to_be_done Ruby Projects "
# Note that i am defining variables with $symbol here. it allows me to use those variables inside of puts
congrats my friends, Now you can use while loop inside of your programs.
It gives you more power of doing, so many more things
so lets get ready a cup of coffee and start coding for awesomeness
that's really great, While Loop was my favorite topic, in my projects it is being used when rather than other loops.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thanks for comment bro :) Yes it is very useful loop :)
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
You got a 1.09% upvote from @postpromoter courtesy of @bilal-haider!
Want to promote your posts too? Check out the Steem Bot Tracker website for more info. If you would like to support the development of @postpromoter and the bot tracker please vote for @yabapmatt for witness!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit