From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/c/language%2Fif.html | 65 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 devdocs/c/language%2Fif.html (limited to 'devdocs/c/language%2Fif.html') diff --git a/devdocs/c/language%2Fif.html b/devdocs/c/language%2Fif.html new file mode 100644 index 00000000..b273c4e8 --- /dev/null +++ b/devdocs/c/language%2Fif.html @@ -0,0 +1,65 @@ +

if statement

Conditionally executes code.

+

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

+

Syntax

+ + +
attr-spec-seq(optional) if ( expression ) statement-true (1)
attr-spec-seq(optional) if ( expression ) statement-true else statement-false (2)
+ + + + +
attr-spec-seq - (C23)optional list of attributes, applied to the if statement
expression - an expression of any scalar type
statement-true - any statement (often a compound statement), which is executed if expression compares not equal to ​0​
statement-false - any statement (often a compound statement), which is executed if expression compares equal to ​0​

Explanation

expression must be an expression of any scalar type.

+

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

+

In the form (2), if expression 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 == 1
+    return b; // b == 0 in C89, b == 1 in C99
+}
(since C99)

Notes

The else is always associated with the closest preceding if (in other words, if statement-true is also an if statement, then that inner if statement must contain an else 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);

If statement-true is entered through a goto, statement-false is not executed.

+

Keywords

if, else

+

Example

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

Output:

+
first is false
+i == 3
+i != 3 is false

References

See also

+
C++ documentation for if statement
+

+ © cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
+ https://en.cppreference.com/w/c/language/if +

+
-- cgit v1.2.3