About if statement and branch processing

Branch processing

Sequential processing

If you use variables and puts, you can do some basic calculations and output to the screen. However, it is also a fact that it is not enough as a program. The processing that only executes the various processing described in the program in that order, It is called sequential processing. Qiita

Branch processing

However, programs cannot be created by sequential processing alone. Different things have to be done in different situations. For example, if you are making a game program, you need to branch the process according to the conditions, such as "If you hit an enemy, the game is over". Processing that changes the processing flow under certain conditions like this is called branch processing.

Qiita

In the Ruby language, an instruction called if is provided as an instruction for describing branch processing. Here, I will explain it.

if statement

Sample program Let's start by learning about the if statement, which is the most basic process of conditional branching. The if statement is a word that means "what if" in English, and is used to perform processing such as "if ...". First, take a look at the program below.

1-1.rb


def calculation
 puts "Enter a number"
 a = gets.to_i
 if(a > 0) 
   puts "The value you enter is a positive number.\n"  #Execute if it is a positive number
 end
end

When I run the program, "Enter a number:" is displayed on the console screen, The cursor will appear next to it, and it will shift to the input mode from the keyboard. Now enter a positive number from the keyboard.

Execution result 1.(The day when you entered a positive loss)


Enter a number:5
The value you enter is a positive number.

Then, as shown above, "The value you entered is a positive number." Is displayed, and the program ends. Also, even in the same program, if you enter 0 and a negative number, nothing will be displayed as follows.

Execution result 2.(If you enter 0 and a negative number)


Enter a number:-

Next, the format of the if statement used in conditional branching is explained. The if woman has the following format.

if statement format


