Update wording of index.md (#19559)

Inserted missing words in the article and comments
Updated the capitalization of True and False where necessary to maintain consistency with Python syntax
This commit is contained in:
Alex
2018-10-16 23:55:20 -04:00
committed by Randell Dawson
parent 50e689226e
commit 6a781e06da

View File

@ -1,10 +1,10 @@
--- ---
title: If Elif Else Statements 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 languages. Note that parenthesis is not used before and after the condition check as in other languages.
```python ```python
if True: if True:
@ -17,15 +17,15 @@ x = 5
if x > 4: if x > 4:
print("The condition was true!") #this statement executes print("The condition was true!") #this statement executes
``` ```
> **Tips** : You can use **1** as alternative to **True** and **0** as alternative to **False** > **Tips** : You can use **1** as alternative to **True** and **0** as an alternative to **False**
_Example_: _Example_:
```python ```python
if 1: #true if 1: # 1 evaluates to true
print('If block will execute!') print('If block will execute!')
``` ```
You can optionally add an `else` response that will execute if the condition is `false`: You can optionally add an `else` response that will execute if the condition is `False`:
```python ```python
if not True: if not True:
print('If statement will execute!') print('If statement will execute!')
@ -42,7 +42,7 @@ else:
print("The condition wasn't true!") #this statement executes print("The condition wasn't true!") #this statement executes
``` ```
*Note that there is no condition following the `else` keyword - it catches all situations where the condition was `false`* *Note that there is no condition following the `else` keyword - it catches all situations where the condition was `False`*
Multiple conditions can be checked by including one or more `elif` checks after your initial `if` statement but only one condition will execute: Multiple conditions can be checked by including one or more `elif` checks after your initial `if` statement but only one condition will execute:
@ -59,9 +59,9 @@ 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 were `True`.*
We can also create nested if's for decision making. Before preceding please refer to the <a href='https://guide.freecodecamp.org/python/code-blocks-and-indentation' target='_blank' rel='nofollow'>indentation guide once</a> before preceding. We can also create nested if statements for decision making. Before preceding please refer to the <a href='https://guide.freecodecamp.org/python/code-blocks-and-indentation' target='_blank' rel='nofollow'>indentation guide once</a> before preceding.
Let's take an example of finding a number which is even and also greater than '10': Let's take an example of finding a number which is even and also greater than '10':
``` ```
@ -71,18 +71,18 @@ if x % 2 == 0: # this is how you create a comment and now, checking for even.
if x > 10: if x > 10:
print("This number is even and is greater than 10") print("This number is even and is greater than 10")
else: else:
print("This number is even, but not greater 10") print("This number is even, but not greater than 10")
else: else:
print ("The number is not even. So point checking further.") print ("The number is not even. So point checking further.")
``` ```
This was just a simple example for nested if's. Please feel free to explore more online. This was just a simple example for 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 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:
``` ```