From aa5e550c3bad1e66b10cf9d2bdc925f4bc2303fa Mon Sep 17 00:00:00 2001 From: mistymayem <44274210+mistymayem@users.noreply.github.com> Date: Sat, 3 Nov 2018 21:10:30 -0400 Subject: [PATCH] Cleaned up formatting and added headers (#20103) --- .../python/if-elif-else-statements/index.md | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/guide/english/python/if-elif-else-statements/index.md b/guide/english/python/if-elif-else-statements/index.md index 84ff165ce72..88f832a40b2 100644 --- a/guide/english/python/if-elif-else-statements/index.md +++ b/guide/english/python/if-elif-else-statements/index.md @@ -4,8 +4,8 @@ title: 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: -Note that parenthesis is not used before and after the condition check as in other languages. +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. ```python if True: print('If block will execute!') @@ -24,6 +24,7 @@ _Example_: if 1: # 1 evaluates to true print('If block will execute!') ``` +***Else statement*** You can optionally add an `else` response that will execute if the condition is `False`: ```python @@ -44,6 +45,8 @@ else: *Note that there is no condition following the `else` keyword - it catches all situations where the condition was `False`* +***Elif statement*** + Multiple conditions can be checked by including one or more `elif` checks after your initial `if` statement but only one condition will execute: ```python @@ -59,13 +62,14 @@ else: 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`.* + +***Nested if statement*** We can also create nested if statements for decision making. Before preceding please refer to the indentation guide once before preceding. Let's take an example of finding a number which is even and also greater than '10': -``` -python +```python x = 34 if x % 2 == 0: # this is how you create a comment and now, checking for even. if x > 10: @@ -73,7 +77,7 @@ if x % 2 == 0: # this is how you create a comment and now, checking for even. else: print("This number is even, but not greater than 10") else: - print ("The number is not even. So 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.