CSC 1060 Week 07 Repetition PDF

Document Details

DivineZebra9695

Uploaded by DivineZebra9695

Red Rocks Community College

Tags

programming control structures loops computer science

Summary

This document contains lecture notes from a computer science class on control structures and repetition using loops.

Full Transcript

CSC 1060 CONTROL STRUCTURES: REPETITION OBJECTIVES AGENDA: WEEK 07 Write programs that execute 1. Loop definitions statements repeatedly using a 2. Types of Loops while or do..while loop....

CSC 1060 CONTROL STRUCTURES: REPETITION OBJECTIVES AGENDA: WEEK 07 Write programs that execute 1. Loop definitions statements repeatedly using a 2. Types of Loops while or do..while loop. 3. for, for range-based loops Control a loop with the user 4. while counting, sentinel loops confirmation and sentinel value. 5. do...while loops Discover the similarities and differences of three types of loop 6. break; and continue; statements. 7. while flag-controlled loops Implement program control with 8. TODO break and continue 9. Resources for Help LOOP DEFINITIONS NEVER use a goto; statement! A loop is a programming structure that repeats a sequence of instructions until a specific condition is met. Loop iteration – repeated body of code Number of iterations – how many times the loop executed Loop test expression – controls the flow of the loop Terminating loop – loop stops repeating – all loops need to terminate! CONTROL STRUCTURE: REPETITION Initialize test value While Test condition is valid Update test value Code instruction Code instruction Code instruction FOR COUNTING LOOP The counter variable used in the loop can be declared inside the loop at initialization for a block scope variable Block scope variables are only accessible within the block they are declared within A counter variable named, i, is the traditional name used by developers and is meaningful. for(initialize; test; update){ // body of code to repeat } FOR RANGE-BASED LOOP The range based for loop iterates over all the elements in range (sequence), where declaration declares a block scope variable able to take the value of an element in this range. for( declaration : range ){ // body to repeat } WHILE COUNTING LOOP int i = 0; // initialize test value while( i < num ){// test condition // body to repeat i++; // update test value } WHILE SENTINEL LOOP A sentinel value is a reserved value to terminate the loop. This loop potentially could have 0 iterations, if the first input entered is the sentinel value. std::cin >> ans; // get input - initialize // while check against sentinel while( ans != 'Q' ) { // test condition // body to repeat std::cin >> ans; // get input - update } DO...WHILE LOOP do{ // body to repeat } while( test condition ); Notice the ; at the end of the test expression required for do...while loop This loop has at least 1 iteration, since you DO first and check the test last. BREAK; STATEMENT The break; statement terminates the loop when it is encountered based upon a condition All types of loops can use break; A logic if decision is required determine whether to break; out of the loop Copy and paste the examples into pythontutor to visualize: https://www.w3schools.com/cpp/cpp_br eak.asp CONTINUE; STATEMENT The continue; statement is used to skip the current iteration of the loop and control of the program goes to the next iteration based upon a condition. When continue; is used in a for loop the update occurs and the condition test is performed before continuing an iteration. The continue; allows the current iteration to stop and continue to the next iteration. A logic if decision is required to determine whether to continue; the next iteration Copy and paste the examples into pythontutor to visualize: https://www.w3schools.com/cpp/cpp_break.asp WHILE FLAG bool flag = false; // initialize while( ! flag ) // test condition CONTROLLED { LOOP get input The flag controlled if ( ? test ? ) while loop is a flag = true; // update good structure to else if( ? test ? ) use when adding continue; input validation to else a program as // body to repeat shown. } REVIEW EXERCISES Complete the Parsons exercises from thinkcpp Problems 1-10 (Pick minimum of 3 to try) Change the question type to Parsons – you may have to change to "Active Write" and then change back to "Parsons" Parsons questions are drag and drop questions with the code mixed up. EARN YOUR PRE-WORK GRADE Post your weekly discussion question and research solution to D2L TODO Complete Week 07 Content Module in D2L to 100% WHAT'S COMING UP NEXT...WEEK 08 QUESTIONS | CLARIFICATIONS | HELP Student Office Hours: Schedule Meeting with Julie o By Appointment o Drop-In Times Available (on-campus) Email: [email protected] RRCC On Campus Tutoring: https://www.rrcc.edu/learning- commons/tutoring 24/7 Online Tutoring: D2L > Content > Resources for Help

Use Quizgecko on...
Browser
Browser