Structured Programming. Flowchart Symbols. Structured Programming. Selection. Sequence. Control Structures ELEC 330 1

Size: px
Start display at page:

Download "Structured Programming. Flowchart Symbols. Structured Programming. Selection. Sequence. Control Structures ELEC 330 1"

Transcription

1 ELEC Structured Programming Control Structures ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne Algorithm Development Conditional Expressions Selection Statements Loops 206_C3 2 Structured Programming Flowchart Symbols Sequence Sequence of steps performed one after another Selection Condition evaluated as true or false if true, one set of statements executed if false, another set of statements executed Repetition Repeat (loop through) a set of steps As long as a condition is true Operation Begin / End Input / Output Computation Comparison start main read radius area = π*radius 2 is yes radius < 0? no Symbol stop main print radius, area 206_C _C3 4 Sequence Selection start main read radius read radius area = π*radius 2 is yes radius < 0? no print radius, area stop main area = π*radius 2 print radius, area print error 206_C _C3 6

2 ELEC Repetition Structured Programming time = 0 is no time 10? yes compute velocity print velocity increment time Alternative Solutions Readable Not necessarily shortest Handle Error Conditions Test for invalid data Exit Attempt to correct Generate Test Data Cover different ranges of values Test boundary conditions 206_C _C3 8 Debugging Conditional Expressions Compile Errors Correct one or two obvious syntax errors Recompile Execution Errors Include cout statements Provide memory snapshot of key objects Relational Operators < less than <= less than or equal to > greater than >= greater than or equal to == equal to!= not equal Logical Operators && and or! not 206_C _C3 10 Operator Precedence s Precedence Operation ( ) ! (type) * / % a = 5.5 b = 1.5 k = 3 a + b >= 6.5 b - k > a a < 10 && a > < <= > >= 6 ==!= 7 && 8 9 = += -= *= /= %= 206_C _C3 12

3 ELEC Selection Statements if Statements if (condition) statement 1; if (condition) statement 1; statement 2;... statement n; if/ Statement if (condition) statement 1; statement 2; read radius is yes radius < 0? no area = π*radius 2 print radius, area print error cin >> r; if (r < 0) cout << "error"; a = PI*r*r; cout << r << a; 206_C _C3 14 Conditional Operator Practice if/ Statement if (a<b) count++; c = a + b; Conditional Statement a<b? count++ : c = a + b; If dist is less than 50.0 and time is greater than 10.0, then increment time by 2; otherwise, increment time by 2.5. if (dist < 50.0 && time > 10.0) time+=2; time+=2.5; 206_C _C3 16 Nested if/ Statements Equivalent switch Statement if (code == 10) cout << "Too hot - turn off." << endl; if (code == 11) cout << "Caution - recheck in 5 min." << endl; if (code == 13) cout << "Turn on fan." << endl; cout << "Normal." << endl; 206_C3 17 switch (code) case 10: cout << "Too hot - turn off." << endl; break; case 11: cout << "Caution - recheck in 5 min." << endl; break; case 13: cout << "Turn on fan." << endl; break; default: cout << "Normal." << endl; 206_C3 18

4 ELEC Selection Statements Practice switch Statement switch (controlling expression) case label_1: case label_2:... default: 206_C3 19 If rank equals 1 or 2 then print "Low", if rank equals 3 or 4 then print "High", otherwise print "Error". switch (rank) case 1: case 2: cout << "Low" << endl; break; case 3: case 4: cout << "High" << endl; default: cout << "Error" << endl; 206_C3 20 Loop Structures while Loop Condition evaluated before statements are executed If condition is false, statement block is skipped If condition is true, statement block is executed and condition is evaluated again Beware of infinite loops <Ctrl> C while (condition) 206_C3 21 /* */ /* Program chapter3_1 */ /* */ /* This program prints a degree-to-radian table */ /* using a while loop structure. */ #include <iostream> #include <iomanip> using namespace std; const double PI = ; 206_C3 22 int main() // Declare and initialize objects. int degrees(0); double radians; // Set formats. cout << fixed << setprecision(6); // Print radians and degrees in a loop. cout << "Degrees to Radians" << endl; while (degrees <= 360) radians = degrees*pi/180; cout << setw(6) << degrees << setw(10) << radians << endl; degrees += 30; 206_C3 23 // Windows friendly exit system("pause"); return 0; 206_C3 24

