Lecture Transcript While and Do While Statements in C++

Size: px
Start display at page:

Download "Lecture Transcript While and Do While Statements in C++"

Transcription

1 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 of the relevant topics, we have already studied. We have looked at iteration idioms in programming. We have seen that they are necessary to solve some problems and they are also convenient in other situations. And we have also seen a very brief glimpse of iteration constructs in C++. In this lecture we are going to look at, in more detail, the while and do-while statements in C++. And we are also going to look at the usage of break statements within while and do-while. Now recall this generic iteration construct that we have studied in a previous lecture. So in general, we have a part of the program, that is executed before the iteration. And then we have some initialization, which is iteration specific, and then the actual iteration happens, where we iterate or repeat as long as a logical condition stays true. And what do we iterate over? A block of statements. And in some cases there are some additional instructions that are executed at the end of every iteration. After that, the part of the program that must be executed at the end of this iteration appears. So we had seen earlier, that this part of the code is usually called a loop. This logical condition is also called a loop condition, and the block of statements, along with instructions to execute at the end of every iteration is often called loop body. Now, in C++, we can implement this using the while construct as follows. We could say while (loop condition), block of statements and the optional instructions if any, that had to be executed at the end of every iteration, can be grouped with this block of statements. Similarly, the initialization part can be grouped with the part of the program before iteration. So we have a part of the program before execution of the loop. We have the while loop, within which, we will call both, the block of statements and statements to be executed at the end of every iteration as the body of the while loop, and then we have the part of the program after the while loop. As a flowchart, this can be represented in this manner. We have the part of the program before iteration, part of the program after iteration, and then we check for this loop condition. If it is true, we go through the loop body and again come and check for the loop condition, and at any time the loop condition becomes false when we are doing this check, we can exit from the loop, and execute the part of the program after iteration. So here is simple animation of how control might flow through a while loop. This red dot indicates the control flow. So, the control might flow around the loop a few times and then come out of the loop. Now the points to remember about while loops in C++ are that the loop condition is checked before executing the loop body. So if the loop condition is false, the very first 1

2 time we are checking it the loop body will not be executed at all. If the loop indeed terminates then the number of times the loop condition is checked is exactly one more than the number of times the loop body is executed. And where does this additional one come from? This additional one comes from the last time the loop condition is checked when it evaluates to false, and then you exit from this loop without executing the loop body. An important point to remember is that if the this loop condition is not changed at all within the loop body, then once you ve entered the loop and you come back and check the loop condition, it will always evaluate to the same truth value as it was in the previous iteration. So if you have entered the loop once, the loop condition must have evaluated to true and in the loop body if you have not changed the loop condition, when you come back and evaluate it again, it will again evaluate to true. And you will again enter the loop body and this will continue forever. You will get, effectively what is called, an infinite loop or a non-terminating program. So you must be very careful when you are using while loops to ensure that the loop body does indeed change the loop condition so that you can finally exit from this loop. Of course there are situations where we might want the program to loop a very large number of times or even infinitely many times. But these are special cases of programs which we will see only later in the course. Now if you look back at the problem that we were trying to solve, when we introduced iteration idioms in programming we had said something like this: That you read the number of students in CS101, read quiz 1, marks of all these students and print their sum, average, maximum and minimum. Now using a while loop, we could solve this and this is kind of obvious to see if I just try to represent the flow of control through this flowchart and if you recall, this is exactly how the flowchart for a while loop looks like. So I am going to read the number of students, initialize aggregates, initialize the counter to 1. Check if the value of this counter which denotes how many quiz marks have been processed so far. If it is less than or equal to the number of students, we go back, ask for the marks, read the marks, update aggregates, increment the count and come back and again check whether we have input the number of students, whether we have input marks for all students, and once we have done that we can exit through this part and compute the average and print aggregates. Now if you go back and look at the flowchart representation for a while loop, it looks exactly like this. Here is the loop condition. Here is the loop body. Here is the program before the while loop. Here is the program after the while loop. So since our control flow matches exactly the pattern, flowchart pattern, for a while loop, we are going to implement it using a while loop. So this is how our C++ program with while might look like. We have our main program and then we have some variable declarations. In this particular case, I have declared the variable count to also be of type float and I will soon explain why that is the case. We have initialized sum to zero during the declaration itself. We ask for the number of students, read the number of students, initialize count to 1.0 Count basically keeps track of the number of students whose marks have been processed so far and then we have this while loop. While count is less than or equal to number of students, we re going to ask for the marks of the student, input the marks, update the values of sum, max and min, increment the count and go around the loop until this logical condition is evaluated to false. 2

