Loose-Info.com
Last Update 2023/09/28
TOP - 各種テスト - C++ - 文

概要

ラベル
空(null)文
ブロック
if文
switch文
while文
do文
for文
break文
continue文
return文
goto文


ラベル


sample.cpp
#include <iostream> int main() { std::cout << "ラベル" << std::endl; int flg = 0; // ラベル label_0 label_0: if (flg == 1) { // goto文によるラベル label_1 へのジャンプ goto label_1; } std::cout << "(1) "; if (flg == 2) { // goto文によるラベル label_2 へのジャンプ goto label_2; } std::cout << "(2) "; if (flg >= 3) { // goto文によるラベル label_3 へのジャンプ goto label_3; } // ラベル label_1 label_1: std::cout << "(3) "; // ラベル label_2 label_2: switch (flg) { // switch文 caseラベル case 0: std::cout << "(case 0) "; break; // switch文 caseラベル case 1: std::cout << "(case 1) "; break; // switch文 defaultラベル default: std::cout << "(case default) "; } flg++; // goto文によるラベル label_0 へのジャンプ goto label_0; // ラベル label_3 label_3: std::cout << "end" << std::endl; }

実行結果
$ gcc -Wall sample.cpp -lstdc++ $ ./a.out ラベル (1) (2) (3) (case 0) (3) (case 1) (1) (case default) (1) (2) end

空(null)文


sample.cpp
#include <iostream> int main(int argc, char *argv[]) { char *p; if (argc > 1) { p = argv[1]; while (*p != '\0' ? (std::cout << static_cast<char>(*(p++)+1)), 1 : 0) ; // 空(null)文 } std::cout << std::endl; }

実行結果
$ gcc -Wall sample.cpp -lstdc++ $ ./a.out 12345 23456 $ ./a.out ABCDE BCDEF $ ./a.out abcde bcdef

ブロック


sample.cpp
#include <iostream> int main() { int i = 0; int j = 100; for (i=0; i<3; i++) { // ブロック(1) int j = i * 3; // ブロックスコープ変数 std::cout << "ブロック(1) j = " << j << std::endl; } if (i >= 2) { // ブロック(2) int j = i * 5; // ブロックスコープ変数 std::cout << "ブロック(2) j = " << j << std::endl; } { // ブロック(3) j = j + 100; // スコープはブロック外 std::cout << "ブロック(3) j = " << j << std::endl; } { // ブロック(4) int j = i * 10; // ブロックスコープ変数 std::cout << "ブロック(4) j = " << j << std::endl; } std::cout << "ブロック外 j = " << j << std::endl; std::cout << std::endl; }

実行結果
$ gcc -Wall sample.cpp -lstdc++ $ ./a.out ブロック(1) j = 0 ブロック(1) j = 3 ブロック(1) j = 6 ブロック(2) j = 15 ブロック(3) j = 200 <--- ブロック外の「j」の値を使用 ブロック(4) j = 30 ブロック外 j = 200 <--- ブロック外の「j」の値を使用

if文


sample.cpp
#include <iostream> #include <string> int main(int argc, char *argv[]) { // 引数の個数による分岐 if (argc > 1) { // 引数が有る場合に実行されるブロック char *p = argv[1]; if ((*p >= '0') && (*p <= '9')) { // 1文字目が数字の場合に実行されるブロック std::cout << "数値で始まる 数値部分 ---> " << std::stoi(p) << std::endl; } else if ((*p == '-') || (*p == '+')) { // 1文字目が符号の場合に実行されるブロック if ((*(p + 1) >= '0') && (*(p + 1) <= '9')) { // 1文字目が符号かつ2文字目が数字の場合に実行されるブロック std::cout << "符号で始まる 符号数値部分 ---> " << std::stoi(p) << std::endl; } } else { // 数値関連以外の文字で始まる文字列の場合に実行されるブロック std::cout << "数値関連以外で始まる文字列 : " << p << std::endl; } } else { // 引数が無い場合に実行されるブロック std::cout << "引数無し" << std::endl; } }

実行結果
$ gcc -Wall sample.cpp -lstdc++ $ ./a.out 引数無し $ ./a.out 123zzz 数値で始まる 数値部分 ---> 123 $ ./a.out -123zzz 符号で始まる 符号数値部分 ---> -123 $ ./a.out aaa123zzz 数値関連以外で始まる文字列 : aaa123zzz

switch文


sample.cpp
#include <iostream> #include <string> int main(int argc, char *argv[]) { if (argc > 1) { std::string sarg = argv[1]; for (int i=0; i < static_cast<int>(sarg.length()); i++) { char& c = sarg[i]; // switch文による分岐処理 switch (c) { // 文字cが「-」の場合は「+」に書き換え case '-': std::cout << "+"; break; // 文字cが「+」の場合は「-」に書き換え case '+': std::cout << "-"; break; // 文字cが「*」「/」「%」の場合は「_」に書き換え case '*': case '/': case '%': std::cout << "_"; break; // その他の場合はそのまま出力 default: std::cout << c; } } std::cout << std::endl; } else { std::cout << "引数無し" << std::endl; } }

実行結果
$ gcc -Wall sample.cpp -lstdc++ $ ./a.out 123-456 123+456 $ ./a.out 123*456 123_456 $ ./a.out 123//456 123__456 $ ./a.out -123*+3-zzz +123_-3+zzz

while文