5 ELEC Loop Structures do/while Loop Condition tested at end of loop Loop always executed at least once do statements while (condition); 206_C _C3 26 Loop Structures // Print radians and degrees in a loop. cout << "Degrees to Radians" << endl; do radians = degrees*pi/180; cout << setw(6) << degrees << setw(10) << radians << endl; degrees += 30; while (degrees <= 360); for Loop Expression 1 initializes loop-control object Expression 2 specifies condition to continue loop Expression 3 specifies modification to loop-control object After execution of statement block for (exp_1; exp_2; exp_3) 206_C _C3 28 Practice // Print radians and degrees in a loop. cout << "Degrees to Radians" << endl; for (int degrees=0; degrees<=360; degrees+=30) radians = degrees*pi/180; cout << setw(6) << degrees << setw(10) << radians << endl; What is the value of count after the nested for loops are executed? int count(0); for (int k=-1; k<4; k++) for (int j=3; j>0; j--) count++; 206_C _C3 30

6 ELEC Loop Structures Structured Input Loops break Statement Immediately exit from the loop continue Statement Skip remaining statements in the current iteration Both useful when error conditions encountered Counter-controlled Loop Number of data values is known Sentinel-controlled Loop Special value indicates end of data End-of-data Loop Continues while new data is available End of file function (Ch 4) 206_C _C3 32 Counter-controlled Loop Counter-controlled Loop // Prompt user for input. cout << "Enter the number of exam scores "; cin >> counter; cout << "Enter " << counter << " exam scores separated by whitespace "; // Input exam scores using counter-controlled loop. for(int i=1; i<=counter; i++) cin >> exam_score; sum = sum + exam_score; 206_C _C3 34 Sentinel-controlled Loop Sentinel-controlled Loop input data_value data_value!=sentinel True input next data_value False 206_C3 35 // Prompt user for input. cout << "Enter exam scores separated by whitespace." << endl; cout << "Enter a negative value to indicate the end of data. "; // Input exam scores using sentinel-controlled loop. cin >> exam_score; while(exam_score >= 0) sum = sum + exam_score; count++; cin >> exam_score; 206_C3 36

7 ELEC Sentinel-controlled Loop Problem Solving Applied Weather Balloons Problem Statement Using polynomials that represent altitude and velocity, print a table using units of meters and meters per second. Find the maximum altitude and its time. Input/Output Description Starting Time Time Increment Ending Time Table of Velocities and Altitude Maximum Altitude and its Time 206_C _C3 38 Problem Solving Applied Problem Solving Applied Hand alt(t) = -0.12t t 3-380t t v(t) = -0.48t t 2-760t Starting Time t = 0 hours Ending Time t = 5 hours Time Increment 1 hour Time Altitude (m) , , , , , Velocity (m/s) _C3 39 Algorithm Development Get user input to specify times for table Generate and print conversion table Find and print maximum height and corresponding time 206_C3 40 Problem Solving Applied Algorithm Refinement Read initial, increment, final values from keyboard Set max_height and max_time to zero Print table heading and set time to initial While time is less than or equal to final Compute height and velocity Print height and velocity If height is greater than max_height Set new max_height and max_time Add increment to time Print max_time and max_height /* */ /* Program chapter3_7 */ /* */ /* This program prints a table of height and */ /* velocity values for a weather balloon. */ #include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() // Declare and initialize objects. double initial, increment, final, time, height, velocity, max_time(0), max_height(0); int loops; 206_C3 41