3 Now what does updating sum, max and min mean? We have already seen this in an earlier lecture. To sum, we add the currently read value of marks and if the value of count is 1, we update min and max, straight away by the value of marks. Otherwise we update min and max through conditional expressions. After we are done reading all the student s marks and computing the aggregates, then we compute the value of average which is sum divided by count. And here you will recall, that sum is an integer and since I had declared count to be a float, therefore sum divided by count will be a float data type, and therefore I will be able to retain any fractional part in the average when I do this division. So this was the reason why we had declared count to be of type float. Average is of course of type float and once we have computed the average we can print the average, sum, min and max and then return control back to the operating system. Now if you look at this loop, what is happening inside this loop is that we are reading marks for each student and then these marks were being provided one after the other. They were not being provided all together. So, this is also sometimes called Streaming inputs. But the important point is that we did not remember all the inputs that we have seen so far. We only remembered some aggregates of these inputs: in particular sum, max and min. So what are aggregates in general? This is, these are some kinds of summary of the streaming inputs we have seen so far such that we don t need to remember the actual values of the inputs but we can just use these summary values to compute the final result. And in fact I want to highlight this point here that accumulation or aggregation is key to programming with loops for streaming inputs. You cannot possibly remember all the inputs that are streaming in. So you have to accumulate or aggregate them and using these aggregates you have to compute whatever you want to compute. Now let s look at a variant of our problem. Earlier we had said that we re going to provide the number of students in the class as an input. Now suppose somebody said that: Well, I cannot give that as an input, So what you re required to do is you re required to read off the marks of CS101 students, one at a time, and then if at any point of time I provide as the input for marks, then you must stop reading. And you must then print out the number of marks entered, their sum, average, minimum and maximum. Now what is the difference of this problem from the earlier version? We do not know a priori how many marks will be entered. Earlier we knew exactly how many students were there and so we could iterate the while loop that many times. In this case, the end of inputs is indicated by a special marks which is And we ll know, when to stop only after we read this special marks. So how could we modify our earlier C++ program to solve this problem? So once again we declare marks, sum, min, max. Declare average, count. And then we have our little while loop over here. But in this while loop, I do not know when I am going to come out of this while loop until I have actually read the marks. So I am going to put while (true) here. Remember, here we can put any logical expression. True is also a logical constant and hence a logical expression, so I can put while (true). But then notice that what this means is that this condition will never evaluate to false, so the loop condition will be always true which means this looks like an infinite loop. So this is an interesting case where we might want to have an infinite loop and then we will see where we, how we will actually come out of this loop. So we say while (true) because we don t 3

