The document discusses different types of repetition structures or loops in programming which are used to repeatedly execute a block of code under certain conditions. It explains the while loop, do-while loop and their syntax as well as providing examples.
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
27 views
Lecture 11-While-Do-While
The document discusses different types of repetition structures or loops in programming which are used to repeatedly execute a block of code under certain conditions. It explains the while loop, do-while loop and their syntax as well as providing examples.
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14
Lecture 11
Introduction to repetition structures. Repetition essentials.
The while loop. Example programs. Loops in programming • A loop in programming is like a repeat button that runs the same set of instructions multiple times until a certain condition is met. • We use loops to perform repetitive tasks efficiently, like counting numbers, going through items in a list, or asking for user input until the correct one is given, saving time and reducing code. Introduction to repetition structures • Repetition structures, or loops, in programming are used to execute a block of code repeatedly under specific conditions. • There are mainly three types: for loops, which run a set number of times. • while loops, which run as long as a condition is true. • do-while loops, similar to while loops but guaranteeing at least one execution of the block. • These structures help in automating repetitive tasks, like iterating over arrays, processing data, or implementing timers, making code more efficient and concise. Why are Repetition structures necessary? • Repetition structures are necessary in programming because they allow for efficient repetition of code blocks without manually writing the same code multiple times. • They help automate tasks, process large datasets, and handle tasks that require repeated actions, such as checking for user input or iterating over elements in a collection. • This leads to more concise, readable, and maintainable code, making it easier to develop, debug, and enhance software applications. Repetition essentials • Repetition in programming, essential for automating and managing repeated tasks, involves three key components: • Initialization: Setting a starting point for the loop, such as defining a counter variable. • Condition: A logical statement that controls the loop's duration, determining how long the loop continues to execute. • Increment/Decrement: Modifying the loop variable in each iteration to progress towards meeting the condition that will end the loop. While loop • A while loop is like a repeated question that keeps asking the same thing until it gets the answer it needs. • In programming, it keeps running the same block of code over and over as long as a certain condition is true. • Once the condition becomes false, the loop stops. For example, it's like listening to your favorite song on repeat until you decide to stop; the music keeps playing (looping) as long as you want it to (the condition). Syntax of while loop Example program Do-While loop • A do-while loop is like a theme park ride that you must go on at least once, but you can choose to continue riding if you still want to. • In programming, it runs the code inside the loop once first, without checking the condition. • After the first run, it checks a condition, and if that condition is true, it keeps running the code over and over again until the condition becomes false. • So, no matter what, the code inside the loop runs at least once. Syntax Do-while syntax explaination • do: This signals the start of the do-while loop. • {...}: Inside the curly braces is the block of code that will execute at least once, regardless of the condition. • while (condition);: After executing the code block once, the loop checks the condition inside the parentheses. If the condition is true, the code block inside the do will run again. This repeats until the condition becomes false. Note that there's a semicolon (;) at the end of the while statement, which is necessary to complete the loop's syntax. Example program Example Program