-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoops.py
77 lines (68 loc) · 2.27 KB
/
Loops.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-
"""
Created on Thu Aug 17 22:24:10 2023
@author: Teja Ram Pooniya
@program: Topic - Loops(if-else) in Python
@Description:
Loops and conditional statements (if-else) are fundamental constructs in programming.
For Loop:
A for loop is used to iterate over a sequence (like a list, tuple, or range)
and execute a block of code for each element.
========================================================================
"""
# Example of a for loop
fruits = ['Apple', 'Banana', 'Cherry']
for fruit in fruits:
print(fruit, "\n")
"""
================================================================================
While Loop:
A while loop repeatedly executes a block of code as long as a certain condition is true.
"""
count = 0
while count <= 11:
print(count)
count += 1
"""
================================================================================
If-Else Statement:
An if statement is used to execute a block of code only if a certain condition is true.
An else statement can be added to specify what should happen if the condition is false.
"""
# Example of if-else statement
age = 18
if age >= 18:
print("You are an adult.\n")
else:
print("You are a minor.\n")
"""
================================================================================
Nested If-Else:
You can also use nested if-else statements to create more complex conditions.
"""
# Example of nested if-else
x = 10
if x > 0:
if x % 2 == 0:
print("Positive even number.\n")
else:
print("Positive odd number.\n")
else:
print("Non-positive number.\n")
"""
================================================================================
Loop Control Statements:
Python also provides loop control statements like break and continue.
break is used to exit a loop prematurely, and continue is used to skip
the current iteration and move to the next one.
"""
# Example of loop control statements
for i in range(10):
if i == 5:
break # Exit the loop when i reaches 5
if i % 2 == 0:
continue # Skip even numbers
print(i)
"""
These constructs allow you to control the flow of your program based on conditions and to perform repetitive tasks efficiently using loops.
"""