Loops repeat a block of code until a certain condition is met. The three types of loops in C++ are: 1. For loops use an initialization statement, test expression, and update statement to control the loop. 2. While loops continuously execute the code block as long as the test expression is true. 3. Do-while loops always execute the code block at least once before checking the test expression, and continue looping as long as the test is true.
This section introduces loops in programming, defining them as a way to repeat code. It lists three types: for loop, while loop, and do while loop.
Details the syntax of the for loop and explains its functioning through initialization, testing, and updates. Also includes a flowchart and program example.
Covers the while loop with its syntax, operational flow, and a flowchart alongside a program example to illustrate its functionality.
Discusses the do while loop, detailing its execution mechanism and providing visual and coded examples, alongside a flowchart.
Concludes the presentation with an invitation for questions, allowing for interaction and clarification on loops in C++.
What is Loop?And Type of Loop The university of Lahore Pakpattan campus
2.
Definition of loop •Loops are used in programming to repeat a specific block of code until some end condition is met. There are three type of loops in C++ programming • for loop • while loop • Do while loop
3.
for Loop Syntaxin c++ for(initializationStatement; testExpression; updateStatement) { // codes }
4.
How for loopworks? 1.The initialization statement is executed only once at the beginning. 2.Then, the test expression is evaluated. 3.If the test expression is false, for loop is terminated. But if the test expression is true, codes inside body of for loop is executed and update expression is updated. 4.Again, the test expression is evaluated and this process repeats until the test expression is false.
How while loopworks? •The while loop evaluates the test expression. •If the test expression is true, codes inside the body of while loop is evaluated. •Then, the test expression is evaluated again. This process goes on until the test expression is false. •When the test expression is false, while loop is terminated.
DO WHILE LOOPSYNTAX IN C++ Do { // codes; } while (testExpression);
14.
HOW DO...WHILE LOOPWORKS? •The codes inside the body of loop is executed at least once. Then, only the test expression is checked. •If the test expression is true, the body of loop is executed. This process continues until the test expression becomes false. •When the test expression is false, do...while loop is terminated.