Master “loop” in programming for beginners

Every loop comes with the following three attributes

  1. A set of instructions to be executed inside the loop
  2. How many times the instructions to be executed
  3. A variable

Identifying these help you to understand and write a loop better

While all the above attributes are interconnected, let’s take some examples and identify these attributes – in Ruby

Example 1 – Ruby code snippet to calculate percentage scored

students = [{ name: “John”, score: 125 },
            { name: “James”, score: 75 },
            { name: “Tom”, score: 50 }]

puts "Name     Score out of 150       Percentage”
students.each do |student|
  percentage = (student[:score] / 150) * 100
  puts “#{student[:name}    #{student[:score]}     #{percentage}%”
end
Output

Name     Score out of 150       Percentage
John            125                83%
James            75                50%
Tom              50                33%

Set of instructions to be executed inside the loop: 

Calculating the percentage and printing the result are the instructions inside the loop

What if we move puts “Name     Score out of 150       Percentage” also inside the loop?

The result will be as below

Name     Score out of 150       Percentage
John            125                83%
Name     Score out of 150       Percentage
James            75                50%
Name     Score out of 150       Percentage
Tom              50                33%

Which is not the result we are expecting, so it is important to identify what set of instructions to be inside the loop

How many times the instructions to be executed:

In this case, the loop is executed 3 times until the data in the set is complete, sometimes this is achieved by a logical condition

Example 2 – Ruby code snippet to print number from 1 to 10

number = 1
while number <= 10 do
  puts number
end

Here the loop is executed 10 times, and when the condition is false (when the number becomes 11), the loop is exited

The variable:

This is the most important part of the loop, almost every loop in real-world programming will have one or more variables. A variable is an element in the loop which changes in each execution, this is also the core principle of the loop. The student’s detail is the variable in the first loop. Each time the instructions are executed, the student detail changes.

In the second example, a  loop to print 1 to 10, the variable is the number. Each time the instruction executes, the value of the number changes

I hope this helps you to improve the way you write and understand loops

I am a software developer with 10 years of experience and around 5 years experience in Ruby on rails, if you would like more such articles, let me know, I will do my best.

I am planning to create a Ruby on rails course for beginners for free if you are interested to provide your details in the following link
Join to learn ruby on rails free

Leave a Reply

Your email address will not be published. Required fields are marked *