[Ruby] Ruby 시작하기 -7 /값비교/IF/While
출처 : https://opentutorials.org/module/11/254
에 정리되어진 글을 보며 직접 공부하여, 다시 요약 & 정리한 글입니다.
근데 정말 별다를게 없구나.-_- Ruby
(1) 값 비교
맞으면 true, 틀리면 false를 return한다.
알파벳의 경우 abc...z순으로 순서를 체킹한다.
puts 1 > 2 puts 1 < 2
puts 5 >= 5 puts 5 <= 4
puts 1 == 1 puts 2 != 1
puts 'cat' < 'dog' puts 'Zoo' < 'ant' |
#OUTPUT
false true true false true true true true |
(2) IF
조건이 맞으면 실행한다.
조건이 맞지 않는다면, else문을 실행한다.
puts 'I am a fortune-teller. Tell me your name:' name = 'AAA' if name == 'AAA' puts 'I see great things in your future.' else puts 'Your future is... Oh my! Look at the time!' puts 'I really have to go, sorry!' end |
#OUTPUT
I am a fortune-teller. Tell me your name: I see great things in your future. |
(3) 순환 while
while 뒤의 조건이 true라면 실행하며
조건이 false가 되는순간 빠져나온다.
command = ''
while command != 'bye' puts command command = gets.chomp end
puts 'Come again soon!' |
#OUTPUT
Hello? Hello? Hi! Hi! Very nice to meet you. Very nice to meet you. Oh... how sweet! Oh... how sweet! Bye #Bye라면 while을 빠져나온다. Come again soon! |