# Computer Programming

Size: px
Start display at page:

Download "Computer Programming"

Transcription

1 Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: while and do while statements in C++ Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1

2 Quick Recap of Relevant Topics Iteration idioms in programming Necessity and convenience of iteration Glimpse of iteration constructs in C++ 2 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

3 Overview of This Lecture Iteration using while and do while statements in C++ break statement in loops 3 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

4 Recall Generic Iteration Construct Part of program before iteration Iteration initialization (setting up initial values, etc) Iterate/Repeat as long as a logical condition stays true { Block of statements Optional instructions to execute at end of every iteration Part of program after iteration Loop Body Loop Condition Loop Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 4

5 while Statement in C++ Part of program before iteration Part of program before execution Iteration initialization (setting up initial values, etc) while (loop condition) { Block of statements Block of statements (Body of while loop) Optional instructions to execute at end of every iteration Part of program after iteration Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 5

6 Flowchart Representation of while Part of program before iteration Logical expression (loop condition) FALSE TRUE Loop Body Part of program after iteration Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 6

7 Points To Remember About while while (loop condition) { Loop Body Loop condition checked before executing loop body Can lead to zero executions of loop body Number of times loop condition is checked = Number of times loop body executed + 1, if loop terminates If loop condition is not changed in loop body, inifinite loop (non-terminating program) possible Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 7

8 Back To Our Problem Read number of students in CS101, read quiz 1 marks of all CS101 students and print their sum, average, maximum and minimum Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 8

9 Flowchart Representation Read numstudents Initialize aggregates (sum, min, max) Initialize count to 1 count <= numstudents? FALSE Ask for marks Read marks Update aggregates Increment count TRUE Compute average Print aggregates Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 9

10 C++ program with while int main() { int marks, sum = 0, min, max, numstudents; float average, count; // Variable declarations cout << Give number of students: ; cin >> numstudents; count = 1.0; // Count of student marks processed while (count <= numstudents) { cout << Give marks of student << count << : ; // Update sum, max, min count = count + 1; average = sum/count; // Print average, sum, min, max return 0; cin >> marks; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 10

11 C++ program with while int main() { int marks, sum = 0, min, max, numstudents; float average, count; // Variable declarations cout << Give number of students: ; cin >> numstudents; count = 1.0; // Count of student marks processed while (count <= numstudents) { cout << Give marks of student << count << : ; // Update sum, max, min count = count + 1; average = sum/count; // Print average, sum, min, max return 0; cin >> marks; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 11

12 C++ program with while int main() { int marks, sum = 0, min, max, numstudents; float average, count; // Variable declarations cout << Give number of students: ; cin >> numstudents; count = 1.0; // Count of student marks processed while (count <= numstudents) { cout << Give marks of student << count << : ; // Update sum, max, min count = count + 1; average = sum/count; // Print average, sum, min, max return 0; cin >> marks; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 12

13 C++ program with while int main() { sum = sum + marks; int marks, sum = 0, min, max, numstudents; if (count == 1) { min = marks; max = marks; float average, count; // Variable declarations else { cout << Give number of students: ; min cin = >> (min numstudents; > marks)? marks: min; count = 1.0; // Count of student marks max processed = (max < marks)? marks: max; while (count <= numstudents) { cout << Give marks of student << count << : ; cin >> marks; // Update sum, max, min count = count + 1; average = sum/count; // Print average, sum, min, max return 0; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 13

14 C++ program with while int main() { int marks, sum = 0, min, max, numstudents; float average, count; // Variable declarations cout << Give number of students: ; cin >> numstudents; count = 1.0; // Count of student marks processed while (count <= numstudents) { cout << Give marks of student << count << : ; // Update sum, max, min count = count + 1; average = sum/count; // Print average, sum, min, max return 0; cin >> marks; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 14

15 Accumulation or Aggregation in Loops int main() { int marks, sum = 0, min, max, numstudents; float average, count; // Variable declarations cout << Give number of students: ; cin >> numstudents; count = 1.0; // Count of student marks processed while (count <= numstudents) { cout << Give marks of student << count << : ; cin >> marks; // Update sum, max, min count = count + 1; average = sum/count; // Print average, sum, min, max return 0; Inputs were provided one after another: streaming inputs We did not remember all inputs seen so far, only some aggregates Aggregates: summary of streaming inputs seen so far so that we can compute final result Accumulation or aggregation: key to programming with loops for streaming inputs Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 15

16 A Variant Of Our Problem Read quiz 1 marks of CS101 students one at a time Stop reading if is entered as marks Print number of marks entered, sum, average, maximum and minimum Difference from earlier version: We do not know a priori how many marks will be entered Indicated by special end-of-inputs marks (-1000) We ll know when to stop only after reading as marks Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 16

17 Modifying Our Earlier C++ program int main() { int marks, sum = 0, min, max; float average, count; // Variable declarations count = 1.0; // Count of student marks processed while (true) { cout << Give marks of student << count << : ; if (marks == -1000) { exit loop else { Update sum, max, min count = count + 1; average = sum/(count 1); // Print count 1, average, sum, min, max return 0; cin >> marks; Infinite loop!!! C++ provides an easy way to do this Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 17

18 break Statement In while Loop while (true) { cout << Give marks of student << count << : ; cin >> marks; if (marks == -1000) { break; else { Update sum, max, min count = count + 1; Recall break from switch case Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 18

19 Can We Do Without break? bool exitflag = false; while (! exitflag) { cout << Give marks of student << count << : ; cin >> marks; if (marks == -1000) {exitflag = true; else { Update sum, max, min count = count + 1; Include within else block to preserve behaviour of program with break Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 19

20 Convenience Of break In Loops while (true) { cout << Give marks of student << count << : ; cin >> marks; if (marks == -1000) { break; else { Update sum, max, min count = count + 1; break avoids such annoying complications Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 20

21 Recap: while Statement in C++ Part of program before iteration Part of program before execution Iteration initialization (setting up initial values, etc) while (loop condition) { Block of statements Block of statements (Body of while loop) Optional instructions to execute at end of every iteration Part of program after iteration Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 21

22 do while Statement in C++ Part of program before iteration Part Iteration of program initialization before (setting execution up initial values, etc) do { Block of statements Optional Block of instructions statements to execute (Body at of end do-while of every iteration loop) while (loop condition) Part of program after iteration Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 22

23 do while Statement Flowchart Part of program before iteration Loop Body Logical expression (loop condition) FALSE TRUE Loop Body Part of program after iteration Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 23

24 From while to do while while (loop condition) { Loop Body if (loop condition) { do { Loop Body while (loop condition); Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 24

25 From do while to while Loop Body; while (loop condition) { Loop Body do { Loop Body while (loop condition); break statements can be used in do while in same manner as in while Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 25

26 do while vs while Almost the same Prefer do while when we are guaranteed to execute loop body at least once Prefer while if loop body may not be executed at all Programmer s choice Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 26

27 Summary while statement in C++ do while statement in C++ Use of break statements Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 27

### Computer Programming

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: for statement in C++ Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1 Quick Recap

More information

### Computer Programming

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Iteration Idioms: Motivation Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1

More information

### Lecture Transcript While and Do While Statements in C++

Lecture Transcript While and Do While Statements in C++ Hello and welcome back. In this lecture we are going to look at the while and do...while iteration statements in C++. Here is a quick recap of some

More information

### Computer Programming

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Lectures 20, 21, 22 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1 A Generic Iteration

More information

### Computer Programming

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Introduction to Pointers Part 1 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

More information

### Computer Programming

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Pointers and Dynamic Memory Part 1 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

More information

### Computer Programming

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Structure of a Simple C++ Program Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

More information

### Computer Programming

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Quiz and Practice Questions on Classes Part 1 Dr. Deepak B. Phatak & Dr. Supratik

More information

### Computer Programming

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: An Example with Sequential and Conditional Execution Dr. Deepak B. Phatak & Dr.

More information

### Computer Programming

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Parameter Passing in Function Calls Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

More information

### Computer Programming

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Recap of Arrays and Simultaneous Equations (S31 S35) Dr. Deepak B. Phatak & Dr.

More information

### Computer Programming

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Merge Sort in C++ and Its Analysis Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

More information

### Computer Programming

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Recursive Functions Part B Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1 Quick

More information

### Computer Programming

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Programming using Structures Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1

More information

### Computer Programming

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session : C++ Standard Library The string Class Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

More information

### Computer Programming

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Character Strings Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1 Quick Recap

More information

### Computer Programming

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Recap of Function Calls and Parameter Passing Dr. Deepak B. Phatak & Dr. Supratik

More information

### Computer Programming

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: An Example Program using Member Functions Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

More information

### LECTURE 5 Control Structures Part 2

LECTURE 5 Control Structures Part 2 REPETITION STATEMENTS Repetition statements are called loops, and are used to repeat the same code multiple times in succession. The number of repetitions is based on

More information

### while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

Week 7: Advanced Loops while Loops in C++ (review) while (expression) may be a compound (a block: {s) Gaddis: 5.7-12 CS 1428 Fall 2015 Jill Seaman 1 for if expression is true, is executed, repeat equivalent

More information

### Computer Programming

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Inheritance in C++ Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1 Overview

More information

### Computer Programming

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Computer Architecture Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1 Quick

More information

### Computer Programming

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Object-oriented Programming using Member Functions Dr. Deepak B. Phatak & Dr. Supratik

More information

### Computer Programming

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session : Template Class list Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1 Quick Recap

More information

### Computer Programming

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session : Concluding Comments on C++ Standard Library Dr. Deepak B. Phatak & Dr. Supratik

More information

### 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

### Computer Programming

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Advanced Graphics Events Guest Lecturer: Dr. Abhiram Ranade Quick recap

More information

### Lecture 7: General Loops (Chapter 7)

CS 101: Computer Programming and Utilization Jul-Nov 2017 Umesh Bellur (cs101@cse.iitb.ac.in) Lecture 7: General Loops (Chapter 7) The Need for a More General Loop Read marks of students from the keyboard

More information

### 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

### Computer Programming

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Access Control and Introduction to Classes Dr. Deepak B. Phatak & Dr. Supratik

More information

### CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

CS 101 Computer Programming and utilization Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building Bombay Lecture 4, Conditional execution of instructions Friday, August

More information

### 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

### *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

### Lecture 7 Tao Wang 1

Lecture 7 Tao Wang 1 Objectives In this chapter, you will learn about: Interactive loop break and continue do-while for loop Common programming errors Scientists, Third Edition 2 while Loops while statement

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 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

### CS 105 Lecture 5 Logical Operators; Switch Statement. Wed, Feb 16, 2011, 5:11 pm

CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm 1 16 quizzes taken Average: 37.9 Median: 40.5 Quiz 1 Results 16 Scores: 45 45 44 43 43 42 41 41 40 36 36 36 34 31 28 21 Avg

More information

### 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

### 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

### 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

### Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Object-oriented programming 1 and data-structures CS/ENGRD 2110 SUMMER 2018 Lecture 1: Types and Control Flow http://courses.cs.cornell.edu/cs2110/2018su Lecture 1 Outline 2 Languages Overview Imperative

More information

### 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

### Control Structures. Control Structures Conditional Statements COMPUTER PROGRAMMING. Electrical-Electronics Engineering Dept.

EEE-117 COMPUTER PROGRAMMING Control Structures Conditional Statements Today s s Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical

More information

### 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

### Control Structures of C++ Programming (2)

Control Structures of C++ Programming (2) CISC1600/1610 Computer Science I/Lab Fall 2016 CISC 1600 Yanjun Li 1 Loops Purpose: Execute a block of code multiple times (repeat) Types: for, while, do/while

More information

### 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

### Quiz Determine the output of the following program:

Quiz Determine the output of the following program: 1 Structured Programming Using C++ Lecture 4 : Loops & Iterations Dr. Amal Khalifa Dr. Amal Khalifa - Spring 2012 1 Lecture Contents: Loops While do-while

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. Salamh Chapter Four: Loops 2 Chapter Goals To implement while, for

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: 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

### 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

### CS106X Handout 03 Autumn 2012 September 24 th, 2012 Getting Started

CS106X Handout 03 Autumn 2012 September 24 th, 2012 Getting Started Handout written by Julie Zelenski, Mehran Sahami, Robert Plummer, and Jerry Cain. After today s lecture, you should run home and read

More information

### 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

### Programming in C++ PART 2

Lecture 07-2 Programming in C++ PART 2 By Assistant Professor Dr. Ali Kattan 1 The while Loop and do..while loop In the previous lecture we studied the for Loop in C++. In this lecture we will cover iteration

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 In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct and use countercontrolled, sentinel-controlled,

More information

### 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

### 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

### CS112 Lecture: Repetition Statements

CS112 Lecture: Repetition Statements Objectives: Last revised 2/18/05 1. To explain the general form of the java while loop 2. To introduce and motivate the java do.. while loop 3. To explain the general

More information

### Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

Arrays: Higher Dimensional Arrays CS0007: Introduction to Computer Programming Review If the == operator has two array variable operands, what is being compared? The reference variables held in the variables.

More information

### 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.

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

### The University of Alabama in Huntsville Electrical and Computer Engineering CPE Example of Objective Test Questions for Test 4

The University of Alabama in Huntsville Electrical and Computer Engineering CPE 112 02 Example of Objective Test Questions for Test 4 True or False Name: 1. The statement switch (n) case 8 : alpha++; case

More information

### 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

### CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1(c): Java Basics (II) Lecture Contents Java basics (part II) Conditions Loops Methods Conditions & Branching Conditional Statements A

More information

### Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #16 Loops: Matrix Using Nested for Loop In this section, we will use the, for loop to code of the matrix problem.

More information

### 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

### Introduction to C++ Lecture Set 2. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 1

Introduction to C++ Lecture Set 2 Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 1 More Arithmetic Operators More Arithmetic Operators In the first session the basic arithmetic operators were introduced.

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) Objectives In this chapter, you will: Learn about repetition (looping) control structures

More information

### Unit 7. 'while' Loops

1 Unit 7 'while' Loops 2 Control Structures We need ways of making decisions in our program To repeat code until we want it to stop To only execute certain code if a condition is true To execute one segment

More information

### CS112 Lecture: Loops

CS112 Lecture: Loops Objectives: Last revised 3/11/08 1. To introduce some while loop patterns 2. To introduce and motivate the java do.. while loop 3. To review the general form of the java for loop.

More information

### 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

### Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Lecture 08-1 Programming in C++ PART 1 By Assistant Professor Dr. Ali Kattan 1 The Conditional Operator The conditional operator is similar to the if..else statement but has a shorter format. This is useful

More information

### 1 Unit 8 'for' Loops

1 Unit 8 'for' Loops 2 Control Structures We need ways of making decisions in our program To repeat code until we want it to stop To only execute certain code if a condition is true To execute one segment

More information

### 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

### Score score < score < score < 65 Score < 50

What if we need to write a code segment to assign letter grades based on exam scores according to the following rules. Write this using if-only. How to use if-else correctly in this example? score Score

More information

### 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

### Computer Programming. Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Lists, Links, and Images in HTML Recap Introduced HTML Examined some basic tags

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 Objectives To learn about the three types of loops: while for do To avoid infinite

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 Lecture 3 Winter 2011/12 Oct 25 Visual C++: Problems and Solutions New section on web page (scroll down)

More information

### 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

### Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Control Structures Lecture 4 COP 3014 Fall 2017 September 18, 2017 Control Flow Control flow refers to the specification of the order in which the individual statements, instructions or function calls

More information

### There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Introduction In the programs that we have dealt with so far, all statements inside the main function were executed in sequence as they appeared, one after the other. This type of sequencing is adequate

More information

### 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

### 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

### COMP 11 Class 17 Outline

COMP 11 Class 17 Outline Topics: Dynamic Arrays and Memory 2 Approach: Main Ideas: Discussion, Explanation, Discussion Memory allocation and pointers 1. Admin a) Sentegy Study Announcment b) Project 2A

More information

### CS110D: PROGRAMMING LANGUAGE I

CS110D: PROGRAMMING LANGUAGE I Computer Science department Lecture 5&6: Loops Lecture Contents Why loops?? While loops for loops do while loops Nested control structures Motivation Suppose that you need

More information

### Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3 Ziad Matni Dept. of Computer Science, UCSB A Word About Registration for CS16 FOR THOSE OF YOU NOT YET REGISTERED:

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. B. Outputs to the console a floating point number f1 in scientific format

More information

### Computer Programming I - Unit 5 Lecture page 1 of 14

page 1 of 14 I. The while Statement while, for, do Loops Note: Loop - a control structure that causes a sequence of statement(s) to be executed repeatedly. The while statement is one of three looping statements

More information

### 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

### Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #17. Loops: Break Statement

Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #17 Loops: Break Statement (Refer Slide Time: 00:07) In this session we will see one more feature that is present

More information

### ENGI 1020 Introduction to Computer Programming J U L Y 5, R E Z A S H A H I D I

ENGI 1020 Introduction to Computer Programming J U L Y 5, 2 0 1 0 R E Z A S H A H I D I Passing by value Recall that it is possible to call functions with variable names different than the parameters in

More information

### Compiling C++ Programs Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Compiling C++ Programs Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3 Ziad Matni Dept. of Computer Science, UCSB Compiling Programs in C++ Input and Output Streams Simple Flow

More information

### Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture 04 Programs with IO and Loop We will now discuss the module 2,

More information

### CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8

CS 302: INTRODUCTION TO PROGRAMMING Lectures 7&8 Hopefully the Programming Assignment #1 released by tomorrow REVIEW The switch statement is an alternative way of writing what? How do you end a case in

More information

### SFU CMPT Topic: Control Statements

SFU CMPT-212 2008-1 1 Topic: Control Statements SFU CMPT-212 2008-1 Topic: Control Statements Ján Maňuch E-mail: jmanuch@sfu.ca Wednesday 23 rd January, 2008 SFU CMPT-212 2008-1 2 Topic: Control Statements

More information

### 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

### Fundamentals of Programming CS-110. Lecture 2

Fundamentals of Programming CS-110 Lecture 2 Last Lab // Example program #include using namespace std; int main() { cout

More information

### Problem Solving With Loops

To appreciate the value of loops, take a look at the following example. This program will calculate the average of 10 numbers input by the user. Without a loop, the three lines of code that prompt the

More information

### Programming for Engineers Iteration

Programming for Engineers Iteration ICEN 200 Spring 2018 Prof. Dola Saha 1 Data type conversions Grade average example,-./0 class average = 23450-67 893/0298 Grade and number of students can be integers

More information

### 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

### 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