CHAPTER 4
CAMILLE JOY SOSA
LOOPS
for LOOP
A FOR LOOP IS A PROGRAMMING CONSTRUCT USED TO ITERATE OVER A SEQUENCE OF
ELEMENTS, EXECUTING A BLOCK OF CODE FOR EACH ELEMENT IN THE SEQUENCE. A FOR
LOOP IS LIKE A CHECKLIST WHERE YOU GO THROUGH EACH ITEM ON YOUR LIST AND DO
SOMETHING WITH IT.
Goes through a sequence of items.
FOR LOOP
In this code:
LINE 2: Defined a list called “numbers” containing the integers from 1 to 5.
LINE 5: Used for loop to iterate over each element in the numbers list.
PROCESS: For each iteration, the variable number takes on the value of the current element in the list.
LINE 6 INDENTION: Inside the loop, we simply print out the value of number.
OUTPUT
FOR LOOP
LINE 1: This line starts with a hash symbol (#), indicating a comment.
LINE 2: Defines a list called "numbers" containing the integers from 1 to 5.
LINE 4: We initialized a variable called "total" to 0 to hold the sum of the numbers.
LINE 6: We used a for loop to iterate over each element in the "numbers" list.
LINE 7: "+=" operator adds the value of the current number to the "total" variable and assigns the result back to the "total" variable,
effectively updating the sum in each iteration of the loop.
LINE 9: Outside the loop Prints out the sum of the numbers stored in the "total" variable.
OUTPUT
FOR LOOP
LINE 2: Defines a list called numbers containing integers from 1 to 10
LINE 4: Initializes an empty list called even_numbers which will be used to store even numbers found in the numbers list.
LINE 6: Begins a for loop that iterates over each element in the numbers list. The variable number will take on the value of each
element in the list during each iteration.
LINE 8: Checks if the current number is even by calculating its remainder when divided by 2. If the remainder is 0 then even.
LINE 10: If the current number is even, it is added to the even_numbers list using the append() method.
LINE 12: Prints out the list of even numbers stored in the even_numbers list.
OUTPUT
FOR LOOP
LINE 2: Defines a list called numbers containing the integers 10, 15, 20, 25, and 30.
LINE 4: We initialized a variable called “even_sum" to 0 to accumulate the sum of even numbers.
LINE 6: Begins a for loop that iterates over each element in the numbers list. The variable number will take on the value of each
element in the list during each iteration.
LINE 8: Checks if the current number is even by calculating its remainder when divided by 2. If the remainder is 0 then even.
LINE 10: If the current number is even, it is added to the even_sum variable using the += operator.
LINE 12: Print out the sum of even numbers calculated earlier and displays it with an appropriate message.
OUTPUT
while LOOP
A WHILE LOOP IS A CONTROL FLOW STATEMENT THAT REPEATEDLY EXECUTES A BLOCK OF CODE AS
LONG AS A SPECIFIED CONDITION IS TRUE. IT’S LIKE A CONTINUOUS ACTION THAT KEEPS HAPPENING
AS LONG AS A PARTICULAR CONDITION REMAINS VALID. IT'S LIKE SAYING "KEEP DOING THIS UNTIL I
SAY STOP."
Keeps going as long as a condition is true.
WHILE LOOP
In this code:
LINE 2: number is initialized to 1.
LINE 4: The while loop continues as long as number is less than or equal to 5.
Inside the loop:
LINE 6: The current value of number is printed.
LINE 8: The value of number is incremented by 1 for the next iteration.
Once number reaches 6, the condition number <= 5 becomes false, and the loop terminates.
OUTPUT
WHILE LOOP
In this code:
LINE 2: number is initialized to 1.
LINE 4: named sum_numbers is initialized to 0. This variable will be used to accumulate the sum of the numbers.
LINE 6: The while loop continues as long as number is less than or equal to 11.
Inside the loop:
LINE 6: The current value of number is printed.
LINE 10: The current value of number is being added to the variable sum_numbers using the += operator.
LINE 12: The value of number is incremented by 1 for the next iteration.
LINE 14: Print the message "Sum of the numbers:" followed by the value stored in the variable sum_numbers .
OUTPUT
WHILE LOOP
LINE 2: number is initialized to 1.
LINE 4: An empty list named even_numbers is initialized to store even numbers.
LINE 6: The while loop continues as long as number is less than or equal to 11.
Inside the loop:
LINE 8: Checks if the current number is even by calculating its remainder when divided by 2. If the remainder is 0 then even.
LINE 10: If the current number is even, it's appended to the even_numbers list using the append() method.
LINE 12: The value of number is incremented by 1 for the next iteration.
LINE 14: Print the message “Even numbers" followed by the value stored in the variable even_numbers.
OUTPUT
WHILE LOOP
LINE 2: number is initialized to 1.
LINE 4: even_sum is initialized to 0.
LINE 6: The while loop continues as long as number is less than or equal to 11.
Inside the loop:
LINE 8: Checks if the current number is even by calculating its remainder when divided by 2. If the remainder is 0 then even.
LINE 10: If the current number is even, this line adds it to the even_sum variable.
LINE 12: The value of number is incremented by 1 for the next iteration.
LINE 14: Print the message “Sum of even numbers: " followed by the value stored in the variable even_sum.
OUTPUT
continue in for LOOP
THE CONTINUE STATEMENT IS USED TO SKIP THE CURRENT ITERATION OF A LOOP AND
MOVE TO THE NEXT ITERATION. CONTINUE IS LIKE A "SKIP AHEAD" COMMAND. IT
TELLS THE LOOP TO JUMP TO THE NEXT ITERATION WITHOUT FINISHING THE
CURRENT ONE.
Skips the rest of the current iteration and moves to the next one.
continue in for LOOP
LINE 2: created a list called numbers containing a mix of positive and negative integers.
LINE 4: Used for loop to iterate over each element in the numbers list. We're going through each number in the list.
Inside the loop:
LINE 6: Checks if the current number is less than zero, means negative
LINE 8: “continue” If a number is negative, we skip it and move to the next number.
LINE 10: If a number is positive, we print it.
OUTPUT
break in for LOOP
THE BREAK STATEMENT IS USED TO EXIT A LOOP PREMATURELY, REGARDLESS OF THE
LOOP'S NORMAL CONDITION FOR TERMINATION. BREAK IS LIKE A SUDDEN STOP
BUTTON. IT LETS YOU JUMP OUT OF THE LOOP AND MOVE ON TO WHATEVER COMES
NEXT.
Jumps out of the loop prematurely.
break in for LOOP
LINE 2: for number in range(1, 11):: This line starts a loop that iterates over numbers from 1 to 10.
LINE 4: if number > 5:: This line checks if the current number is greater than 5.
LINE 6: break: If the current number is greater than 5, this line stops the loop immediately.
LINE 8: print(number): This line prints the current number if it's less than or equal to 5.
LINE 9: print("Loop ended."): This line prints "Loop ended." after the loop is finished or broken out of.
OUTPUT
continue in while
LOOP
continue in while LOOP
LINE 2: count = 0: This line initializes a variable named count and sets its initial value to 0.
LINE 4: while count < 5:: This line starts a while loop that continues as long as the value of count is less than 5.
LINE 6: count += 1: This line increments the value of count by 1. It's shorthand for count = count + 1.
LINE 8: checks if the current value of count is even.
LINE 10: the continue statement skips the rest of the loop's code and continues with the next iteration.
LINE 12: print("Odd count:", count): This line prints the current value of count if it's odd (i.e., if the if condition in line 8 is false).
LINE 14: This line prints a message indicating that the loop has finished. It executes after the while loop has completed because it's
not indented within the loop.
OUTPUT
break in while LOOP
break in while LOOP
LINE 2: number = 0: Initializes a variable number with the value 0.
LINE 4: while number <= 11:: Starts a while loop that continues as long as number is less than or equal to 11.
LINE 6: if number > 5:: Checks if the current value of number is greater than 5.
LINE 8: break: If the condition number > 5 is true, the break statement is executed, which exits the loop.
LINE 10: print(number): Prints the current value of number.
LINE 12: number += 1: Increments the value of number by 1 in each iteration of the loop.
LINE 14: Once the loop ends (either due to reaching 11 or breaking out when number > 5), it prints "Loop ended."
OUTPUT
THANK YOU
END