sample.cpp
#include <iostream> #include <string> #include <iomanip> int main(int argc, char *argv[]) { int i, j; if (argc > 1) { // 各引数をwhileループでチェック i = 0; while (++i < argc) { if ((*argv[i] < '0') || (*argv[i] > '9')) { std::cout << "引数が不正" << std::endl; std::exit(1); } } // 制御式がtrueである限りループ i = 0; while (i++ < std::stoi(argv[1])) { std::cout << std::setw(2) << std::setfill('0') << i << std::endl; if (argc > 2) { j = 0; // 無限ループ int m = std::stoi(argv[2]); while (1) { if (++j > m) { break; } std::cout << " " << std::setw(2) << std::setfill('0') << j << std::endl; } } } std::cout << std::endl; } else { std::cout << "引数無し" << std::endl; } }

実行結果
$ gcc -Wall sample.cpp -lstdc++ $ ./a.out 3 3 01 01 02 03 02 01 02 03 03 01 02 03

do文


sample.cpp
#include <iostream> #include <string> #include <iomanip> int main(int argc, char *argv[]) { int i, m; if (argc > 1) { if ((*argv[1] < '0') || (*argv[1] > '9')) { std::cout << "引数が不正" << std::endl; std::exit(1); } i = 0; m = std::stoi(argv[1]); // ループ開始 do { std::cout << std::setw(2) << std::setfill('0') << i << std::endl; // 制御式がtrueである限りループ } while (i++ < m); std::cout << std::endl; } else { std::cout << "引数無し" << std::endl; } }

実行結果
$ gcc -Wall sample.cpp -lstdc++ $ ./a.out 3 00 01 02 03

for文


sample.cpp
#include <iostream> #include <string> #include <iomanip> int main(int argc, char *argv[]) { if (argc > 1) { // 各引数をforループでチェック for (int i=1; i < argc; i++) { if ((*argv[i] < '0') || (*argv[i] > '9')) { std::cout << "引数が不正" << std::endl; std::exit(1); } } // iの初期化 ; 制御式 ; iの加算 for (int i=0; i < std::stoi(argv[1]); i++) { std::cout << std::setw(2) << std::setfill('0') << i << std::endl; if (argc > 2) { // jの宣言・初期化 ; 制御式 ; jの加算 for (int j=0; j < std::stoi(argv[2]); j++) { std::cout << " " << std::setw(2) << std::setfill('0') << j << std::endl; } } } std::cout << std::endl; int i = 100; // 無限ループ for (;;) { std::cout << std::setw(4) << std::setfill('0') << i << std::endl; if (++i > std::stoi(argv[1]) + 100) { break; } } std::cout << std::endl; int a[] = {1, 2, 3}; // 範囲ベースのforループ // 範囲内の要素が代入・参照される変数の宣言 : 範囲の対象となる配列やクラス等 for (int& n : a) { std::cout << n << std::endl; } } else { std::cout << "引数無し" << std::endl; } }

実行結果
$ gcc -Wall sample.cpp -lstdc++ $ ./a.out 3 3 00 00 01 02 01 00 01 02 02 00 01 02 0100 0101 0102 0103 1 2 3

break文


sample.cpp
#include <iostream> int main() { int i; for (i=0; i<3; i++) { if (i == 1) { std::cout << "for break" << std::endl; // for文終了へ break; } std::cout << "for " << i << std::endl; } i = 0; while (i++ < 3) { if (i == 2) { std::cout << "while break" << std::endl; // while文終了へ break; } std::cout << "while " << i << std::endl; } switch (i) { case 1: std::cout << "switch break" << std::endl; // switch文終了へ break; default: std::cout << "switch default" << std::endl; } }

実行結果
$ gcc -Wall sample.cpp -lstdc++ $ ./a.out for 0 for break while 1 while break switch default

continue文


sample.cpp
#include <iostream> int main() { for (int i=0; i<4; i++) { std::cout << "i = " << i << std::endl; if (i == 0) { // (1)~(3)は表示せずに次のループへジャンプ continue; } std::cout << "(1)" << std::endl; if (i == 1) { // (2)、(3)は表示せずに次のループへジャンプ continue; } std::cout << "(2)" << std::endl; if (i == 2) { // (3)は表示せずに次のループへジャンプ continue; } std::cout << "(3)" << std::endl; } }

実行結果
$ gcc -Wall sample.cpp -lstdc++ $ ./a.out i = 0 i = 1 (1) i = 2 (1) (2) i = 3 (1) (2) (3)

return文


sample.cpp
#include <iostream> void func1() { std::cout << "func1" << std::endl; // 型voidの関数のため式を伴わないreturn文(省略可) return; } int func2() { // int型の値を返すreturn文 return 0; } int func3(int x, int y) { // int型の式を伴うreturn文 return x + y; } int main() { func1(); std::cout << "func2() = " << func2() << std::endl; std::cout << "func3() = " << func3(1, 2) << std::endl; }

実行結果
$ gcc -Wall sample.cpp -lstdc++ $ ./a.out func1 func2() = 0 func3() = 3

goto文


sample.cpp
#include <iostream> int main() { for (int i=0; i<4; i++) { std::cout << "i = " << i << std::endl; if (i == 0) { // ループ内でのジャンプ goto label1; } if (i == 1) { // ループ内でのジャンプ goto label2; } if (i == 2) { // ループ外へのジャンプ goto label3; } // ループ内のラベル label1: std::cout << "label1" << std::endl; // ループ内のラベル label2: std::cout << "label2" << std::endl; } // ループ外のラベル label3: std::cout << "label3" << std::endl; }

実行結果
$ gcc -Wall sample.cpp -lstdc++ $ ./a.out i = 0 label1 label2 i = 1 label2 i = 2 label3

実行環境

GNU bash, version 5.1.16
GCC-12.2.0
GNU C Library 2.36
GNU Binutils 2.39


コード例・出力内容中の表記

・実行例中の太字表記部分は、コマンドなどの入力された文字列を示します。
・「」や「...」の着色省略表記は、 実際のソースコードや出力内容などを省略加工した部分を示します。