8 ELEC // Get user input. cout << "Enter initial value for table (in hours) \n"; cin >> initial; cout << "Enter increment between lines (in hours) \n"; cin >> increment; cout << "Enter final value for table (in hours) \n"; cin >> final; // Print report heading. cout << "\n\nweather Balloon Information \n"; cout << "Time Height Velocity \n"; cout << "(hrs) (meters) (meters/s) \n"; // Set formats. cout << fixed << setprecision(2); // Compute and print report information. // Determine number of iterations required. // Use integer index to avoid rounding error. loops = (int)( (final - initial)/increment ); for (int count=0; count<=loops; count++) time = initial + count*increment; height = -0.12*pow(time,4) + 12*pow(time,3) - 380*time*time *time + 220; velocity = -0.48*pow(time,3) + 36*time*time - 760*time ; cout << setw(6) << time << setw(10) << height << setw(10) << velocity/3600 << endl; if (height > max_height) max_height = height; max_time = time; // Print maximum height and corresponding time. cout << "\nmaximum balloon height was " << setw(8) << max_height << " meters \n"; cout << "and it occurred at " << setw(6) << max_time << " hours \n"; Testing 206_C3 45 Summary Structured Programming Algorithm Development Conditional Expressions Selection Statements Loops Problem Solving Applied End of Chapter Summary C++ Statements Style Notes Debugging Notes 206_C3 47

Outline chapter 4 control structures: REPETITION READ/STUDY SEC 4.1 TO 4.6

Outline chapter 4 control structures: REPETITION READ/STUDY SEC 4.1 TO 4.6 Outline chapter 4 control structures: REPETITION READ/STUDY SEC 4.1 TO 4.6 STUDY AND PRACTICE WITH CODING NEWBIE VIDEO 12 FOR loops 13 WHILE loops 14 DO WHILE loops in this order: The text covers While

More information

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011 The American University in Cairo Department of Computer Science & Engineering CSCI 106-07&09 Dr. KHALIL Exam-I Fall 2011 Last Name :... ID:... First Name:... Form I Section No.: EXAMINATION INSTRUCTIONS

More information

Why Is Repetition Needed?

Why Is Repetition Needed? Why Is Repetition Needed? Repetition allows efficient use of variables. It lets you process many values using a small number of variables. For example, to add five numbers: Inefficient way: Declare a variable

More information

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Computer Programming Basic Control Flow - Loops Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Objectives To learn about the three types of loops: while for do To avoid infinite

More information

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators Chapter 5: 5.1 Looping The Increment and Decrement Operators The Increment and Decrement Operators The Increment and Decrement Operators ++ is the increment operator. It adds one to a variable. val++;

More information

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank 1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A. integer B 1.427E3 B. double D "Oct" C. character B -63.29 D. string F #Hashtag

More information

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018. Week 2 Branching & Looping Gaddis: Chapters 4 & 5 CS 5301 Spring 2018 Jill Seaman 1 Relational Operators l relational operators (result is bool): == Equal to (do not use =)!= Not equal to > Greater than

More information

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A. Engineering Problem Solving With C++ 4th Edition Etter TEST BANK Full clear download (no error formating) at: https://testbankreal.com/download/engineering-problem-solving-with-c-4thedition-etter-test-bank/

More information

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

EP241 Computing Programming

EP241 Computing Programming EP241 Computing Programming Topic 4 Loops Department of Engineering Physics University of Gaziantep Course web page www.gantep.edu.tr/~bingul/ep241 Sep 2013 Sayfa 1 Introduction Loops are control structures

More information

Name SECTION: 12:45 2:20. True or False (12 Points)

Name SECTION: 12:45 2:20. True or False (12 Points) Name SECION: 12:45 2:20 rue or False (12 Points) 1. (12 pts) Circle for true and F for false: F a) Local identifiers have name precedence over global identifiers of the same name. F b) Local variables

More information

In this chapter you will learn:

In this chapter you will learn: 1 In this chapter you will learn: Essentials of counter-controlled repetition. Use for, while and do while to execute statements in program repeatedly. Use nested control statements in your program. 2

