※「if」文を抜けるというのは、どこかの条件式が真と評価された場合はそれ以降の条件式を評価せずに「if」文の次へ進むということです。 注意する点としては「else if」の条件式はそれよりも前に評価された全ての条件式が偽だった場合のみ評価されるということです。上記のサンプルのような場合ですと、それぞれの条件式だけを見ればどちらの条件式も真となりますが、2番目の条件式は1番目の条件式が偽だった場合のみ評価されます。 では簡単なサンプルプログラムを作成して試してみます。 「if」文では単独の条件分岐だけではなく複数の条件分岐を使って処理を分けることが出来ます。これには「if .. else if .. else」文を使用します。書式は次の通りです。

「else if」は必要な数だけ記述することが出来ます。また全ての条件式が偽だった場合に実行する処理として「else」の次のブロック内に処理を記述できます。「else」の部分は必要無ければ記述しなくても構いません。 「else if」は必要な数だけ記述することが出来ます。また全ての条件式が偽だった場合に実行する処理として「else」の次のブロック内に処理を記述できます。「else」の部分は必要無ければ記述しなくても構いません。 #if、#elif、#else、および #endif ディレクティブ (C/c + +)それ以外の場合は、エラーメッセージが生成されます。条件付きコンパイル ディレクティブがインクルード ファイルに含まれている場合も同じ条件を満たす必要があります。つまり、インクルード ファイルの終わりに対になる条件付きコンパイル ディレクティブが必要です。When conditional-compilation directives are contained in include files, they must satisfy the same conditions: There must be no unmatched conditional-compilation directives at the end of the include file.Macro replacement is done within the part of the line that follows an プリプロセッサは、指定されたいずれかのThe preprocessor selects one of the given occurrences of プリプロセッサによって選択されたテキスト ブロックだけがコンパイルされます。Only text blocks selected by the preprocessor are compiled.プリプロセッサは、true (0 以外) の定数式が見つかるまで、各 It selects all text (including other preprocessor directives beginning with 式には整数型を指定する必要があり、整数定数、文字定数、およびExpressions must have integral type and can include only integer constants, character constants, and the ターゲット環境は、整数のすべての範囲を表すことができない可能性があります。The target environment may be unable to represent all ranges of integers.トランスレーターは、ターゲット環境とは別のコード値のセットに文字定数を翻訳できます。The translator can translate character constants to a set of code values different from the set for the target environment.ターゲット環境のプロパティを確認するには、その環境用にビルドされたアプリを使用して、制限の値を確認しTo determine the properties of the target environment, use an app built for that environment to check the values of the 式は環境に対してクエリを実行することはできません。また、対象のコンピューターで実装の詳細から分離された状態を維持する必要があります。The expression must not query the environment, and must remain insulated from implementation details on the target computer.This constant expression is considered true (nonzero) if the それ以外の場合、条件は False (0) です。空のテキストとして定義された識別子は、定義されていると見なされます。An identifier defined as empty text is considered defined.どちらの識別子も定義されていない場合、次の例の条件付きコンパイル ステートメントは、The conditional compilation statements in the following example assume a previously defined symbolic constant named 条件付きコンパイルの一般的な用途は、同じヘッダー ファイルの多重インクルードを防ぐことです。A common use for conditional compilation is to prevent multiple inclusions of the same header file.C++ では、多くの場合、クラスがヘッダーファイルで定義されているので、次のようなコンストラクトを使用して複数の定義を防ぐことができます。In C++, where classes are often defined in header files, constructs like this one can be used to prevent multiple definitions:The preceding code checks to see if the symbolic constant その場合、ファイルは既に含まれており、再処理は必要ありません。If so, the file has already been included and doesn't need reprocessing.定義されていない場合、EXAMPLE.H が処理されてから、すべてのページ フィードバックを表示 #if、#elif、#else、および #endif ディレクティブ (C/c + +) #if, #elif, #else, and #endif directives (C/C++) 08/29/2019 この記事の内容 #Ifディレクティブは、 #elif、 #else、および #endifディレクティブを使用して、ソースファイルの一部のコンパイルを制御します。 ここではC言語の基本構文 if 〜 else文を紹介します。 if 〜 else文 if 〜 else文は、条件式を満たす「真(true)」の場合、条件を満たさない「偽(false)」の場合の両方で何かしらの処理を行う場合に利用します。 コンパイルが終わりましたら「test5-1」と入力して実行します。 if文は、if、else, else if で構成されます。ifだけあれば、if文になります。 condition が真のとき、A の命令が実行されます。 ifだけあれば、if文になります。 else ifは、1つ以上記述できます。省略可能です。 elseは、1つのみ記述できます。省略可能です。elseのブロック{ }の処理は、どの条件も合致しなかったときに実行されます。 if文のサンプルです。 初心者~中級者の方を対象としたプログラミング方法や開発環境の構築の解説を行うサイトの運営を行っています。