Computer Programming

Similar documents
Computer Programming

Computer Programming

Lecture Transcript While and Do While Statements in C++

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

LECTURE 5 Control Structures Part 2

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

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

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

Computer Programming

Lecture 7: General Loops (Chapter 7)

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

Computer Programming

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

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

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

Lecture 7 Tao Wang 1

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

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

REPETITION CONTROL STRUCTURE LOGO

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

Increment and the While. Class 15

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

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

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

Why Is Repetition Needed?

Control Structures of C++ Programming (2)

Chapter 5: Loops and Files

Quiz Determine the output of the following program:

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

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

Introduction. C provides two styles of flow control:

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

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

Programming in C++ PART 2

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

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

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

CS112 Lecture: Repetition Statements

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

Repetition Structures

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

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

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

CS111: PROGRAMMING LANGUAGE II

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

C++ Final Exam 2017/2018

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

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

Unit 7. 'while' Loops

CS112 Lecture: Loops

do { statements } while (condition);

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

1 Unit 8 'for' Loops

Chapter 5: Control Structures II (Repetition)

Score score < score < score < 65 Score < 50

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

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

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

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

EP241 Computing Programming

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

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

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

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

COMP 11 Class 17 Outline

CS110D: PROGRAMMING LANGUAGE I

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

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.

Computer Programming I - Unit 5 Lecture page 1 of 14

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

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

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

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

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

CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8

SFU CMPT Topic: Control Statements

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

Fundamentals of Programming CS-110. Lecture 2

Problem Solving With Loops

Programming for Engineers Iteration

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

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

Transcription:

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

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,

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,

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

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

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

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

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

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

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

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

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

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

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

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

A Variant Of Our Problem Read quiz 1 marks of CS101 students one at a time Stop reading if -1000 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 -1000 as marks Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 16

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

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

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

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

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

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

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

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

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

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

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