4 know how many student marks to read. And then we say, Give marks of student, read in the marks, and if the marks is then I want to exit the loop by some means, otherwise I want to update sum, max and min like I had done earlier, increment count and then loop back again. And finally I have to calculate the average, in this case it is sum divided by count - 1. Because note that, when I exit from this loop by reading as the marks, I have already incremented the value of count at the end of the previous iteration. So even if I don t execute this loop even once, the value of count is 1, if I execute this loop once, the value of count is 2 and so on. And therefore I need to divide by count - 1 when I m computing the average. And finally I print all of the desired quantities. Now how do we exit from this loop when marks is equal to -1000? It turns out that C++ provides an easy way to do this. In fact that is called a break statement. So I could simply say that if marks is then break and you would recall that we had also used the break statement when we discussed switch-case statements in an earlier lecture. Now can we really do without breaks? Or are breaks absolutely essential? So it turns out, that we can do without breaks. For example I could say that, let me use an exitflag which is false to begin with. And then I m going to iterate as many times as the exitflag remains false, and then if marks becomes I ll set this exitflag to true. So at the next time when I go back to check this condition, this condition which is negation of exitflag, evaluates to false. However note that, this incrementing of count must now appear within the else part because if I had a break here then when I exited from the loop, count would not have been incremented. But if I use an exitflag and if this incrementing of count is outside this else block then it would be incremented even after exitflag is set to true. Count would be incremented and only then would I exit this loop. So if I want to preserve behaviour with respect to the previous code, I have to include break within the else block. And using break provides us the convenience of avoiding such annoying complications of putting code that in any case I want to execute before the next iteration is executed. I can now put this code outside the else block if I use the break statement over here. Now recap, this was our while statement in C++. There is a very similar construct that we can use in C++ called do-while. Now in a do-while statement, it s basically the same as the while statement except that this block of the statements is executed at least once. So the flow chart for a do-while statement looks like this: We have the program before iteration, the program after iteration, the loop body, there s a loop condition and then the loop - the same loop body again. And so the control flows like this. So we execute the loop body as many times as is needed but at least once, and then when the condition evaluates to false we come out of the iteration. Now what is the relation between a while and a do-while? If I give you a while loop, it can be easily converted to a do-while by basically checking the loop condition first and if the loop condition is true then I can put the do-while inside. And similarly, if I give you a do-while loop, I can easily implement it as a while loop while making sure that the loop body is executed at least once. And break statements can of course be used in a do-while in the same manner as they are used in a while statement. So what is the comparison between while and do-while? They are almost the same, and it s really left to the programmer s choice. We prefer to use do-while 4

5 when we know that we re going to execute the loop body at least once. Otherwise we prefer to use while. So in summary, we looked at the while statement in C++, do-while statement in C++ and we also looked at the use of break statements. Thank You! 5

Computer Programming

Computer Programming 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,

More information

Computer Programming

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 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 5 Control Structures Part 2

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

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur Lecture 18 Switch Statement (Contd.) And Introduction to

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

(Refer Slide Time: 00:26)

(Refer Slide Time: 00:26) Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute Technology, Madras Module 07 Lecture 07 Contents Repetitive statements

More information

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur Lecture 15 Branching : IF ELSE Statement We are looking

More information

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture 05 I/O statements Printf, Scanf Simple

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

CS112 Lecture: Repetition Statements

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

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

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

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

Switching Circuits and Logic Design Prof. Indranil Sengupta Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Switching Circuits and Logic Design Prof. Indranil Sengupta Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Switching Circuits and Logic Design Prof. Indranil Sengupta Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture - 02 Octal and Hexadecimal Number Systems Welcome

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

CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I CS 106 Introduction to Computer Science I 06 / 04 / 2015 Instructor: Michael Eckmann Today s Topics Questions / comments? Calling methods (noting parameter(s) and their types, as well as the return type)

More information

Information Science 1

Information Science 1 Information Science 1 Fundamental Programming Constructs (1) Week 11 College of Information Science and Engineering Ritsumeikan University Topics covered l Terms and concepts from Week 10 l Flow of control

More information

Programming for Engineers Iteration

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

Compiler Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Compiler Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore Compiler Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore Module No. # 10 Lecture No. # 16 Machine-Independent Optimizations Welcome to the

More information

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

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

CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I CS 106 Introduction to Computer Science I 02 / 15 / 2016 Instructor: Michael Eckmann Questions? Comments? Loops to repeat code while loops for loops do while loops Today s Topics Logical operators Example

More information

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #29 Arrays in C (Refer Slide Time: 00:08) This session will learn about arrays in C. Now, what is the word array

More information

Programming and Data Structure

Programming and Data Structure Programming and Data Structure Dr. P.P.Chakraborty Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture # 09 Problem Decomposition by Recursion - II We will

More information

(Refer Slide Time: 01.26)

(Refer Slide Time: 01.26) Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture # 22 Why Sorting? Today we are going to be looking at sorting.

More information

(Refer Slide Time: 1:27)

(Refer Slide Time: 1:27) Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture 1 Introduction to Data Structures and Algorithms Welcome to data

More information

Discussion 11. Streams

Discussion 11. Streams Discussion 11 Streams A stream is an element and a promise to evaluate the rest of the stream. You ve already seen multiple examples of this and its syntax in lecture and in the books, so I will not dwell

More information

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

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

Theory of control structures

