Slight Grammatical Changes on if-elif-else-statements (#21222)
<!-- Please follow this checklist and put an x in each of the boxes, like this: [x]. It will ensure that our team takes your pull request seriously. --> - [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
This commit is contained in:
committed by
Paul Gamble
parent
dbfab5b118
commit
e4bff66f89
@ -4,8 +4,9 @@ title: If / Elif / Else Statements
|
|||||||
|
|
||||||
## If / Elif / Else Statements
|
## If / Elif / Else Statements
|
||||||
|
|
||||||
The `if`/`elif`/`else` structure is a common way to control the flow of a program, allowing you to execute specific blocks of code depending on the value of some data. If the condition following the keyword `if` evaluates as `True`, the block of code will execute.
|
The `if`/`elif`/`else` structure is a common way to control the flow of a program, allowing you to execute specific blocks of code depending on the value of some data. If the condition following the keyword `if` evaluates as `True`, the block of code will execute:
|
||||||
Note: that parenthesis is not used before and after the condition check as in other programming languages.
|
Note that parenthesis are not used before and after the condition check such as in other languages.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
if True:
|
if True:
|
||||||
print('If block will execute!')
|
print('If block will execute!')
|
||||||
@ -62,7 +63,7 @@ else:
|
|||||||
print("Neither will I!") #this statement does not execute
|
print("Neither will I!") #this statement does not execute
|
||||||
```
|
```
|
||||||
|
|
||||||
*Note: only the first condition that evaluates as `True` will execute. Even though `z > 6` is `True`, the `if/elif/else` block terminates after the first true condition. This means that an `else` will only execute if none of the conditions were `True`.*
|
*Note only the first condition that evaluates as `True` will execute. Even though `z > 6` is `True`, the `if/elif/else` block terminates after the first true condition. This means that an `else` will only execute if none of the conditions prior to it were `True`.*
|
||||||
|
|
||||||
***Nested if statement***
|
***Nested if statement***
|
||||||
|
|
||||||
@ -79,14 +80,14 @@ if x % 2 == 0: # this is how you create a comment and now, checking for even.
|
|||||||
else:
|
else:
|
||||||
print ("The number is not even. So no point checking further.")
|
print ("The number is not even. So no point checking further.")
|
||||||
```
|
```
|
||||||
This was just a simple example for nested if statement. Please feel free to explore more online.
|
This was just a simple example of a nested if statement. Please feel free to explore more online.
|
||||||
|
|
||||||
While the examples above are simple, you can create complex conditions using <a href='https://guide.freecodecamp.org/python/comparisons' target='_blank' rel='nofollow'>boolean comparisons</a> and <a href='https://guide.freecodecamp.org/python/boolean-operations' target='_blank' rel='nofollow'>boolean operators</a>.
|
While the examples above are simple, you can create complex conditions using <a href='https://guide.freecodecamp.org/python/comparisons' target='_blank' rel='nofollow'>boolean comparisons</a> and <a href='https://guide.freecodecamp.org/python/boolean-operations' target='_blank' rel='nofollow'>boolean operators</a>.
|
||||||
|
|
||||||
|
|
||||||
***Inline python if-else statement***
|
***Inline python if-else statement***
|
||||||
|
|
||||||
We can also use if-else statements with inline python functions
|
We can also use if-else statements with inline python functions.
|
||||||
The following example should check if the number is greater or equal than 50, if yes return True:
|
The following example should check if the number is greater or equal than 50, if yes return True:
|
||||||
|
|
||||||
```
|
```
|
||||||
|
Reference in New Issue
Block a user