Why does this code only output 1-3 not 4?? 😕 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why does this code only output 1-3 not 4?? 😕

😲😲 The range is 1..5 , and is supposed to break if i>3, but shouldn't the code be written as i>=3?? because it outputs "1, 2, 3" .... 3 is equal to 3 , not greater than . so it should output "1, 2, 3, 4" ???? for i in 1..5 break if i > 3 puts i end # outputs: # 1 # 2 # 3

17th Jun 2017, 4:19 AM
Aisha S. 🦋
Aisha S. 🦋 - avatar
2 Answers
+ 8
You print i AFTER the condition is evaluated. when i is 3: is 3 > 3? No, don't break. print 3; when i is 4: is 4 > 3? Yes, break. *Exits loop.* If it were i >= 3, then the output would be: 1, 2. Same as i > 2.
17th Jun 2017, 5:47 AM
Rrestoring faith
Rrestoring faith - avatar
+ 2
thank you !!! 😊 @Rrestoring faith
17th Jun 2017, 3:54 PM
Aisha S. 🦋
Aisha S. 🦋 - avatar