Theory of control structures Theory of control structures Paper written by Bohm and Jacopini in 1966 proposed that all programs can be written using 3 types of control structures. Theory of control structures sequential structures

More information

(Refer Slide Time: 00:02:02)

(Refer Slide Time: 00:02:02) Computer Graphics Prof. Sukhendu Das Dept. of Computer Science and Engineering Indian Institute of Technology, Madras Lecture - 20 Clipping: Lines and Polygons Hello and welcome everybody to the lecture

More information

Programming in C++ PART 2

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

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02 Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02 Hello, in this lecture we will learn about some fundamentals concepts of java.

More information

Information Science 1

Information Science 1 Topics covered Information Science 1 Fundamental Programming Constructs (1) Week 11 Terms and concepts from Week 10 Flow of control and conditional statements Selection structures if statement switch statement

More information

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

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

COMP combinational logic 1 Jan. 18, 2016

COMP combinational logic 1 Jan. 18, 2016 In lectures 1 and 2, we looked at representations of numbers. For the case of integers, we saw that we could perform addition of two numbers using a binary representation and using the same algorithm that

More information

CS1 Lecture 5 Jan. 25, 2019

CS1 Lecture 5 Jan. 25, 2019 CS1 Lecture 5 Jan. 25, 2019 HW1 due Monday, 9:00am. Notes: Do not write all the code at once before starting to test. Take tiny steps. Write a few lines test... add a line or two test... add another line

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

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #06 Loops: Operators

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #06 Loops: Operators Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #06 Loops: Operators We have seen comparison operators, like less then, equal to, less than or equal. to and

More information

(Refer Slide Time: 05:25)

(Refer Slide Time: 05:25) Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering IIT Delhi Lecture 30 Applications of DFS in Directed Graphs Today we are going to look at more applications

More information

CS61A Notes Disc 11: Streams Streaming Along

CS61A Notes Disc 11: Streams Streaming Along CS61A Notes Disc 11: Streams Streaming Along syntax in lecture and in the book, so I will not dwell on that. Suffice it to say, streams is one of the most mysterious topics in CS61A, trust than whatever

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 Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture - 08 Constants and Inline Functions Welcome to module 6 of Programming

More information

Text Input and Conditionals

Text Input and Conditionals Text Input and Conditionals Text Input Many programs allow the user to enter information, like a username and password. Python makes taking input from the user seamless with a single line of code: input()

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 Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture 10 Reference and Pointer Welcome to module 7 of programming in

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

10/24/2016. We Can Perform Any Computation with an FSM. ECE 120: Introduction to Computing. Find the Minimum Value Among Ten Integers

10/24/2016. We Can Perform Any Computation with an FSM. ECE 120: Introduction to Computing. Find the Minimum Value Among Ten Integers University of Illinois at Urbana-Champaign Dept. of Electrical and Computer Engineering ECE 120: Introduction to Computing From FSM to Computer We Can Perform Any Computation with an FSM Let s build an

More information

CS61A Summer 2010 George Wang, Jonathan Kotker, Seshadri Mahalingam, Eric Tzeng, Steven Tang

CS61A Summer 2010 George Wang, Jonathan Kotker, Seshadri Mahalingam, Eric Tzeng, Steven Tang CS61A Notes Week 6B: Streams Streaming Along A stream is an element and a promise to evaluate the rest of the stream. You ve already seen multiple examples of this and its syntax in lecture and in the

More information

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

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

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

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage: Discussion 1H Notes (Week 3, April 14) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs31 More on Arithmetic Expressions The following two are equivalent:! x = x + 5;

More information

Lecture 7 Tao Wang 1

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

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

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

CS110D: PROGRAMMING LANGUAGE I

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

Intro. Scheme Basics. scm> 5 5. scm>

Intro. Scheme Basics. scm> 5 5. scm> Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if

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 Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture - 33 Overloading Operator for User - Defined Types: Part 1 Welcome

More information

CS112 Lecture: Loops

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

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #13. Loops: Do - While

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #13. Loops: Do - While Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #13 Loops: Do - While So far we have been using while loops in C, now C programming language also provides you

More information

Computer Programming I - Unit 5 Lecture page 1 of 14

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

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

Laboratory 5: Implementing Loops and Loop Control Strategies

