ENSC 151 - Week 5 PDF

Summary

This document is a set of lecture notes for a course on C++ programming, focusing on loops, including increment and decrement operators, different types of loops (while, for, do-while), break and continue statements, nested loops, and variable name scope. The notes are from Dr. Maira Monteiro at Seattle Pacific University.

Full Transcript

ENSC 151 – Week 5 Professor: Yuri Rodrigues, PhD Based on material from Dr. Maira Monteiro of the Seattle Pacific University, used here with permission Objectives We will learn how to write loops in C ++ programming. For this, the following key concepts are covered: Increme...

ENSC 151 – Week 5 Professor: Yuri Rodrigues, PhD Based on material from Dr. Maira Monteiro of the Seattle Pacific University, used here with permission Objectives We will learn how to write loops in C ++ programming. For this, the following key concepts are covered: Increment and decrement operators; Loops (while, for, do-while); Break and continue statements in loops; Nested loops; Variable name scope. Loops What is it? Repeating code multiple times What is it? Repeating code multiple times What is it?  Why use it? To use variables efficiently For example, compute average with fewer variables Results in cleaner, more efficient code, and saves time! A loop repeats a set of statements multiple times until some condition is satisfied. Loops Post-test: Execute statement(s), check if expression is true, if so, execute Two types of repetition: statement(s), check if expression is Pre-test: true, if so, execute statement(s), … Check if expression is true, if so, repeat statement(s), check if expression is true, if so, repeat statement(s), … Before we start: the ++ and -- Operators It is very common to subtract 1 or add 1 from the current value of an integer variable. There are two operators which abbreviate these operations: ++ add one to the current integer variable -- subtract one from the current integer variable Example: int j=0; j++; // j = 1; Equivalent to j = j + 1; j--; // j = 0; Equivalent to j = j - 1; New Operators: Increment The increment operator: addition by one Syntax: ++variable (pre-increment) or variable++ (post-increment) Both increment the variable by 1 int x = 1; x++; //x is now 2 … int x = 1; ++x; //x is now 2 What is the difference? int x = 5; int y = x++; // value of x assigned to y, then x incremented. So, x is 6, y is 5 versus int x = 5; int y = ++x; // x is incremented, then value of x is assigned to y. So, x is 6, y is 6 New Operators: Decrement The decrement operator: subtraction by one Syntax: --variable (pre-decrement) or variable-- (post-decrement) Both decrement the variable by 1 int x = 3; x--; //x is now 2 --x; //x is now 1 What is the difference? int x = 5; int y = x--; // value of x is assigned to y, then x is decremented. So, x is 4, y is 5 versus int x = 5; int y = --x; // x is decremented, then value of x is assigned to y. So, x is 4, y is 4 The while Loop A pre-test loop The expression provides an entry condition to the loop While the expression is true, execute statement(s) Once the expression is false, the loop ends (without executing statement(s)) Can have one or more statements executed each time expression is true while Loop Syntax Example: while (expression) { This is acceptable style! statements Single characters OK for } int i = 0; controlling loops while (i < 10) { cout > name; Lisa 128 Cindy 359 Nicole 267 if (name == SENTINEL) Blair 165 found = true; Abby 290 Amy 190 Megan 450 while (!found) { //Flag-Controlled Loop Elizabeth 280 cin >> numOfBoxesSold; Meridth 290 totalNumOfBoxesSold = totalNumOfBoxesSold + numOfBoxesSold; Leslie 430 Chelsea 378 numOfVolunteers++; -1 cin >> name; The total number of boxes sold: 3347 Enter the cost of one box: 3.50 if (name == SENTINEL) found = true; The total money made by selling cookies: $11714.50 } The average number of boxes sold by each volunteer: 278.92 More on Expressions in while Statements The expression in a while statement can be complex Example: what if we only want (at most) 100 volunteers’ names while ((numOfVolunteers > name;... } for Loop: a pre-test, counter-controlled loop A pre-test, counter-controlled loop The most common type of loop is the for loop. Syntax: for (initial statement; loop condition; update statement) { statements } Explanation: 1) initial statement - is executed once at the start of the loop 2) loop condition - is evaluated before every loop iteration to check for loop termination 3) update statement - is evaluated after every loop iteration to update the loop counter for Loop: a pre-test, counter-controlled loop Step 1. If evaluates to true, go to Step 2 int i = 0; Example: If evaluates to false, go to Step 4 Step 0. Happens once, Step 3. Execute this update statement, before we start loop go back to Step 1 int i; i

Use Quizgecko on...
Browser
Browser