More information

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition) C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition) Objectives In this chapter, you will: Learn about repetition (looping) control structures

More information

C++ Programming: From Problem Analysis to Program Design, Third Edition

C++ Programming: From Problem Analysis to Program Design, Third Edition C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 5: Control Structures II (Repetition) Why Is Repetition Needed? Repetition allows you to efficiently use variables Can input,

More information

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed? Chapter 5: Control Structures II (Repetition) Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct and use countercontrolled, sentinel-controlled,

More information

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol. 1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol. B. Outputs to the console a floating point number f1 in scientific format

More information

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York CSc 10200 Introduc/on to Compu/ng Lecture 8 Edgardo Molina Fall 2011 City College of New York 18 The Null Statement Null statement Semicolon with nothing preceding it ; Do-nothing statement required for

More information

Programming. C++ Basics

Programming. C++ Basics Programming C++ Basics Introduction to C++ C is a programming language developed in the 1970s with the UNIX operating system C programs are efficient and portable across different hardware platforms C++

More information

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

C++ Programming Lecture 7 Control Structure I (Repetition) Part I C++ Programming Lecture 7 Control Structure I (Repetition) Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department while Repetition Structure I Repetition structure Programmer

More information

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester Programming Language Control Structures: Repetition (while) Eng. Anis Nazer Second Semester 2017-2018 Repetition statements Control statements change the order which statements are executed Selection :

More information

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition) Structures Programming in C++ Sequential Branching Repeating Loops (Repetition) 2 1 Loops Repetition is referred to the ability of repeating a statement or a set of statements as many times this is necessary.

More information

The American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010

The American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010 The American University in Cairo Computer Science & Engineering Department CSCE 106-08 Dr. KHALIL Exam II Spring 2010 Last Name :... ID:... First Name:... Form - I EXAMINATION INSTRUCTIONS * Do not turn

More information

Introduction. C provides two styles of flow control:

Introduction. C provides two styles of flow control: Introduction C provides two styles of flow control: Branching Looping Branching is deciding what actions to take and looping is deciding how many times to take a certain action. Branching constructs: if

More information

I/O Streams and Standard I/O Devices (cont d.)

I/O Streams and Standard I/O Devices (cont d.) Chapter 3: Input/Output Objectives In this chapter, you will: Learn what a stream is and examine input and output streams Explore how to read data from the standard input device Learn how to use predefined

More information

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++ CSCE 206: Structured Programming in C++ 2017 Spring Exam 2 Monday, March 20, 2017 Total - 100 Points B Instructions: Total of 13 pages, including this cover and the last page. Before starting the exam,

More information

REPETITION CONTROL STRUCTURE LOGO

REPETITION CONTROL STRUCTURE LOGO CSC 128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING REPETITION CONTROL STRUCTURE 1 Contents 1 Introduction 2 for loop 3 while loop 4 do while loop 2 Introduction It is used when a statement or a block of

More information

CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)

CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points) Name rue or False (12 Points) 1. (12 pts) Circle for true and F for false: F a) Local identifiers have name precedence over global identifiers of the same name. F b) Local variables retain their value

More information

Increment and the While. Class 15

Increment and the While. Class 15 Increment and the While Class 15 Increment and Decrement Operators Increment and Decrement Increase or decrease a value by one, respectively. the most common operation in all of programming is to increment

More information

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad Outline 1. C++ Iterative Constructs 2. The for Repetition Structure 3. Examples Using the for Structure 4. The while Repetition Structure

More information

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays Outline 13.1 Test-Driving the Salary Survey Application 13.2 Introducing Arrays 13.3 Declaring and Initializing Arrays 13.4 Constructing

More information

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++ Repetition Contents 1 Repetition 1.1 Introduction 1.2 Three Types of Program Control Chapter 5 Introduction 1.3 Two Types of Repetition 1.4 Three Structures for Looping in C++ 1.5 The while Control Structure

