What Will I Learn?
Learning to program can be done with any general language: the same rules basic are used regardless of the language. However, some are closer than others to the human logic and therefore better adapted to the learning of programming. This is the case of RUBY
.
- The Iterations
- Loops with Example
- Comparison of loops
- Summary of total tutorial
Requirements
- Mac OS,Windows or Linux OS
- Go to http://www.ruby-lang.org/en/downloads, click on Download Ruby,
Difficulty
- Intermediate
Tutorial Contents
The Iterations
The Iterations
The iterations are the loops
, that is to say the sequences of instructions that are executed with several times. This situation is very common in programming. It is used especially when it must make a total from several amounts. For example, the amount of an invoice is calculated thus, knowing that an invoice can consist of several articles:
1 .go to the first invoice line (in the first article);
2 . put the amount of this line in the total variable;
3 .as long as there are lines, go to the next line and add the amount to the total variable;
4 .when there is no more line, the total variable contains the real total.
Ruby includes four main forms of loops:
• while ... end ("looping as long as ...")
• until ... end ("loop until ...")
• for ... in ... end ("loop the specified number of times ...")
• loop do ... end ("looping").
For example, the iteration while ... end
below shows the numbers 1, 2 and 3 one after the other:
number = 1
while number <4
puts number
number + = 1
end
Initially, number is 1. When entering the loop, Ruby checks that it is smaller than 4, which is the case, which allows the iteration to begin. The puts number statement displays the value of the variable, ie
1, then number + = 1 increments this value by 1, which brings it to 2, and the loop starts again until the value of the variable reaches 4, which stops the loop. The processing then continues with the instructions that are after the loop. Instead of number + = 1, we can write number = number + 1.
The incrementation
is the operation of increasing the value of a variable each turn loop (in general, the increment is 1). The variable used for incrementation is called the counter and his name is traditionally i. Instead of i = i + 1, we can also write i + = 1. This notation is popular. It comes from the C language. We can also decrement the counter. Its value then drops with each turn.
With the until ... end
loop, here is how to display the numbers 1, 2 and 3:
i = 1
until i > 3
puts i
i += 1
end
The for ... end
loop is a third possibility:
for i in 1..3
puts i
end
Finally, there is the loop loop do ... end
:
i = 1
loop do
puts i
i + = 1
break if i> 3
end
There is a short form of these loops. Example:
i = 0
print "# {i + = 1}" while i <4
The expression "# {i}"
means that the value of i must be displayed. It can be practical. Both
Following code snippets are equivalent:
puts "Total : " + i.to_s | puts "Total : #{i.to_s}" |
---|
You can also put the loop statement at the end of the block rather than at the beginning. Example:
i = 0
begin
print "#{i} "
i += 1
end while i < 4
We can even write something like:
i = 4
puts (loop do i -= 1 ; print "#{i}, " ; break " Partez !" if i==1 ; end)
What the interpreter answers "3, 2, 1, Go! ".
The semicolons replace the line break to separate the instructions. They are used when you want to write several instructions on a single line.
Comparison of loops
Comparison of loops
For, while, and until loops exist in almost any language.What is the difference between the four forms of loops?
• for ... end is used to make a predetermined number of turns. This is the most readable loop shape and the most compact so far as we know how many turns to do.
• while ... end is used when the number of loops is not known beforehand. It stops when a given condition ceases to be true.
• until ... end it is equivalent to while, but the stop occurs when a given condition ceases to be false (when it becomes true).
• loop do ... end is unconditional. This is the simplest iteration. She does not stops only if we use the statement break to place a condition stop inside the loop.
In summary
In summary
The essential notions of programming are four in number:
• objects, anything that can be manipulated: strings, numbers, tables, etc. ; the variables are part of it;
• the methods, that is to say the possible actions on the objects;
• classes and the organization of objects that goes with (inheritance, polymorphism, ...);
• the flow of treatment: connections (if ... if) and loops (repeat .... as ...).
Curriculum
The tutorials which I have already shared on Utopian that make up a Course Curriculum -
Posted on Utopian.io - Rewarding Open Source Contributors
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Thanks For The Approval. I'm glad to make this contribution.
Hey @meblogger I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x