KnowledgeBoat Logo
|

Informatics Practices

Write the output of the following:

for x in range (10, 20):
   if (x % 2 == 0):
      continue
   print (x)

Python Control Flow

2 Likes

Answer

11
13
15
17
19

Working

Below is the detailed explanation of this code:

  1. for x in range(10, 20): → This loop iterates over the range of numbers starting from 10 (inclusive) up to but not including 20 (exclusive). So, x will take on the values: 10, 11, 12, 13, 14, 15, 16, 17, 18, 19.

  2. if x % 2 == 0: → For each value of x, this condition checks if x is an even number. The modulus operator % returns the remainder of x divided by 2. If the remainder is 0, x is even.

  3. continue → If the condition x % 2 == 0 is true (i.e., x is even), the continue statement is executed. This statement skips the rest of the code inside the loop for the current iteration and moves to the next iteration.

  4. print(x) → This line prints the value of x if it is not even (i.e., if the continue statement is not executed).

Let’s analyze the loop iteration by iteration:

  • When x = 10:

  • 10 % 2 == 0 is true.

  • continue is executed, so print(x) is skipped.

  • When x = 11:

  • 11 % 2 == 0 is false.

  • print(x) outputs:

    11
    
  • When x = 12:

  • 12 % 2 == 0 is true.

  • continue is executed, so print(x) is skipped.

  • When x = 13:

  • 13 % 2 == 0 is false.

  • print(x) outputs:

    13
    
  • When x = 14:

  • 14 % 2 == 0 is true.

  • continue is executed, so print(x) is skipped.

  • When x = 15:

  • 15 % 2 == 0 is false.

  • print(x) outputs:

    15
    
  • When x = 16:

  • 16 % 2 == 0 is true.

  • continue is executed, so print(x) is skipped.

  • When x = 17:

  • 17 % 2 == 0 is false.

  • print(x) outputs:

    17
    
  • When x = 18:

  • 18 % 2 == 0 is true.

  • continue is executed, so print(x) is skipped.

  • When x = 19:

  • 19 % 2 == 0 is false.

  • print(x) outputs: 19

Putting all these outputs together, the full output will be:

11
13
15
17
19

Answered By

1 Like


Related Questions