第12講 For文以外の様々なループ文
第9話 前判断と後判断の違い
を実現するプログラム例
Do While文の場合
Function f1(m As Long, n As Long, h As Long)
Dim w As Long, i As Long
i = m
While w + i <= n
w = w + i
i = i + h
Wend
f1 = w
End Function
Function f2(m As Long, n As Long, h As Long)
Dim w As Long, i As Long
i = m
While w + i * i <= n
w = w + i * i
i = i + h
Wend
f2 = w
End Function
Function f3(m As Long, n As Long, h As Long)
Dim w As Long, i As Long
i = m
While w + i * i * i <= n
w = w + i * i * i
i = i + h
Wend
f3 = w
End Function
Function f4(m As Long, n As Long, h As Long)
Dim w As Long, i As Long
i = m
While w + i * i * i * i <= n
w = w + i * i * i * i
i = i + h
Wend
f4 = w
End Function
Do...Loop While文の場合
Function f1(m As Long, n As Long, h As Long)
Dim w As Long, i As Long
i = m
While w + i <= n
w = w + i
i = i + h
Wend
f1 = w
End Function
Function f2(m As Long, n As Long, h As Long)
Dim w As Long, i As Long
i = m
While w + i * i <= n
w = w + i * i
i = i + h
Wend
f2 = w
End Function
Function f3(m As Long, n As Long, h As Long)
Dim w As Long, i As Long
i = m
While w + i * i * i <= n
w = w + i * i * i
i = i + h
Wend
f3 = w
End Function
Function f4(m As Long, n As Long, h As Long)
Dim w As Long, i As Long
i = m
While w + i * i * i * i <= n
w = w + i * i * i * i
i = i + h
Wend
f4 = w
End Function
解説
結局、今回の場合は前判断、後判断は関係ありませんでした。
判定条件 w + i <= n 等の問題にすぎなかったのです。
前判断と後判断の違いは、
最初の条件が偽であった場合に、
前判断は1回もループ処理が行われないのに対して、
後判断は偽でも1回はループ処理が行われることに、
にすぎないのです。
eclipse c++ 入門
魔方陣 数独で学ぶ VBA 入門
数独のシンプルな解き方・簡単な解法の研究
vc++講義へ
excel 2013 2010 2007 vba入門へ
VB講義基礎へ
初心者のための世界で一番わかりやすいVisual C++入門基礎講座へ
初心者のための世界で一番わかりやすいVisual Basic入門基礎講座へ
専門用語なしの C言語 C++ 入門(Visual C++ 2010で学ぶ C言語 C++ 入門)
専門用語なしの excel vba マクロ 入門 2013 2010 2007 対応講義 第1部
eclipse java 入門へ
excel 2016 vba 入門へ
小学生からエンジニアまでのRuby入門へ
本サイトトップへ