Learning coding means GreatToCode Be more than a Coder ! Greattocode , Join GreatToCode Community,1000+ Students Trusted On Us .If You want to learn coding, Then GreatToCode Help You.No matter what It Takes !


CODE YOUR WAY TO A MORE FULFILLING And HIGHER PAYING CAREER IN TECH, START CODING FOR FREE Camp With GreatToCode - Join the Thousands learning to code with GreatToCode
Interactive online coding classes for at-home learning with GreatToCode . Try ₹Free Per Month Coding Classes With The Top Teachers . 10 Python programs for Loop Control Statements

10 Python programs for Loop Control Statements

Become More Then A coder | Learn & Start Coding Now.
Here are ten Python programs demonstrating the usage of loop control statements (`break`, `continue`, and `pass`) in various scenarios:

### 1. Program to Print Numbers until 5 using `break`:

```python
# Program 1: Printing numbers until 5 using break
for num in range(1, 11):
    if num == 6:
        break
    print(num)
```

### 2. Program to Skip Even Numbers using `continue`:

```python
# Program 2: Skipping even numbers using continue
for num in range(1, 11):
    if num % 2 == 0:
        continue
    print(num)
```

### 3. Program to Find Prime Numbers using `break`:

```python
# Program 3: Finding prime numbers using break
for num in range(2, 20):
    for i in range(2, num):
        if num % i == 0:
            break
    else:
        print(num)
```

### 4. Program to Print Numbers using `pass`:

```python
# Program 4: Printing numbers using pass
for num in range(1, 6):
    pass # Placeholder for future code
    print(num)
```

### 5. Program to Search for a Specific Element using `break`:

```python
# Program 5: Searching for a specific element using break
search_list = [4, 7, 2, 9, 5]
search_item = 9
for item in search_list:
    if item == search_item:
        print("Item found!")
        break
else:
    print("Item not found!")
```

### 6. Program to Skip Negative Numbers using `continue`:

```python
# Program 6: Skipping negative numbers using continue
numbers = [-3, 4, -5, 7, 0, -1, 9]
for num in numbers:
    if num < 0:
        continue
    print(num)
```

### 7. Program to Generate Fibonacci Sequence using `break`:

```python
# Program 7: Generating Fibonacci sequence using break
a, b = 0, 1
fib_sequence = []
while True:
    fib_sequence.append(a)
    a, b = b, a + b
    if a > 50:
        break
print(fib_sequence)
```

### 8. Program to Print Even Numbers using `pass`:

```python
# Program 8: Printing even numbers using pass
for num in range(1, 11):
    if num % 2 == 0:
        pass
    else:
        print(num)
```

### 9. Program to Skip a Specific Number using `continue`:

```python
# Program 9: Skipping a specific number using continue
for num in range(1, 11):
    if num == 5:
        continue
    print(num)
```

### 10. Program to Demonstrate Nested Loops with `break`:

```python
# Program 10: Demonstrating nested loops with break
for i in range(1, 6):
    for j in range(1, 6):
        print(i * j)
        if j == 3:
            break
```

Feel free to run these programs and observe the outcomes. They showcase different use cases of loop control statements in Python, from terminating loops prematurely to skipping iterations and placeholder code.

Post a Comment

0 Comments

•Give The opportunity to your child with GreatToCode Kid's • Online Coding Classes for Your Kid • Introduce Your kid To the world's of coding
•Fuel You Career with our 100+ Hiring Partners, Advance Your Career in Tech with GreatToCode. •Join The Largest Tech and coding Community and Fast Forward Your career with GreatToCode. •10000+ Learner's+ 90 % placement Guarantee. • Learning Coding is Better with the GreatToCode community .
•Greattocode Kid's •GreatToCode Career •GreatToCode Interview •GreatToCode Professional •GreatToCode for schools •GreatToCode For colleges •GreatToCods For Businesses.
Are you ready to join the millions of people learning to code? GreatToCode Pass is your one-stop-shop to get access to 1000+ courses, top-notch support, and successful job placement. What are you waiting for? Sign up now and get your future in motion with GreatToCode Pass.