Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Control flow #33

Merged
merged 6 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions python_basics/7.control_flow/break_challenge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
* Problem Description

* Create a program to calculate the sum of integers entered by the user. If the user enters 0 or negative integer, terminate the loop and print the sum.


* Initialize a variable named total with value 0 at the beginning.

* Create a while loop with condition True.

* If the user enters 0 or negative integer, use break to terminate the loop.

* If the user enters a positive number, add it to the total variable.

* Print the total from outside of the loop.

"""

total = 0

while True:
number = int(input())

if number == 0 or number < 0:
break

total += number

print(total)
17 changes: 17 additions & 0 deletions python_basics/7.control_flow/break_statement.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""
* The break and continue statements are used inside a loop to alter its flow.

* The break statement terminates the loop immediately when it is encountered. And, the continue statement allows us to skip statements inside the loop.

"""

"""
* Break Statement
"""

for i in range(1, 6):
print(i)
if i == 3:
break

print('This is outside the loop.')
20 changes: 20 additions & 0 deletions python_basics/7.control_flow/continue_challenge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""
* Create a program to print odd numbers between 1 and n, where n is a positive integer entered by the user.

*
Take integer input from the user and store it in variable n.
*
Use a for loop to iterate from number equal to 1 to n+1.
*
If number is even, use continue to skip the number from printing.
*
If number is odd, print the number.

"""

number = int(input('Insert a number: '))

for n in range(1, number + 1):
if n % 2 == 0:
continue
print(n)
13 changes: 13 additions & 0 deletions python_basics/7.control_flow/continue_statement.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""
*The continue statement skips the code inside the loop for that current iteration. The loop will not terminate but continues on with the next iteration.
"""

# Example:

for number in range(1, 11):

# condition to check odd number
if number % 2 != 0:
continue

print(number)