fixed some minor mistakes like vanished colon and etc. (#22984)

This commit is contained in:
tahirsoft
2018-11-21 01:59:04 +02:00
committed by Manish Giri
parent bc48cff3e8
commit df0196f6e3

View File

@ -5,13 +5,13 @@ 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 are not used before and after the condition check such as in other 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!')
``` ```
```python ```python
x = 5 x = 5
@ -20,11 +20,12 @@ if x > 4:
``` ```
> **Tips** : You can use **1** as alternative to **True** and **0** as an 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: # 1 evaluates to true if 1: # 1 evaluates to true
print('If block will execute!') print('If block will execute!')
``` ```
***Else statement*** ***Else statement***
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`:
@ -34,7 +35,7 @@ if not True:
else: else:
print('Else statement will execute!') print('Else statement will execute!')
``` ```
Or you can also see this example Or you can also see this example:
```python ```python
y = 3 y = 3
@ -43,7 +44,6 @@ if y > 4:
else: 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`*
***Elif statement*** ***Elif statement***
@ -67,7 +67,7 @@ else:
***Nested if statement*** ***Nested if statement***
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. 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>.
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':
```python ```python
@ -85,6 +85,7 @@ Output
```python ```python
This number is even and is greater than 10 This number is even and is greater than 10
``` ```
This was just a simple example of a 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>.