[Ruby basics] About the role of true and break in the while statement

By setting true in the conditional expression of the while statement, it is possible to "loop the process unless the process is stopped".

Simple example



while true #By setting the conditional expression to true, the processing can be continued as long as the processing is not stopped.
 puts "Please enter the number 1 or 2"
  number = gets.to_i #The character string entered by the gets method is to_Convert to a numerical value with the i method and assign it to the variable number.
 puts "Entered number" #If a string is entered instead of a number, 0 is displayed.
 puts number
 if number == 1
   puts "The number you entered is#{number}is." #After execution, return to the second line (loop)
 elsif number == 2
   puts "The number you entered is#{number}is."  #After execution, return to the second line (loop)
 else
   puts "The number you entered is other than 1 or 2. I'm done."
   break #If you enter a number or character string other than 1 or 2, the else statement is executed and the loop ends.(break).

 end
end

As long as it remains true, the process will continue, so you have to stop it somewhere. You can force it to stop by typing Ctrl + C on the console, but you'll need to set break to do that in your code.

Recommended Posts

[Ruby basics] About the role of true and break in the while statement
[For beginners] DI ~ The basics of DI and DI in Spring ~
[Ruby] The role of subscripts in learning elements in arrays
About the difference between classes and instances in Ruby
[Technical memo] About the advantages and disadvantages of Ruby
[Ruby] Class nesting, inheritance, and the basics of self
[Ruby] Questions and verification about the number of method arguments
About the difference between "(double quotation)" and "single quotation" in Ruby
[Ruby] About the difference between 2 dots and 3 dots of range object.
About the behavior of ruby Hash # ==
Basics of sending Gmail in Ruby
About the role of the initialize method
About the operation of next () and nextLine ()
Summary of hashes and symbols in Ruby
[Ruby] Classification and usage of loops in Ruby
About the mechanism of the Web and HTTP
Basics of java basics ② ~ if statement and switch statement ~
(Determine in 1 minute) About the proper use of empty ?, blank? And present?
[Ruby] Summary of class definitions. Master the basics.
[Ruby] Behavior of evaluation of conditional expression in while
Think about the combination of Servlet and Ajax
About the idea of anonymous classes in Java
Ruby on Rails ~ Basics of MVC and Router ~
Basics of threads and Callable in Java [Beginner]
About next () and nextLine () of the Scanner class
[Ruby] Difference between methods with and without self in the class. About class methods and instance methods.
Do you use the for statement after all? Do you use a while statement? Proper use of for statement and while statement
Find the greatest common divisor and least common multiple of any number of integers in Ruby
ArrayList and the role of the interface seen from List
Count the number of occurrences of a string in Ruby
[Ruby] I thought about the difference between each_with_index and each.with_index
[Grails] About the setting area and the setting items of application.yml
Addition of variables in iterative processing in a while statement
I summarized the types and basics of Java exceptions
Get the URL of the HTTP redirect destination in Ruby
Handling of date and time in Ruby. Use Date and Time properly.
12 Corresponds to the while statement
[Java] for statement, while statement
# 3 [Note] do ~ while statement
Let's understand the if statement!
Let's understand the guard statement!
Let's understand the for-in statement!
Is the ternary operator evil?
Let's understand the switch statement!
Java learning memo (while statement, do-while statement)
[Ruby basics] About the role of true and break in the while statement
[Java] Nowadays, the extended for statement is not exclusively for List, isn't it?
About eval in Ruby
About Ruby if statement
Basics of try-with-resources statement
About the [ruby] operator
I tried to summarize the basics of kotlin and java
A note about the seed function of Ruby on Rails
The nth and n + 1st characters of a Ruby string
[Ruby] Creating code using the concept of classes and instances
Think about the differences between functions and methods (in Java)
Create more Tabs and Fragments in the Fragment of BottomNavigationView
Handling of line beginning and line ending in regular expressions in Ruby
[Java] I thought about the merits and uses of "interface"
About the handling of Null
About for statement and if statement
12 Corresponds to the while statement
About the description of Docker-compose.yml
Understand the basics of docker
About Ruby hashes and symbols
The basics of Swift's TableView
About the same and equivalent
Judgment of fractions in Ruby
About Ruby and object model
About Ruby classes and instances
[Java] Get the dates of the past Monday and Sunday in order