Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      if statement

      From cppreference.com
      <c‎ |language
       
       
       
      Statements
      Labels
      Expression statements
      Compound statements
      Selection statements
      if
      Iteration statements
      Jump statements
       

      Conditionally executes code.

      Used where code needs to be executed only if some condition is true.

      Contents

      [edit]Syntax

      attr-spec-seq (optional)if (expression)statement-true (1)
      attr-spec-seq (optional)if (expression)statement-trueelsestatement-false (2)
      attr-spec-seq -(C23) an optional list ofattributes, applied to theif statement
      expression - anexpression of any scalar type
      statement-true - anystatement (often a compound statement), which is executed ifexpression compares not equal to0
      statement-false - anystatement (often a compound statement), which is executed ifexpression compares equal to0

      [edit]Explanation

      expression must be an expression of anyscalar type.

      Ifexpression compares not equal to the integer zero,statement-true is executed.

      In the form(2), ifexpression compares equal to the integer zero,statement-false is executed.

      As with all other selection and iteration statements, the entire if-statement has its own block scope:

      enum{a, b}; int different(void){if(sizeof(enum{b, a})!=sizeof(int))return a;// a == 1return b;// b == 0 in C89, b == 1 in C99}
      (since C99)

      [edit]Notes

      Theelse is always associated with the closest precedingif (in other words, ifstatement-true is also anif statement, then that innerif statement must contain anelse part as well):

      int j=1;if(i>1)if(j>2)printf("%d > 1 and %d > 2\n", i, j);else// this else is part of if (j > 2), not part of if (i > 1)printf("%d > 1 and %d <= 2\n", i, j);

      Ifstatement-true is entered through agoto,statement-false is not executed.

      [edit]Keywords

      if,else

      [edit]Example

      Run this code
      #include <stdio.h> int main(void){int i=2;if(i>2){printf("i > 2 is true\n");}else{printf("i > 2 is false\n");}     i=3;if(i==3)printf("i == 3\n"); if(i!=3)printf("i != 3 is true\n");elseprintf("i != 3 is false\n");}

      Output:

      i > 2 is falsei == 3i != 3 is false

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 6.8.5.2 The if statement (p: 154)
      • C17 standard (ISO/IEC 9899:2018):
      • 6.8.4.1 The if statement (p: 108-109)
      • C11 standard (ISO/IEC 9899:2011):
      • 6.8.4.1 The if statement (p: 148-149)
      • C99 standard (ISO/IEC 9899:1999):
      • 6.8.4.1 The if statement (p: 133-134)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 3.6.4.1 The if statement

      [edit]See also

      C++ documentation forif statement
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/language/if&oldid=183393"

      [8]ページ先頭

      ©2009-2025 Movatter.jp