Laboratory 5: Implementing Loops and Loop Control Strategies Laboratory 5: Implementing Loops and Loop Control Strategies Overview: Objectives: C++ has three control structures that are designed exclusively for iteration: the while, for and do statements. In today's

More information

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur Lecture 17 Switch Statement (Refer Slide Time: 00:23) In

More information

introduction to Programming in C Department of Computer Science and Engineering Lecture No. #40 Recursion Linear Recursion

introduction to Programming in C Department of Computer Science and Engineering Lecture No. #40 Recursion Linear Recursion introduction to Programming in C Department of Computer Science and Engineering Lecture No. #40 Recursion Linear Recursion Today s video will talk about an important concept in computer science which is

More information

Loops. CSE 114, Computer Science 1 Stony Brook University

Loops. CSE 114, Computer Science 1 Stony Brook University Loops CSE 114, Computer Science 1 Stony Brook University http://www.cs.stonybrook.edu/~cse114 1 Motivation Suppose that you need to print a string (e.g., "Welcome to Java!") a user-defined times N: N?

More information

(Refer Slide Time: 0:32)

(Refer Slide Time: 0:32) Digital Image Processing. Professor P. K. Biswas. Department of Electronics and Electrical Communication Engineering. Indian Institute of Technology, Kharagpur. Lecture-57. Image Segmentation: Global Processing

More information

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

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Programming in C++ Indian Institute of Technology, Kharagpur Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Programming in C++ Indian Institute of Technology, Kharagpur Lecture 14 Default Parameters and Function Overloading

More information

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #43 Multidimensional Arrays In this video will look at multi-dimensional arrays. (Refer Slide Time: 00:03) In

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

LESSON 3 CONTROL STRUCTURES

LESSON 3 CONTROL STRUCTURES LESSON CONTROL STRUCTURES PROF. JOHN P. BAUGH PROFJPBAUGH@GMAIL.COM PROFJPBAUGH.COM CONTENTS INTRODUCTION... Assumptions.... Logic, Logical Operators, AND Relational Operators..... - Logical AND (&&) Truth

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

MA 1128: Lecture 02 1/22/2018

MA 1128: Lecture 02 1/22/2018 MA 1128: Lecture 02 1/22/2018 Exponents Scientific Notation 1 Exponents Exponents are used to indicate how many copies of a number are to be multiplied together. For example, I like to deal with the signs

More information

More About WHILE Loops

More About WHILE Loops More About WHILE Loops http://people.sc.fsu.edu/ jburkardt/isc/week04 lecture 07.pdf... ISC3313: Introduction to Scientific Computing with C++ Summer Semester 2011... John Burkardt Department of Scientific

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

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n) Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Module 10A Lecture - 20 What is a function?

More information

Looping Subtasks. We will examine some basic algorithms that use the while and if constructs. These subtasks include

Looping Subtasks. We will examine some basic algorithms that use the while and if constructs. These subtasks include 1 Programming in C Looping Subtasks We will examine some basic algorithms that use the while and if constructs. These subtasks include Reading unknown quantity of data Counting things Accumulating (summing)

More information

6.001 Notes: Section 17.5

6.001 Notes: Section 17.5 6.001 Notes: Section 17.5 Slide 17.5.1 Now, let's look at one example in which changing the evaluation model allows us to explore a very different kind of computational problem. Our goal is to show how

More information

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #19. Loops: Continue Statement Example

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #19. Loops: Continue Statement Example Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #19 Loops: Continue Statement Example Let us do a sample program using continue statements, I will introduce

More information

Comp 151. Control structures.

Comp 151. Control structures. Comp 151 Control structures. admin For these slides read chapter 7 Yes out of order. Simple Decisions So far, we ve viewed programs as sequences of instructions that are followed one after the other. While

More information

Signed umbers. Sign/Magnitude otation

Signed umbers. Sign/Magnitude otation Signed umbers So far we have discussed unsigned number representations. In particular, we have looked at the binary number system and shorthand methods in representing binary codes. With m binary digits,

More information

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I BASIC COMPUTATION x public static void main(string [] args) Fundamentals of Computer Science I Outline Using Eclipse Data Types Variables Primitive and Class Data Types Expressions Declaration Assignment

