SlideShare a Scribd company logo
1 of 74
Download to read offline
Ruby for, if, while
技大祭実行委員 情報局
1
for文
for文とは
for文を使うと
処理を繰り返すことができる
for文とは
1から5までを足したらいくつ?
という問題があるとする
for文
1から5までを足したらいくつ?
for文を使わない場合…
total = 1
total = total + 2
total = total + 3
total = total + 4
total = total + 5
for文とは
1から10000までを足したらいくつ?
という問題があるとする
for文
total = 1
total = total + 2
total = total + 9999
total = total + 10000
1から10000までを足したらいくつ?
・
・
・
for文
total = 1
total = total + 2
total = total + 9999
total = total + 10000
1から10000までを足したらいくつ?
・
・
・
大変すぎる!
for文とは
for文を使おう!
for文の文法
for文とは
total = 0
for i in 1..10000 do
total = total + i
end
1から10000までを足したらいくつ?
for文を使うと、
for文とは
total = 0
for i in 1..10000 do
total = total + i
end
1から10000までを足したらいくつ?
for文を使うと、
簡単になった!
for文の文法
for i in 1..10000 do
total = total + i
end
forで始まりendで閉じる
※doは省略可能
for文の文法
for i in 1..10000 do
total = total + i
end
forの後に変数を書く
今回の場合、1~10000がiに順番に代入される
for文の文法
for i in 1..10000 do
total = total + i
end
forのinの後に繰り返し回数を書く
今回の場合、1~10000の10000回処理が繰り返される。
for文の文法
for i in 1..10000 do
total = total + i
end
forとendの間に繰り返したい処理を書く
for文の文法
for i in 1..10000 do
total = total + i
end
forとendの間に繰り返したい処理を書く
1~10000の数字を代入する変数名 繰り返し回数
for文の文法
for 変数 in 繰り返し回数 do
処理
end
まとめると、以下のようになる
for文の文法-2重ループ-
for i in 1..10 do
  for j in 1..3 do
  printf(“%d”, i)
  end
end
以下のように書く
for文の文法-2重ループ-
for i in 1..10 do
  for j in 1..3 do
  printf(“%d”, i)
  end
end
iを表示する
for文の文法-2重ループ-
for i in 1..10 do
  for j in 1..3 do
  printf(“%d”, i)
  end
end
3回繰り返す
for文の文法-2重ループ-
for i in 1..10 do
  for j in 1..3 do
  printf(“%d”, i)
  end
end
10回繰り返す
3回繰り返す
for文の文法-2重ループ-
for i in 1..10 do
  for j in 1..3 do
  printf(“%d”, i)
  end
end
10回繰り返す
iを表示する
iを3回表示するのを10回繰り返す
3回繰り返す
for文の文法-2重ループ-
for i in 1..10 do
  for j in 1..3 do
  printf(“%d”, i)
  end
end
10回繰り返す
iを表示する
実行結果
111222333444555666777888999101010
for文の文法-2重ループ-
for 変数A in 繰り返し回数 do
  for 変数B in 繰り返し回数 do
  処理
  end
end
まとめると、以下のようになる
問題(for文)
問題
以下の模様を出力するプログラムを作成せよ
(1) (2)
*****    *
*****    **
*****    ***
*****    ****
*****    *****
余裕があれば(2)も
(1)
*****
*****
*****
*****
*****
解答例
for i in 1..5 do
for j in 1..5 do
printf("*")
end
printf("n")
end
(2)
*
**
***
****
*****
for i in 1..5 do
for j in 1..i do
printf("*")
end
printf("n")
end
2
while文
while文とは
while文を使うと
処理を繰り返すことができる
(for文と同じ!)
for文との違い
for文
繰り返し回数を
指定する
while文
繰り返す条件を
指定する
while文の文法
while文の文法
for i in 1..10 do
  printf(“%d”, i)
end
以下のように書く
i = 0
while i <= 10 do
  printf(“%d”, i)
i = i + 1
end
for文だとこんな感じ
while文の文法
for i in 1..10 do
  printf(“%d”, i)
end
以下のように書く
i = 0
while i <= 10 do
  printf(“%d”, i)
i = i + 1
end
for文だとこんな感じ
出力結果:12345678910
while文の文法
for 変数名 in 繰り返す回数 do
  繰り返す処理
end
以下のように書く
変数名
while 繰り返す条件 do
  繰り返す処理