More information

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved Chapter Four: Loops Slides by Evan Gallagher The Three Loops in C++ C++ has these three looping statements: while for do The while Loop while (condition) { statements } The condition is some kind of test

More information

Chapter 4 - Notes Control Structures I (Selection)

Chapter 4 - Notes Control Structures I (Selection) Chapter 4 - Notes Control Structures I (Selection) I. Control Structures A. Three Ways to Process a Program 1. In Sequence: Starts at the beginning and follows the statements in order 2. Selectively (by

More information

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Dr. Khalil Exam II Fall 2011

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Dr. Khalil Exam II Fall 2011 The American University in Cairo Computer Science & Engineering Department CSCE 106 Dr. Khalil Exam II Fall 2011 Last Name :... ID:... First Name:... Form I Section No.: ( ) EXAMINATION INSTRUCTIONS *

More information

Loops and Files. of do-while loop

Loops and Files. of do-while loop L E S S O N S E T 5 Loops and Files PURPOSE PROCEDURE 1. To introduce counter and event controlled loops 2. To work with the while loop 3. To introduce the do-while loop 4. To work with the for loop 5.

More information

Chapter 2 - Control Structures

Chapter 2 - Control Structures Chapter 2 - Control Structures 1 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures 2.5 if Selection Structure 2.6 if/else Selection Structure 2.7 while Repetition Structure 2.8 Formulating

More information

Building on the foundation. Now that we know a little about cout cin math operators boolean operators making decisions using if statements

Building on the foundation. Now that we know a little about cout cin math operators boolean operators making decisions using if statements Chapter 5 Looping Building on the foundation Now that we know a little about cout cin math operators boolean operators making decisions using if statements Advantages of Computers Computers are really

More information

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator. Chapter 5: Looping 5.1 The Increment and Decrement Operators Copyright 2009 Pearson Education, Inc. Copyright Publishing as Pearson 2009 Addison-Wesley Pearson Education, Inc. Publishing as Pearson Addison-Wesley

More information

Chapter 3 Problem Solving and the Computer

Chapter 3 Problem Solving and the Computer Chapter 3 Problem Solving and the Computer An algorithm is a step-by-step operations that the CPU must execute in order to solve a problem, or to perform that task. A program is the specification of an

More information

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords. Chapter 1 File Extensions: Source code (cpp), Object code (obj), and Executable code (exe). Preprocessor processes directives and produces modified source Compiler takes modified source and produces object

More information

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char Week 1 Operators, Data Types & I/O Gaddis: Chapters 1, 2, 3 CS 5301 Fall 2016 Jill Seaman Programming A program is a set of instructions that the computer follows to perform a task It must be translated

More information

Name Section: M/W T/TH Number Definition Matching (8 Points)

Name Section: M/W T/TH Number Definition Matching (8 Points) Name Section: M/W T/TH Number Definition Matching (8 Points) 1. (8 pts) Match the words with their definitions. Choose the best definition for each word. Iteration Counter Event Counter Loop Abstract Step

More information

Problem Solving: Storyboards for User Interaction

Problem Solving: Storyboards for User Interaction Topic 6 1. The while loop 2. Problem solving: hand-tracing 3. The for loop 4. The do loop 5. Processing input 6. Problem solving: storyboards 7. Common loop algorithms 8. Nested loops 9. Problem solving:

More information

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science. Instructor: Final Exam Fall 2011

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science. Instructor: Final Exam Fall 2011 The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science Instructor: Final Exam Fall 2011 Last Name :... ID:... First Name:... Section No.: EXAMINATION

More information

Looping. Arizona State University 1

Looping. Arizona State University 1 Looping CSE100 Principles of Programming with C++, Fall 2018 (based off Chapter 5 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University

More information

Chapter 2 - Control Structures

Chapter 2 - Control Structures Chapter 2 - Control Structures 1 2.11 Assignment Operators 2.12 Increment and Decrement Operators 2.13 Essentials of Counter-Controlled Repetition 2.1 for Repetition Structure 2.15 Examples Using the for

More information

Chapter 2 - Control Structures

Chapter 2 - Control Structures Chapter 2 - Control Structures 1 Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures 2.5 if Selection Structure 2.6 if/else Selection Structure 2.7 while Repetition Structure

More information

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017 Loops! Loops! Loops! Lecture 5 COP 3014 Fall 2017 September 25, 2017 Repetition Statements Repetition statements are called loops, and are used to repeat the same code mulitple times in succession. The

More information

Introduction to C++ (Extensions to C)

Introduction to C++ (Extensions to C) Introduction to C++ (Extensions to C) C is purely procedural, with no objects, classes or inheritance. C++ is a hybrid of C with OOP! The most significant extensions to C are: much stronger type checking.

More information

BITG 1233: Introduction to C++

BITG 1233: Introduction to C++ BITG 1233: Introduction to C++ 1 Learning Outcomes At the end of this lecture, you should be able to: Identify basic structure of C++ program (pg 3) Describe the concepts of : Character set. (pg 11) Token

More information

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-3 Flow Of Control Flow of control refers to the

More information

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples. Outline Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples. 1 Arrays I Array One type of data structures. Consecutive group of memory locations

More information

Example 3. #include <iostream> using namespace std; int main()

Example 3. #include <iostream> using namespace std; int main() 1 Repetition Structure Examples Example 1 #include float number, sum=0; // number to be read cin >> number; // read first number cout

More information

do { statements } while (condition);

do { statements } while (condition); Topic 4 1. The while loop 2. Problem solving: hand-tracing 3. The for loop 4. The do loop 5. Processing input 6. Problem solving: storyboards 7. Common loop algorithms 8. Nested loops 9. Problem solving:

More information

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement Last Time Let s all Repeat Together 10/3/05 CS150 Introduction to Computer Science 1 1 We covered o Counter and sentinel controlled loops o Formatting output Today we will o Type casting o Top-down, stepwise

More information

Chapter 5: Control Structures II (Repetition)

Chapter 5: Control Structures II (Repetition) Chapter 5: Control Structures II (Repetition) 1 Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled, sentinel-controlled, flag-controlled, and

More information

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN PowerPoint Slides adapted from *Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* Copyright 2012 Pearson Education Inc. COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

More information

C/C++ Programming Lecture 7 Name:

C/C++ Programming Lecture 7 Name: 1. The increment (++) and decrement (--) operators increase or decrease a variable s value by one, respectively. They are great if all you want to do is increment (or decrement) a variable: i++;. HOWEVER,

More information

Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.

Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A. Islamic University of Gaza Computer Engineering Dept. C++ Programming For Industrial And Electrical Engineering By Instructor: Ruba A. Salamh Chapter Four: Loops 2 Chapter Goals To implement while, for

More information

CMPS 221 Sample Final

CMPS 221 Sample Final Name: 1 CMPS 221 Sample Final 1. What is the purpose of having the parameter const int a[] as opposed to int a[] in a function declaration and definition? 2. What is the difference between cin.getline(str,

More information

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

The cin Object. cout << Enter the length and the width of the rectangle? ; cin >> length >> width; The cin Object Short for console input. It is used to read data typed at the keyboard. Must include the iostream library. When this instruction is executed, it waits for the user to type, it reads the

More information

C++ Final Exam 2017/2018

C++ Final Exam 2017/2018 1) All of the following are examples of integral data types EXCEPT. o A Double o B Char o C Short o D Int 2) After the execution of the following code, what will be the value of numb if the input value

More information

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 4: Repetition Control Structure

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 4: Repetition Control Structure Learning Objectives At the end of this chapter, student should be able to: Understand the requirement of a loop Understand the Loop Control Variable () Use increment (++) and decrement ( ) operators Program

More information

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements Review: Exam 1 9/20/06 CS150 Introduction to Computer Science 1 1 Your First C++ Program 1 //*********************************************************** 2 // File name: hello.cpp 3 // Author: Shereen Khoja

More information

Name Section: M/W T/TH Number Definition Matching (6 Points)

Name Section: M/W T/TH Number Definition Matching (6 Points) Name Section: M/W T/TH Number Definition Matching (6 Points) 1. (6 pts) Match the words with their definitions. Choose the best definition for each word. Event Counter Iteration Counter Loop Flow of Control

More information

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++ CSCE 206: Structured Programming in C++ 2017 Spring Exam 1 Monday, February 13, 2017 Total - 100 Points B Instructions: Total of 11 pages, including this cover and the last page. Before starting the exam,

More information

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++ CSCE 206: Structured Programming in C++ 2017 Spring Exam 1 Monday, February 13, 2017 Total - 100 Points A Instructions: Total of 11 pages, including this cover and the last page. Before starting the exam,

More information

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 1 ARRAYS Arrays 2 Arrays Structures of related data items Static entity (same size

More information

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering Lecture 3 Winter 2011/12 Oct 25 Visual C++: Problems and Solutions New section on web page (scroll down)

More information

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following: Computer Department Program: Computer Midterm Exam Date : 19/11/2016 Major: Information & communication technology 1 st Semester Time : 1 hr (10:00 11:00) Course: Introduction to Programming 2016/2017

More information

Repetition Structures

Repetition Structures Repetition Structures Chapter 5 Fall 2016, CSUS Introduction to Repetition Structures Chapter 5.1 1 Introduction to Repetition Structures A repetition structure causes a statement or set of statements

More information

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution. Repetition Structures Introduction to Repetition Structures Chapter 5 Spring 2016, CSUS Chapter 5.1 Introduction to Repetition Structures The Problems with Duplicate Code A repetition structure causes

More information

Engineering Problem Solving with C++, Etter/Ingber

Engineering Problem Solving with C++, Etter/Ingber Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs C++, Second Edition, J. Ingber 1 Simple C++ Programs Program Structure Constants and Variables C++ Operators Standard Input

More information

causing a set of statements (the body) to be executed repeatedly. C++ provides three control structures to support iteration (or looping).

causing a set of statements (the body) to be executed repeatedly. C++ provides three control structures to support iteration (or looping). 1 causing a set of statements (the body) to be executed repeatedly. C++ provides three control structures to support iteration (or looping). Before considering specifics we define some general terms that

More information

C++ Quick Reference. switch Statements

C++ Quick Reference. switch Statements C++ Quick Reference if Statements switch Statements Array/Initializer if (condition) if (condition) else if (condition1) else if (condition2) else Loop Statements while (condition) do while (condition);

More information

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

1) What of the following sets of values for A, B, C, and D would cause the string one to be printed? Instructions: This homework assignment focuses primarily on some of the basic syntax and semantics of C++. The answers to the following questions can be determined from Chapters 6 and 7 of the lecture

More information

CSCI 1061U Programming Workshop 2. C++ Basics

CSCI 1061U Programming Workshop 2. C++ Basics CSCI 1061U Programming Workshop 2 C++ Basics 1 Learning Objectives Introduction to C++ Origins, Object-Oriented Programming, Terms Variables, Expressions, and Assignment Statements Console Input/Output

More information

Introduction to C++ Dr M.S. Colclough, research fellows, pgtas

Introduction to C++ Dr M.S. Colclough, research fellows, pgtas Introduction to C++ Dr M.S. Colclough, research fellows, pgtas 5 weeks, 2 afternoons / week. Primarily a lab project. Approx. first 5 sessions start with lecture, followed by non assessed exercises in

More information

Add Subtract Multiply Divide

Add Subtract Multiply Divide ARITHMETIC OPERATORS if AND if/else AND while LOOP Order of Operation (Precedence Part 1) Copyright 2014 Dan McElroy Add Subtract Multiply Divide + Add - Subtract * Multiply / Divide = gives the quotient

More information

Expressions, Input, Output and Data Type Conversions

Expressions, Input, Output and Data Type Conversions L E S S O N S E T 3 Expressions, Input, Output and Data Type Conversions PURPOSE 1. To learn input and formatted output statements 2. To learn data type conversions (coercion and casting) 3. To work with

More information

Chapter 6 Topics. While Statement Syntax Count-Controlled Loops Event-Controlled Loops Using the End-of-File Condition to Control Input Data

Chapter 6 Topics. While Statement Syntax Count-Controlled Loops Event-Controlled Loops Using the End-of-File Condition to Control Input Data Chapter 6 Looping Chapter 6 Topics While Statement Syntax Count-Controlled Loops Event-Controlled Loops Using the End-of-File Condition to Control Input Data 2 Chapter 6 Topics Using a While Statement

More information

Chapter 2 - Control Structures

Chapter 2 - Control Structures Chapter 2 - Control Structures 1 Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures 2.5 if Selection Structure 2.6 if/else Selection Structure 2.7 while Repetition Structure

More information

DELHI PUBLIC SCHOOL TAPI

DELHI PUBLIC SCHOOL TAPI Loops Chapter-1 There may be a situation, when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed

More information

Objectives. In this chapter, you will:

Objectives. In this chapter, you will: Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates arithmetic expressions Learn about

More information

Chapter 5: Loops and Files

Chapter 5: Loops and Files Chapter 5: Loops and Files 5.1 The Increment and Decrement Operators The Increment and Decrement Operators ++ is the increment operator. It adds one to a variable. val++; is the same as val = val + 1;

More information

A SHORT COURSE ON C++

A SHORT COURSE ON C++ Introduction to A SHORT COURSE ON School of Mathematics Semester 1 2008 Introduction to OUTLINE 1 INTRODUCTION TO 2 FLOW CONTROL AND FUNCTIONS If Else Looping Functions Cmath Library Prototyping Introduction

More information

Chapter 3. More Flow of Control

Chapter 3. More Flow of Control Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-2 Flow Of Control Flow of control refers to the

More information

Getting started with C++ (Part 2)

Getting started with C++ (Part 2) Getting started with C++ (Part 2) CS427: Elements of Software Engineering Lecture 2.2 11am, 16 Jan 2012 CS427 Getting started with C++ (Part 2) 1/22 Outline 1 Recall from last week... 2 Recall: Output

More information

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science Instructor: Dr. Khalil Final Exam Fall 2013 Last Name :... ID:... First Name:... Form

More information

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma. REVIEW cout Statement The cout statement invokes an output stream, which is a sequence of characters to be displayed to the screen. cout

More information

Ch 6. Functions. Example: function calls function

Ch 6. Functions. Example: function calls function Ch 6. Functions Part 2 CS 1428 Fall 2011 Jill Seaman Lecture 21 1 Example: function calls function void deeper() { cout

More information

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points) Name Definition Matching (8 Points) 1. (8 pts) Match the words with their definitions. Choose the best definition for each word. Relational Expression Iteration Counter Count-controlled loop Loop Flow

More information

Chapter 5: Prefix vs. Postfix 8/19/2018. The Increment and Decrement Operators. Increment and Decrement Operators in Program 5-1

Chapter 5: Prefix vs. Postfix 8/19/2018. The Increment and Decrement Operators. Increment and Decrement Operators in Program 5-1 Chapter 5: Loops and Files The Increment and Decrement Operators ++ is the increment operator. It adds one to a variable. val++; is the same as val = val + 1; ++ can be used before (prefix) or after (postfix)

More information

Repetition and Loop Statements Chapter 5

Repetition and Loop Statements Chapter 5 Repetition and Loop Statements Chapter 5 1 Chapter Objectives To understand why repetition is an important control structure in programming To learn about loop control variables and the three steps needed

More information

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College while Loops Lecture 13 Sections 5.8-5.9 Robb T. Koether Hampden-Sydney College Wed, Sep 26, 2018 Robb T. Koether (Hampden-Sydney College) while Loops Wed, Sep 26, 2018 1 / 25 1 while Loops 2 Input Loops

More information

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information