More information

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #33 Pointer Arithmetic

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #33 Pointer Arithmetic Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #33 Pointer Arithmetic In this video let me, so some cool stuff which is pointer arithmetic which helps you to

More information

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng Slide Set 2 for ENCM 335 in Fall 2018 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2018 ENCM 335 Fall 2018 Slide Set 2 slide

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

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

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

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

Unit-II Programming and Problem Solving (BE1/4 CSE-2) Unit-II Programming and Problem Solving (BE1/4 CSE-2) Problem Solving: Algorithm: It is a part of the plan for the computer program. An algorithm is an effective procedure for solving a problem in a finite

More information

Control Structures in Java if-else and switch

Control Structures in Java if-else and switch Control Structures in Java if-else and switch Lecture 4 CGS 3416 Spring 2016 February 2, 2016 Control Flow Control flow refers to the specification of the order in which the individual statements, instructions

More information

FOR Loops. Last Modified: 01 June / 1

FOR Loops. Last Modified: 01 June / 1 FOR Loops http://people.sc.fsu.edu/ jburkardt/isc/week04 lecture 08.pdf... ISC3313: Introduction to Scientific Computing with C++ Summer Semester 2011... John Burkardt Department of Scientific Computing

More information

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream? 2. How does memoization work?

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream? 2. How does memoization work? STREAMS 10 COMPUTER SCIENCE 61AS Basics of Streams 1. What is a stream? 2. How does memoization work? 3. Is a cons-stream a special form? Practice with Streams 1. Define a procedure (ones) that, when run

More information

Computer Programming

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

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

(Refer Slide Time: 00:01:30)

(Refer Slide Time: 00:01:30) Digital Circuits and Systems Prof. S. Srinivasan Department of Electrical Engineering Indian Institute of Technology, Madras Lecture - 32 Design using Programmable Logic Devices (Refer Slide Time: 00:01:30)

More information

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

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

Lecture 1: Overview

Lecture 1: Overview 15-150 Lecture 1: Overview Lecture by Stefan Muller May 21, 2018 Welcome to 15-150! Today s lecture was an overview that showed the highlights of everything you re learning this semester, which also meant

More information

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #44 Multidimensional Array and pointers In this video, we will look at the relation between Multi-dimensional

More information

Continuing with whatever we saw in the previous lectures, we are going to discuss or continue to discuss the hardwired logic design.

Continuing with whatever we saw in the previous lectures, we are going to discuss or continue to discuss the hardwired logic design. Computer Organization Part I Prof. S. Raman Department of Computer Science & Engineering Indian Institute of Technology Lecture 10 Controller Design: Micro programmed and hard wired (contd) Continuing

More information

(Refer Slide Time 3:31)

(Refer Slide Time 3:31) Digital Circuits and Systems Prof. S. Srinivasan Department of Electrical Engineering Indian Institute of Technology Madras Lecture - 5 Logic Simplification In the last lecture we talked about logic functions

More information

CMPSCI 250: Introduction to Computation. Lecture #7: Quantifiers and Languages 6 February 2012

CMPSCI 250: Introduction to Computation. Lecture #7: Quantifiers and Languages 6 February 2012 CMPSCI 250: Introduction to Computation Lecture #7: Quantifiers and Languages 6 February 2012 Quantifiers and Languages Quantifier Definitions Translating Quantifiers Types and the Universe of Discourse

More information

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore Lecture - 20 Intermediate code generation Part-4 Run-time environments

More information

Condition Controlled Loops. Introduction to Programming - Python

Condition Controlled Loops. Introduction to Programming - Python + Condition Controlled Loops Introduction to Programming - Python + Repetition Structures n Programmers commonly find that they need to write code that performs the same task over and over again + Example:

More information

Lecture 7: General Loops (Chapter 7)

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

CS102: Variables and Expressions

CS102: Variables and Expressions CS102: Variables and Expressions The topic of variables is one of the most important in C or any other high-level programming language. We will start with a simple example: int x; printf("the value of

More information

(I m not printing out these notes! Take your own.)

(I m not printing out these notes! Take your own.) PT1420 Week 2: Software Program Design I (I m not printing out these notes! Take your own.) Today we'll be discussing designing programs: Algorithms and errors Flowcharts and pseudocode Sequence structures

More information