end
for文だとこんな感じ
問題(while文)
問題
次の数字が何桁か判定するプログラムを作成せよ
1
1234
13
245
10000
次ページにヒントあり
問題
次の数字が何桁か判定するプログラムを作成せよ
1
1234
13
245
10000
ヒント
10で何回割れるか考える
解答例
p = 1234 # ここは数値によって変える
n = 0
while p > 1 do
p = p / 10.0
n = n + 1
end
printf("%dn", n)
nums = [1234, 13, 245, 10000]
for p in nums do
n = 0
printf("p = %dn", p)
while p >= 1 do
p = p / 10.0
n = n + 1
end
printf("%dnn", n)
end
解答例1 解答例2
3
if文
if文とは
if文を使うと
ある条件のときのみ実行する処理
を実現できる
if文とは
1~10までの整数で
3の倍数のみ表示する
という処理を実装したいとき
if文
for i in 1..10 do
  iが3の倍数のときだけprintf(“%dn”, i)
end
このように書けると便利
if文を使おう!
if文の文法
if文の文法
for i in 1..10 do
if i % 3 == 0
printf(“%dn”, i)
end
end
今回の場合はこのように書く
if文の文法
for i in 1..10 do
if i % 3 == 0
printf(“%dn”, i)
end
end
この部分がif文
ifで始まって
endで終わる
if文の文法
for i in 1..10 do
if i % 3 == 0
printf(“%dn”, i)
end
end
処理を実行するための条件を書く
if文の文法
for i in 1..10 do
if i % 3 == 0
printf(“%dn”, i)
end
end
条件を満たした時に
実行する処理を書く
if文の文法
for i in 1..10 do
if i % 3 == 0
printf(“%dn”, i)
end
end
処理を実行するための条件
実行する処理
if文の文法
for i in 1..10 do
if 条件式
実行したい処理
end
end
問題(if文)
問題
解答例
おまけ:条件式
条件式
i < 1 # i が1よりも小さいとき
i > 1 # i が1よりも大きいとき
i <= 1 # i が1以下のとき
i >= 1 # i が1以上のとき
数の大小
i > 1 && i < 10 # i が1よりも大きくかつ10よりも小さいとき
i < 1 || i > 10 # i が1よりも小さいかもしくは10よりも大きいとき
複数の条件
i == 1 # i が1のとき
i != 1 # i が1でないとき
一致・不一致
else文
else文とは
1~10までの整数で
3の倍数のみ表示する
それ以外は * を表示する
という処理を実装したいとき
else文を使おう!
elseとはif文の条件でないときに
処理を実行してくれる
else文の文法
else文の文法
for i in 1..10 do
if i % 3 == 0
printf(“%dn”, i)
else
printf(“ * ”)
end
end
今回の場合はこのように書く
この部分を追加
else文の文法
for i in 1..10 do
if i % 3 == 0
printf(“%dn”, i)
else
実行したい処理
end
end
else if 文
else if 文とは
1~10までの整数で
3の倍数のみ表示する
5の倍数も表示する
それ以外は * を表示する
という処理を実装したいとき
else if 文を使おう!
else if 文 では
条件Aを満たさない
かつ
条件Bを満たすとき
に処理を実行させる
else if 文の文法
else if 文とは
1~10までの整数で
① 3の倍数のみ表示する
② 5の倍数も表示する
③ それ以外は * を表示する
という処理を実装する
else if 文の文法
for i in 1..10 do
if i % 3 == 0
printf(“%dn”, i)
elsif i % 5 == 0
printf(“%dn”, i)
else
printf(“ * ”)
end
end
今回の場合はこのように書く
この部分を追加
①
②
③
if, else, else if を全て使ったときの文法
for i in 1..10 do
if 条件A
条件Aを満たしたときに実行
elsif 条件B
条件Aを満たさず条件Bを満たした時に実行
else
すべての条件を満たさない時に実行
end
end
①
②
③
問題(if, else, else if文)
問題
FizzBuzz問題
– 1から100までの数をプリントするプログラム を書け。
ただし3の倍数のときは数の代わりに 「Fizz」と、
5の倍数のときは「Buzz」とプリントし、
3と5両方の倍数の場合には「FizzBuzz」とプリントすること。
解答例
for i in 1..100
if i % 3 == 0 and i % 5 == 0
puts ‘fizzbazz’
elsif i % 3 == 0
puts ‘fizz’
elsif i % 5 == 0
puts ‘buzz’
else
puts i
end
end

More Related Content

Featured

Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 

ruby for_if_while