メインコンテンツまでスキップ

Java: OR論理演算子の評価条件

· 約2分
Yu Sasaki
Enterprise Security Manager / Advisor

以前、OR演算の2つのオペランドが両方評価されるか否かがあやふやだったので以下のコードを以て改めて確認してみます。

public class Sample1 {
public static void main(String[] args) {
int i = 5, j = 10, k =15;
if ((i++ < j) | (k-- > j))
System.out.println("values of i: " + i + " values of k: " + k);
if ((i < j) || (--k > j))
System.out.println("values of k: " + k);
}
}

実行結果は

values of i: 6 values of k: 14
values of k: 14

となります。 1つめのif文で使われている演算子はビット論理OR演算子で左右の両オペランドを評価します。よって式i++とk--が評価されているため結果はvalues of i: 6 values of k: 14となります。 2つめのif文で使われているのは短絡論理OR演算子で、評価順序は||の_左側のオペランドを先ず評価し_、それがtrueなら_右側は評価せず_(ORは片方が真ならもう片方の結果にかかわらず真ですから)、_falseなら評価_します。よって、if ((i < j) || (--k > j))ではi < jがtrueとなるため--k > jは評価されずkはデクリメントされません。よってkの値は変わらず、上記のような実行結果となります。

Words of the Day - Friday March 27, 2009

· 約1分
Yu Sasaki
Enterprise Security Manager / Advisor

28. It goes without saying that her condition is fatal. 29. Encountering a inevitable problem, we should solve it steadily. 30. What is juvenile delinquency caused by in the school? 31. "In all likelihood, my intuition will be right under these circumstances!" "huh..." "What!?" "Oh, Nothing in particular." 32.This seat is vacant because it's the priority seat. 33. If anything, I'm making much of quality over quantity.

Words of the Day - Thursday March 26, 2009

· 約1分
Yu Sasaki
Enterprise Security Manager / Advisor

25. Why did you compromise on your opinion. 26. Typically, competent engineers have a decent custom. 27. Reflecting the past dismal consequences, we'll execute the concrete idea.

情報処理: 覚え書き

· 約5分
Yu Sasaki
Enterprise Security Manager / Advisor

1. 主なデータの管理・取り出し方法

  1. LIFO (Last-In First-Out): キューなどに使われる。
  2. FIFO (First-In First-Out): スタック。再帰的な処理をする際、実行中の状態を保存しておく為にも使われる。
  3. LFU (Least Frequently Used): 参照頻度が最も少ないものを取り出す。
  4. LRU (Least Recently Used): 未使用時間が最も長いものを取り出す。

Words of the Day - Saturday March 21, 2009

· 約1分
Yu Sasaki
Enterprise Security Manager / Advisor

12. That blog article combines prose with poetry. 13. The up-to-date patch of secrity came out last week. 14. Do you know the well-known fairy tale as a fable. 15. He feels the generation gap toward a contemporary. 16. I use next to nothing about this function. 17. At times I encounter the fabulose sculpture in the museum.

Excel VBA: Hello Worldと変数、配列の宣言

· 約3分
Yu Sasaki
Enterprise Security Manager / Advisor

ポツポツ使う場面が増えてきたので、備忘録代わりにまとめていこうかな(気が向いたときに)。

VBAでHello World

メニューバーからツール→マクロ→Visual Basic Editor(Alt+F11)をクリックするとエディタが起動します。最初は何もファイルが開かれていないと思うので、まず、メニューバーから挿入→標準モジュールをクリックして新規作成し、試しに以下のコードを打ち込んで実行してみます。

Sub helloWorld()
MsgBox "Hello, VBA"
End Sub

入力し終わったらメニューの実行→Sub/ユーザー フォームの実行(F5)をクリックするとスクリプトが実行されます。結果は下図のようになります。 vba_hello_world ダイアログボックス内に文字列が表示されていますね。仮に下記のようにした場合は、

Sub helloWorld()
MsgBox "Hello, VBA"
MsgBox "Hello, Excel"
End Sub

一つ目の"Hello, VBA"のダイアログのOKを押した後、"Hello, Excel"のダイアログが表示されます。どうやらMsgBox関数はブロック関数みたい。

変数の宣言 - Dim

Sub varTest()
Dim num1 As Integer
Dim str1 As String
Dim num2 As Integer, str2 As String
num1 = 10
str1 = "文字列1"
MsgBox num1 & " " & str1
MsgBox num2 & " " & str2, vbInformation, "デバッグプリント"
End Sub

実行結果は vba_var01 vba_var02 変数宣言の一般式は

Dim <変数名> As <型>

のようにDim~Asステートメントを用います。 また、MsgBox関数のvbinformationを指定することで情報メッセージアイコンを表示します。ちなみに、出力する複数の文字列の連結は「&」を用います。

定数の宣言 - Const

Const <定数名> As <型> = <値>

変数宣言と似てますね。異なるところは、DimがConstに変わり、宣言と値の初期化を同時に行うことですかね。変数も値の初期化は可能ですが、それは任意です。

オブジェクトの参照を変数に格納 - Set

オブジェクトの参照を代入する際にはSetステートメントを使用します。以下のコードは1つ目のワークシート名を表示しています。

Sub objectTest()
Dim tmpSheet As Worksheet
Set tmpSheet = Worksheets(1)
MsgBox tmpSheet.Name, vbInformation
End Sub

実行結果は vba_var_set01 一般式は

Set <オブジェクト型変数> = <オブジェクト>

コンパイルエラー、「SubまたはFunctionが定義されていません」の原因

大抵ミスタイプです。Worksheets(1)と打つところをWorksheet(1)としてしまうとか。変数宣言のミスでもデフォルトでは「SubまたはFunctionが~」と出力されるので注意。

配列の宣言

Dim <配列名>(<要素数>) As <型>

Sub objectTest()
Dim arr(3) As Integer
arr(0) = 99
MsgBox arr(0)
End Sub

ダイアログには99が表示されます。配列の添え字は0から数えますが、下記の文をモジュールの宣言セクションに書くことで1から数えられます。

Option Base 1

Option Base 1
Sub objectTest()
Dim arr(3) As Integer
arr(1) = 99
MsgBox arr(1)
End Sub

実行結果は同じく99です。

Words of the Day - Friday March 20, 2009

· 約1分
Yu Sasaki
Enterprise Security Manager / Advisor

3. We are fair in terms of drawing a card at random in a sense. 4. He stood on leaning against the wall. 5. A girl passed by me giving off a subtle scent of powder smoke. 6. It reminded me of the awful case. 7. I was so beside myself that I'm soaked of sweat. 8. Ichiro is the mental pillar of Japan. 9. The candy that I receive tastes terrific. 10. Regardless of wearing a mask, this room stinks. 11. Do you know such perfume that includes the potential harmful effect that can scarcely tell fact from fiction.