初めてのPython(10章)
2部ではビルドインオブジェクトのことをやり、3部ではステートメントについて
Pythonプログラムの構造
その他のお勉強
Cのコードブロック
思いっきり勘違い。
(A)と(B)は同じだと思っていたら、(B)とは別で(C)と同じだった。。。
elseは元も内側のifに対応するってことなのか。
「ブロックはちゃんと括弧で括りなさい」っていうのはこういうことが簡単に起きちゃうからなんだね。
(A)
if(input1 == 0) if(input2 == 0) fprintf(stderr, "input1 and input2 is zero\n"); else fprintf(stderr, "input1 is non-zero\n");
(B)
if(input1 == 0) { if(input2 == 0) { fprintf(stderr, "input1 and input2 is zero\n"); } } else { fprintf(stderr, "input1 is non-zero\n"); }
(C)
if(input1 == 0) { if(input2 == 0) { fprintf(stderr, "input1 and input2 is zero\n"); } else { fprintf(stderr, "input1 is non-zero\n"); } }
(お試し結果)
[kobakoba0723@fedora13-intel64 ~]$ gcc -o test test.c [kobakoba0723@fedora13-intel64 ~]$ ./test 0 0 input1 and input2 is zero [kobakoba0723@fedora13-intel64 ~]$ ./test 0 1 input1 is non-zero [kobakoba0723@fedora13-intel64 ~]$ ./test 1 0 [kobakoba0723@fedora13-intel64 ~]$ ./test 1 1 [kobakoba0723@fedora13-intel64 ~]$ gcc -o test_bracket test_bracket.c [kobakoba0723@fedora13-intel64 ~]$ ./test_bracket 0 0 input1 and input2 is zero [kobakoba0723@fedora13-intel64 ~]$ ./test_bracket 0 1 input1 is non-zero [kobakoba0723@fedora13-intel64 ~]$ ./test_bracket 1 0 [kobakoba0723@fedora13-intel64 ~]$ ./test_bracket 1 1 [kobakoba0723@fedora13-intel64 ~]$
(test.c)
#include <stdio.h> int main(int argc, char *argv[]) { int input1, input2; input1 = atoi(argv[1]); input2 = atoi(argv[2]); if(input1 == 0) { if(input2 == 0) { fprintf(stderr, "input1 and input2 is zero\n"); } } else { fprintf(stderr, "input1 is non-zero\n"); } return 0; }
(test_bracket.c)
#include <stdio.h> int main(int argc, char *argv[]) { int input1, input2; input1 = atoi(argv[1]); input2 = atoi(argv[2]); if(input1 == 0) { if(input2 == 0) { fprintf(stderr, "input1 and input2 is zero\n"); } else { fprintf(stderr, "input1 is non-zero\n"); } } return 0; }