Computer Programming

Similar documents
Computer Programming

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

LECTURE 5 Control Structures Part 2

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

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

Computer Programming

Computer Programming

Lecture 7: General Loops (Chapter 7)

ENGR 1181 MATLAB 09: For Loops 2

Computer Programming

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

Advanced Computer Programming

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

Score score < score < score < 65 Score < 50

Introduction. C provides two styles of flow control:

CS111: PROGRAMMING LANGUAGE II

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

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

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

LECTURE NOTES ON PROGRAMMING FUNDAMENTAL USING C++ LANGUAGE

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

Lecture 7 Tao Wang 1

REPETITION CONTROL STRUCTURE LOGO

Quiz Determine the output of the following program:

Accelerating Information Technology Innovation

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

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

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

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

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Programming in C++ PART 2

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

Why Is Repetition Needed?

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

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

Computer Science II Lecture 1 Introduction and Background

Example: Monte Carlo Simulation 1

EP241 Computing Programming

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1

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

CS110D: PROGRAMMING LANGUAGE I

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

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

Conditionals and Loops

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

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

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

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

CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8

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

Algorithmic Thinking and Structured Programming (in Greenfoot) Teachers: Renske Smetsers Sjaak Smetsers

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

CS112 Lecture: Loops

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

Flow Control. CSC215 Lecture

Programming for Experimental Research. Flow Control

CS 112 Introduction to Programming

Repetition Structures

Programming for Engineers Iteration

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

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

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

CS1004: Intro to CS in Java, Spring 2005

CS112 Lecture: Repetition Statements

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

Add Subtract Multiply Divide

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

Chapter 5: Control Structures II (Repetition)

Fundamentals of Programming CS-110. Lecture 2

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

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

LECTURE NOTES ON PROGRAMMING FUNDAMENTAL USING C++ LANGUAGE

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

Iteration: Intro. Two types of loops: 1. Pretest Condition precedes body Iterates 0+ times. 2. Posttest Condition follows body Iterates 1+ times

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

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

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

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

1 Unit 8 'for' Loops

Transcription:

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 of Relevant Topics Iteration idioms in programming Necessity and convenience of iteration while and do while statements in C++ 2 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Overview of This Lecture Iteration using for. statement in C++ break statement in for loops Comparison of different iteration constructs in C++ 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

for Statement in C++ Semi-colons not to denote end of executable statements, but to separate three parts inside for (.. ) Part of program before iteration for (iteration initialization ; loop condition ; instructions to execute at end of every iteration) { Block of statements ( for loop body ) Part of program after iteration Note absence of semi-colon Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 5

Flowchart Representation of for Part of program before iteration Iteration Initialization Instructions at end of every iteration Logical expression (loop condition) TRUE for loop body FALSE Part of program after iteration Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 6

Points to Remember About for for (initialization; loop condition; instructions after every iteration) { for loop body Initialization code executed only once before first entry in loop Loop condition checked before executing for body Can lead to zero executions of for body Number of times loop condition is checked = Number of times for body executed + 1, if loop terminates Loop condition can be changed in for body or in instructions after every iteration Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 7

Revisiting The Quiz Marks 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 Increment count count <= numstudents? FALSE Ask for marks Read marks Update aggregates TRUE Compute average Print aggregates Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 9

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

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

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

C++ Program with for int main() { int marks, Initialization sum = 0, min, codemax, numstudents; Loop condition Instruction to float average, count; // Variable declarations execute after cout << Give number of students: ; cin >> numstudents; every iteration for (count = 1.0; count <= numstudents; count = count + 1) { cout << Give marks of student << count << : ; cin >> marks; // Update sum, max, min for loop body average = sum/count; // Print average, sum, min, max return 0; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 13

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

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

break Statement in for Loops Behaviour same as in while and do while loops Jump out of the loop immediately on executing break Recall our variant of the quiz 1 marks 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 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 16

Our C++ Program with break int main() { int marks, sum = 0, min, max; float average, count; // Variable declarations Infinite loop!!! for (count = 1.0; true ; count = count + 1) { cout << Give marks of student << count << : ; cin >> marks; if (marks == -1000) { break; else { Update sum, min, max average = sum/(count 1); // Print count 1, average, sum, min, max return 0; Jump out of for loop on executing break Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 17

Our C++ Program with break int main() { int marks, sum = 0, min, max; float average, count; // Variable declarations for (count = 1.0; true ; count = count + 1) { cout << Give marks of student << count << : ; cin >> marks; if (marks == -1000) { break; else { Update sum, min, max average = sum/(count 1); // Print count 1, average, sum, min, max return 0; These instructions skipped after executing break Jump out of for loop on executing break Next statement executed after break Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 18

for Loops with Empty Parts int main() { Skipping loop condition in for int marks, sum = 0, min, max; equivalent to true loop condition float average, count; // Variable declarations for (count = 1.0; true ; count = count + 1) { cout << Give marks of student << count << : ; cin >> marks; if (marks == -1000) { break; else { Update sum, min, max average = sum/(count 1); // Print count 1, average, sum, min, max return 0; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 19

for Loops with Empty Parts ; for ( count = 1.0 ; true ; count = count + 11) { cout << Give marks of student << count << : ; cin >> marks; if (marks == -1000) { break; else { Update sum, min, max ; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 20

From for to while for (initialization; loop condition; instraftereveryiteration) { for Loop Body Initialization ; while (loop condition) { for Loop Body; instraftereveryiteration; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 21

From while to for for ( ; loop condition; ) { while Loop Body while (loop condition) { while Loop Body for to do while, and do while to for can be done by recalling the transformation of while to and from do while Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 22

for vs while for (count = 1.0; count <= numstudents; count = count + 1) { // Process marks Book-keeping cleanly isolated Real computation we want to do count = 1.0; while (count <= numstudents) { // Process marks count = count + 1; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 23

for vs while for (count = 1.0; count <= numstudents; count = count + 1) { // Process marks count = 1.0; while (count <= numstudents) { // Process marks count = count + 1; Real computation we want to do Book-keeping mixed up Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 24

for vs while Often a programmer s choice dependent on the context In general, a good idea to separate book-keeping from real computation Prefer for loops However, loop condition may not simply be based on bookkeeping Real computation in loop body may determine loop condition Prefer while loops Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 25

Summary for statement in C++ Variants of for statement Use of break statement in for statements Comparison with while and do while statements Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 26