Ruby Break Next

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Ruby Break Statement

The Ruby break statement is used to terminate a loop. It is mostly used in while loop where value is printed till the condition is true, then break statement terminates the loop.

The break statement is called from inside the loop.

Syntax:

  1. break 

  Example:

  1. i = 1   
  2. while true   
  3.     if i*5 >= 25   
  4.         break   
  5.     end   
  6.     puts i*5   
  7.     i += 1   
  8. end  

Output:

Ruby Break 1

Ruby Next Statement

The Ruby next statement is used to skip loop's next iteration. Once the next statement is executed, no further iteration will be performed.

The next statement in Ruby is equivalent to continue statement in other languages.

Syntax:

  1. next  

Example:

Output:

Ruby Break 2