Loops - CMPT 1109 - PDF

Summary

This document is lecture notes on loops in C++ programming for a CMPT 1109 course. It covers different types of loops, problem-solving techniques using loops, and various practical examples. The notes include learning outcomes, problem statements, flowchart visualization for loop implementation, and details on while, do-while, for loops within C++ context.

Full Transcript

Loops Chapter 5 Instructor: Link Course: CMPT 1109 2 Learning Outcomes ▸ Design algorithms with loops ▸ Recognize different types of loops; count-controlled and event-controlled loops. ▸ Problem solving using different loops (for, while and do-while) and nested...

Loops Chapter 5 Instructor: Link Course: CMPT 1109 2 Learning Outcomes ▸ Design algorithms with loops ▸ Recognize different types of loops; count-controlled and event-controlled loops. ▸ Problem solving using different loops (for, while and do-while) and nested loops ▸ Choose the best loop ▸ Avoid pitfalls while working with loops ▸ Differentiate loops and decision structures ▸ Apply loops for validating data ▸ Apply sentinel value and loops to give user the control of execution 3 Problem statement ▸ We need a program to receives the number of apples from an unknown sequence of trucks and calculates/displays the total amount of apples. The input ends with entering of -1. How do we solve this problem? numApples sum initial 0 0 The input sequence would be a group of 65 0+65 numbers followed by -1, for example: 48 0+65+48 65 48 83 93 -1 … … ▸ Repeat the following steps while the numApples is not -1. Add the numApples value to the sum Read input and store in numApples Report the final value of sum 4 Plan the Solution start numApples = 0 sum = 0 A loop executes instructions False repeatedly while a condition is True numApples != -1 sum True sum += numApples start numApples 5 Looping -- also called iteration/repeatition ▸ The third basic structure of programming ▹ sequence, selection, iteration ▸ Allows the execution of block of statement(s) repeatedly ▸ Loops are made up a loop condition and the body which contains the statement(s) to repeat 6 Typical use-cases ▸ Execute a loop: ▹ while a specific condition remains true ▹ until a specific condition becomes false ▹ until we reach the end of some input stream ▹ a specific number of times ▹ for each element in a collection/list ▹ forever ▹ more 7 C++ Looping Constructs ▸ while loop ▹ iterate while a condition remains true ▹ stop when the condition becomes false ▹ check the condition at the beginning of every iteration ▸ do-while loop ▹ similar to while ▹ check the condition at the end of every iteration ▸ for loop ▹ iterate a specific number of times ▸ Range-based for loop ▹ one iteration for each element in a range or collection 8 Introducing while Loops Pseudocode C++ syntax while(condition){ Repeat while condition is true statements A statements A } ▸ A while loop repeats the statements Flowchart inside the body a ▸ Before executing the body each time, it False checks whether the condition is true: condition b ▹ Only enters the block, if the condition evaluates to true true ▹ The first time that the condition is false, Statements A code execution continues after closing curly brace of the loop (exits the loop) 9 while Loop ▸ A simple while loop to display numbers from 1 to 5; Example Flowchart int i = 1; i=1 1 2 while (i number; we prompt user for the } integer again and again cout 5) { Note: The program is cout > number; previous example, } with the exception of the condition in the cout 5) { cout height; double area = width * height; cout

Use Quizgecko on...
Browser
Browser