|
| 1 | +# Control Statements in Python |
| 2 | + |
| 3 | +Control statements in Python are used to control the flow of execution of the program. Here are a few examples: |
| 4 | + |
| 5 | +## If Statement |
| 6 | + |
| 7 | +The `if` statement is used to test a condition. If the condition is true, the block of code inside the `if` statement is executed. |
| 8 | + |
| 9 | +```python |
| 10 | +x = 10 |
| 11 | +if x > 5: |
| 12 | + print("x is greater than 5") |
| 13 | +``` |
| 14 | + |
| 15 | +## If-Else Statement |
| 16 | + |
| 17 | +The `if-else` statement is used to test a condition and execute one block of code if the condition is true and another block of code if the condition is false. |
| 18 | + |
| 19 | +```python |
| 20 | +x = 10 |
| 21 | +if x > 5: |
| 22 | + print("x is greater than 5") |
| 23 | +else: |
| 24 | + print("x is not greater than 5") |
| 25 | +``` |
| 26 | + |
| 27 | +## Elif Statement |
| 28 | + |
| 29 | +The `elif` statement is used to test multiple conditions. If the first condition is false, it checks the next condition, and so on. |
| 30 | + |
| 31 | +```python |
| 32 | +x = 10 |
| 33 | +if x > 15: |
| 34 | + print("x is greater than 15") |
| 35 | +elif x > 5: |
| 36 | + print("x is greater than 5 but less than or equal to 15") |
| 37 | +else: |
| 38 | + print("x is 5 or less") |
| 39 | +``` |
| 40 | + |
| 41 | +### Problems On Conditionals |
| 42 | +- [Python If-Else HackerRank](https://www.hackerrank.com/challenges/py-if-else/problem?isFullScreen=true) |
| 43 | +- [Check The Status GFG](https://www.geeksforgeeks.org/problems/check-the-status/1?page=1&category=python&difficulty=Easy&sortBy=submissions) |
| 44 | + |
| 45 | +## While Loop |
| 46 | + |
| 47 | +The `while` loop is used to repeat a block of code as long as a condition is true. |
| 48 | + |
| 49 | +```python |
| 50 | +x = 0 |
| 51 | +while x < 5: |
| 52 | + print(x) |
| 53 | + x += 1 |
| 54 | +``` |
| 55 | + |
| 56 | +## For Loop |
| 57 | + |
| 58 | +The `for` loop is used to iterate over a sequence (such as a list, tuple, or string) and execute a block of code for each item in the sequence. |
| 59 | + |
| 60 | +```python |
| 61 | +fruits = ["apple", "banana", "cherry"] |
| 62 | +for fruit in fruits: |
| 63 | + print(fruit) |
| 64 | +``` |
| 65 | + |
| 66 | +## Break Statement |
| 67 | + |
| 68 | +The `break` statement is used to exit a loop prematurely. |
| 69 | + |
| 70 | +```python |
| 71 | +for i in range(10): |
| 72 | + if i == 5: |
| 73 | + break |
| 74 | + print(i) |
| 75 | +``` |
| 76 | + |
| 77 | +## Continue Statement |
| 78 | + |
| 79 | +The `continue` statement is used to skip the rest of the code inside a loop for the current iteration and continue with the next iteration. |
| 80 | + |
| 81 | +```python |
| 82 | +for i in range(10): |
| 83 | + if i % 2 == 0: |
| 84 | + continue |
| 85 | + print(i) |
| 86 | +``` |
| 87 | + |
| 88 | +## Pass Statement |
| 89 | + |
| 90 | +The `pass` statement is a null operation; it is used when a statement is required syntactically but no code needs to be executed. |
| 91 | + |
| 92 | +```python |
| 93 | +for i in range(10): |
| 94 | + if i % 2 == 0: |
| 95 | + pass |
| 96 | + else: |
| 97 | + print(i) |
| 98 | +``` |
| 99 | + |
| 100 | +### Problems On Conditionals |
| 101 | +- [Loops HackerRank](https://www.hackerrank.com/challenges/python-loops/problem?isFullScreen=true) |
| 102 | +- [For Loop GFG](https://www.geeksforgeeks.org/problems/for-loop-python/1?page=1&category=python&sortBy=submissions) |
| 103 | +- [While Loop GFG](https://www.geeksforgeeks.org/problems/while-loop-in-python/1?page=1&category=python&sortBy=submissions) |
| 104 | + |
| 105 | + |
| 106 | +These are some of the basic control statements in Python that help in controlling the flow of the program. |
0 commit comments