Summary

This document details Python loops, specifically the for and while loop. The document provides explanations and examples of various loop types, ideal for programming education for undergraduate level students. The presentation includes detailed diagrams and explanations.

Full Transcript

Chapter 5 – Loops “Cont’d PRESENTED BY : DR. THANAA MOHAMED HASSAN Agenda Nested For While Loop Nested While Nested Loop A nested loop is a loop inside a loop The "inner loop" will be executed one time for each iteration of the "outer loop" Nested For Loop for iterating_va...

Chapter 5 – Loops “Cont’d PRESENTED BY : DR. THANAA MOHAMED HASSAN Agenda Nested For While Loop Nested While Nested Loop A nested loop is a loop inside a loop The "inner loop" will be executed one time for each iteration of the "outer loop" Nested For Loop for iterating_var1 in sequence: #outer loop for iterating_var2 in sequence: #inner loop #block of statements #Other statements Nested For Loop i=[1,2,3] J=[4,5,6] Nested For Loop Nested For Loop Write a nested for loop program to print multiplication table in Python from 1 till 10 inclusive The While Loop There are two parts to every while loop: 1. The while statement starts with the while keyword, followed by a test condition, and ends with a colon (:). 2. The loop body contains the code that gets repeated at each step of the loop. Each line is indented four spaces. The While Loop The While Loop Check how many times a given number can be divided by 3 before it is less than or equal to 10. The While Loop When do we use a while loop, and why do we use it? Automate and repeat tasks.: while loops execute blocks of code over and over again until the condition is met it allows us to automate and repeat tasks in an efficient manner. Indefinite Iteration: The while loop will run as often as necessary to complete a particular task. When the user doesn’t know the number of iterations before execution, while loop is used instead of a for loop Reduce complexity: while loop is easy to write. using the loop, we don’t need to write the statements again and again. Infinite loop: If the code inside the while loop doesn’t modify the variables being tested in the loop condition, the loop will run forever. The While Loop The While Loop Infinite While Loop If-Else in While Loop Print even and odd numbers between 1 to the entered number. Break Continue Else in While Loop Nested While Loop Nested While Loop Thank You 

Use Quizgecko on...
Browser
Browser