Files
freeCodeCamp/guide/english/c/if/index.md
Miftah Mizwar 8eb754d4eb Update wrong output of the example of code (#22519)
* [x]  I have read [freeCodeCamp's contribution guidelines](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/CONTRIBUTING.md).
* [x]  My pull request has a descriptive title (not a vague title like `Update index.md`)
* [x]  My pull request targets the `master` branch of freeCodeCamp.
* [x]  None of my changes are plagiarized from another source without proper attribution.
* [x]  My article does not contain shortened URLs or affiliate links.

If your pull request closes a GitHub issue, replace the XXXXX below with the issue number.

Closes #XXXXX
2018-11-13 01:36:14 -05:00

65 lines
1.2 KiB
Markdown

---
title: If
---
# If
The if statement executes different blocks of code based on conditions.
```
if (condition) {
// Do something when `condition` is true
}
else {
// Do something when original if `condition` is false
}
```
When `condition` is true, code inside the `if` section executes, otherwise `else` executes. Sometimes you would need to add a second condition. For readability, you should use a `else if` rather than nesting `if` statements.
```
if (condition) {
// Do something if `condition` is true
}
else if (anotherCondition) {
// Do something if `anotherCondition` is true
}
else {
// Do something if `condition` AND `anotherCondition` is false
}
```
Note that the `else` and `else if` sections are not required, while `if` is mandatory.
## Example
```
#include <stdio.h>
int main () {
// Local variable definition
int a = 100;
// Check the boolean condition
if(a < 5) {
// If condition is true then print the following
printf("a is less than 5!\n" );
}
else {
// If condition is false then print the following
printf("a is not less than 5!\n" );
}
printf("Value of a is : %d\n", a);
return 0;
}
```
## Output
```
-> a is not less than 5!
-> Value of a is : 10
```