if(#Conditional expression)
  #processing
end

When the conditional expression in () is satisfied, the if statement executes the process enclosed in {}. In 1-1.rb, a> 0, that is, a When it is greater than 0, the condition is satisfied and the puts statement in {} is executed. > Is called a comparison operator. The comparison operators are as follows.

Example of use

operator meaning Example of use
> Greater a > 0
>= that's all a >= 0
< Smaller a < 0
<= Less than a <= 0
== equal a == 0
!= Not equal a != 0

From the above, If this program says that the number entered from the keyboard is a positive integer, "The value entered is a positive number." Is displayed, otherwise nothing is displayed. However, if possible, I would like to display some message even when a value other than a positive number is entered. So, let's introduce the mechanism that can do it next.

Qitta

if ~ else statement

・ Sample program First of all, please see the following program.

1-2.rb


def calculation
 puts "Enter a number"
 a = gets.to_i
 if(a > 0) 
   puts "The value you enter is a positive number.\n"  #Execute if it is a positive number
 else
   puts "The value you enter is not a positive number.\n" #Run if 0 or negative number
 end
end

The execution result of this program does not change if you enter a positive integer after execution. However, if you enter a negative number, the execution result will be as follows.

Execution result 3.(If you enter 0 and a negative number)


Enter a number:-1
The value you enter is not a positive number.

else statement

The if ~ else statement that appears here has the following format. if ~ else statement format

if(Conditional expression)
  #Processing ①
else
  #Processing ②
end

When the conditional expression in () of the if statement is satisfied, process (1) is executed in the same way as when the if statement is used alone. However, in other cases, that is, if the conditional expression is not satisfied, the process (2) below the else statement is executed. Therefore, this program will output "The value you entered is not a positive number" if a is not a positive integer, that is, 0 or a negative value. Flow chart below Qiita

elsif statement

Sample program When using if and else, I was able to write the processing when a certain condition is satisfied and when it is not. However, in reality, there are many cases where the condition consists of multiple conditions. What should I do in such a case? At that time, elsif is useful. First, let's take a look at the following sample.

1-3.rb


def math
 puts "1~Please enter a value of 3:"
 num = gets.to_i
 if(num == 1) 
   puts "one\n"  #Processing when num is 1
 elsif(num == 2) 
   puts "two\n" #Processing when num is 2
 elsif(num == 3) 
   puts "three\n" #Processing when num is 3
 else
   puts "Inappropriate value.\n" #Processing when other values are entered
 end
end
Execution result 1(If a value from 1 to 3 is entered)
Please enter a value from 1 to 3:1
one
Execution fruit delivery 2(If a value other than that is entered)
Please enter a value from 1 to 3:4
Inappropriate value.

As a result of execution, if you enter 1, it will be displayed as one, if you enter 2, it will be displayed as two, and if you enter 3, it will be displayed as three. If you enter any other value, "Please enter a value from 1 to 3" with this if and else alone You can only execute processing when one condition is met and when it is not. With elsif, it is possible to separate cases for multiple conditions. The format of the if statement including elsif is as follows.

if ~ elsif ~ else statement format

if(Conditional expression ①)
Processing ①
elsif(Conditional expression ②)
Processing ②
else
Processing ③
end

If the conditional expression (1) is satisfied, the process (1) is executed. If the conditional expression (2) is satisfied, the process (2) is executed. If neither of the conditions is satisfied, the process (3) is executed. You can add any number </ font> of elsif after if. So you can add as many conditions as you like. for that reason. The processing flow of 1-3.rb is as follows.

Qiita

Complex if statement

Sample program

Next, let's combine this knowledge to create a more complicated if syntax. First, take a look at the sample below.

list1-4.rb



 def main
  #Enter the dice roll
  puts "Please enter a number from 1 to 6:"
  dice = gets.to_i
  #Find out if the value is within the dice roll
  if(1 <= dice && dice <= 6)
    if(dice == 2 || dice == 4 || dice == 6)
     #Divide the processing according to whether the dice roll is even or odd
       puts "Ding (a butterfly)\n"   #If it is an even number, it is a butterfly.
    else
       puts "Half (Han)\n"  #If it is odd, half (han)
    end
  else
    puts "It is a number outside the range.\n"
  end
 end

The execution result of this program can be roughly divided into three types. The first is an even number between 1 and 6, That is, if you enter a value such as 2,4,6. As shown below, "It's a butterfly" is displayed and the program ends.

Execution result 1.(2,4,If 6 is entered)


Please enter a number from 1 to 6:2
Ding(Butterfly)is.

Next, also between 1 and 6, if you enter an odd number such as 1,3,5 this time, "Half (Han)." Is displayed and Pro Gram ends.

Execution result 2.(1,3 of,If 5 is entered)


Please enter a number from 1 to 6:5
Ding(Butterfly)is.

Finally, if you enter an integer other than 1 to 6, that is, a number that does not correspond to the dice, "It is a number outside the range." Is displayed and the program ends.

Execution result 3.(When a number outside the range is entered)


Please enter a number from 1 to 6: 10
It is a number outside the range.

Recommended Posts

About if statement and branch processing
About Ruby if statement
[Ruby] conditional branch if statement
[Swift] "for-in statement" about iterative processing
if statement
Basics of java basics ② ~ if statement and switch statement ~
Ruby conditional branch processing
About products using crud processing and devise (for beginners)
Link Processing and SQLite
About classes and instances
About gets and gets.chomp
About redirect and forward
10 Corresponds to if statement
Studying Java-Part 10-if statement
About encapsulation and inheritance
hash and each statement
[Ruby] Get a 3-digit integer and conditionally branch using an if statement (digits method)
[Java] Branch and repeat
About Serializable and serialVersionUID
About synchronized and Reentrant Lock
Swift conditional branch statement summary
Java if and switch statements
[Swift] About asynchronous processing "Operation"
Let's understand the if statement!
Common processing and error processing springmvc
About Ruby hashes and symbols
About the same and equivalent
Points for refactoring (if statement)
☾ Java / Iterative statement and iterative control statement
About classes and instances (evolution)
About pluck and ids methods
Consideration about classes and instances
[Ruby] problem with if statement
About Java Packages and imports
About Ruby and object model
About Ruby classes and instances
About instance variables and attr_ *
About self-introduction and common errors
Creating a mixed conditional expression of Rails if statement and unless