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

Size: px
Start display at page:

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

Transcription

1 Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage: More on Arithmetic Expressions The following two are equivalent:! x = x + 5; x += 5; The above expressions add 5 to x and store the increased amount back into x. Similarly, you can do: y = y - 10; or y -= 10; z = z * (x + 2); or z *= x + 2; The following is erroneous. Do you see why? x = 10; y /= x - 10; There is yet another shortcut for incrementing/decrementing a value. The following are equivalent:! x = x + 1;!! x += 1;!! x++;!! ++x; // increment by 1! y = y - 1;!! y -= 1;!! y--;!! --y; // decrement by 1 The incremental operator ++ is the reason behind the naming of language C++, as C++ started as the extention of the language C. (Can you guess the name of the predecessor of C?) (Maybe a little tricky) ++ and -- behave slightly differently based on their location. See the following: x = 5;! y = x++;! cout << y << endl;!!! // It prints: This is an assignment, so the first step is to evaluate the right hand side expression and the next step is to assign the evaluated value to the variable on the left hand side. The expression x++ evaluates to the original value of x. On the contrary, when the variable comes after ++, it evaluates to the resulting value. x = 5;! y = ++x;! cout << y << endl;!!! // It prints: Use this kind of increment-assignment combination only if you fully understand what is going on. It is wise to break it into two statements when it is not obvious what you are doing. The Type bool Remember, boolean expressions are expressions that evaluate to either true or false. Just like integers can be stored in int variables and real numbers can be stored in double variables, true and false values can be stored in bool variables. For instance, the following are equivalent: if (x > 5) cout << "x is greater than 5" << endl; and Copyright Brian Choi All Rights Reserved. Week 3, Page 1/10

2 bool test = x > 5; if (test) cout << "x is greater than 5" << endl; bool s can be handy when you have to evaluate the same condition multiple times. They let you evaluate the expression once and for all, as in: bool test = x > 5;!! // test is true if x is greater than 5 at this moment. if (test && x < 10)! // true if x is greater than 5 and less than 10. // do something if (test x < 0)! // true if x is greater than 5 or less than 0. // do something else Nested If Statements Recall that an if statement takes the following form ( else part omitted): if (condition) // if body This is a single if statement, and both if and else bodies may consist of multiple statements. The following if statement has two statements in the if body. if (x > 5) cin >> x; cout << "x was greater than 5, so we assigned a new value for x: " << x << endl; This means that in an if body (and in an else body), we can have another if statement as well. if (x > 5) cout << "x is greater than 5"; if (x > 10) cout << "... and it is even greater than 10." << endl; else cout << "... but it is less than or equal to 10." << endl; And you can nest your if statements as many times as you want (as long as it doesn t get messy). Copyright Brian Choi All Rights Reserved. Week 3, Page 2/10

3 Practices Problem 1 Get name and test score of a student, and print a letter grade. The score must be between 0 and 100 (inclusive). If the score is out of range, print error. Assume the class is not curved -- give A for , B for 80-89, C for 70-79, D for 60-69, and F for below 60. The score cannot have any decimal points. Assume the user always enters some number (as opposed to characters) for score. For example: Name: Brian Score: 92 Brian receives a/an A. Your code: Name: Mr. Lazy Score: 25 Mr. Lazy receives a/an F. Name: Sean Score: -1 error Copyright Brian Choi All Rights Reserved. Week 3, Page 3/10

4 Problem 2 Get the birthday of a person and today s date, and determine how many days are left before the birthday. If the birthday has passed, say so. For simplicity, assume there are only 30 days in a month, and that the user never provides illogical input. Examples: Birthday (month): 5 Birthday (day): 25 Today (month): 3 Today (day): 2 83 days left Birthday (month): 11 Birthday (day): 4 Today (month): 9 Today (day): days left Birthday (month): 3 Birthday (day): 2 Today (month): 5 Today (day): 24 Your birthday has passed I ve written a portion of the code for you below: int birthmonth, birthday; int currentmonth, currentday; int daysleft; // Get the dates. cout << Birthday (month): ; cin >> birthmonth; cout << Birthday (day): ; cin >> birthday; cout << Today (month): ; cin >> currentmonth; cout << Today (day): ; cin >> currentday; // Your code below. return 0; Copyright Brian Choi All Rights Reserved. Week 3, Page 4/10

5 Loops While Loops Here is the syntax for a while loop: while (condition) body; body is repeatedly run while condition holds. Take a look at the following example: #include <iostream> Output: using namespace std; int x = 0, y = 10; while (x < y) x++; // equivalent to x = x + 1 cout << x << endl; cout << endl; return 0; Be careful when you are using while statements, though. what is wrong with the following code? x = 1; y = 0; while (y == 0) x++; Infinite loops will cause your program to crash so you should always make sure that it ends at some point! There is a variation of while statement called do-while. It works just like while, but will execute the loop once no matter what, and then test the condition. This is most useful in a case like the following, where you ask for a valid input from the user: int x = 0; do cout << "Input 5 or 6: "; cin >> x; while (x!= 5 && x!= 6); // Don t forget that semicolon For Loops Often, in your looping structure you will need some kind of counter to keep track of the number of times the loop is executed. (e.g., printing Hello 100 times, adding numbers 1 to 50) While this can be done with a while loop, for your convenience, C++ defines another looping structure that supports it: for ( initialization ; condition ; update ) body which we call for loops. An example should help this make better sense: Copyright Brian Choi All Rights Reserved. Week 3, Page 5/10

6 #include <iostream> using namespace std; int i, sum = 0; for (i = 1; i <= 100; i++) sum = sum + i; cout << "sum = " << sum << endl; return 0; What does the above program do? The evaluation rule is as follows. Recall that the syntax for a for loop is: for ( initialization ; condition ; update ) body 1. Execute initialization 2. Check the condition If true, 3. run the body 4. do the update go back to 2 If false, exit the for loop. The order of execution is therefore For to While conversion If you can do something using a while loop, you should be able to write a for loop equivalent, and vice versa. The for-to-while conversion rule is quite straightforward, and is illustrated on the right side. initialization should come before everything, and it must be executed only once. Therefore, it comes before the whole while loop. Then the condition must be checked, thus condition goes inside the while statement. If this is true, we execute the body, and then update. Note that update statement must be inside the while loop. (What happens if it s outside the loop?) This conversion is somewhat straightforward from the evaluation steps of for loops we saw above. For to do-while conversion works in a similar way. Homework 2 should have bugged you enough for this. for (initialization; condition; update) body initialization while (condition) body update Copyright Brian Choi All Rights Reserved. Week 3, Page 6/10

7 Question: Can you tell how the following for statements will behave? What values does i take on? for (int i = 10; i >= 0; i--) for (int i = 0; i < 100; i += 2) for (int i = 32; i > 1; i /= 2) Question: Write a for loop that finds the sum of numbers 1 to 100. Question: (Nested Loops) How many times does # get printed? for (int i = 0; i < 10; i++) for (int j = i; j < 10; j++) cout << # ; cout << # ; Question: Convert the following for loop into a while loop. for (int i = 0; i < 50; i += 2) cout << Hello << endl; Copyright Brian Choi All Rights Reserved. Week 3, Page 7/10

8 Functions Suppose I want to write a program that computes 3 things: - the sum of integers 1 to the sum of integers 51 to the sum of integers 103 to 276 You can create a loop for each one of the sums, but this is cumbersome. If we are allowed to define a method of summing up numbers just once, and then use it three times, it will reduce some burden. In other words, you can teach your computer how to sum, as opposed to what to sum. In C++, functions (or equivalently known as methods, subroutines, or procedures in other contexts) are meant to serve this purpose. An example of a C++ function is shown here ----> Hey, that resembles the main() thing that we used to write. In fact, main() is also a function, which we call main function, and it happens to be the one that gets executed when the program begins. Before we take it apart, let me bring up an analogy: int sum(int begin, int end) int temp = 0; for (int i = begin; i <= end; i++) temp += i; return temp; This is called a function for a reason. Where else do you see functions? In high school algebra, we ve seen functions presented in the following way: f(x) = 2x + 5 This function, called f, takes a value, denoted x, and maps this number in a predetermined way to another number. If the input x is 5, for instance, we do: f(5) = 2(5) + 5 = 15 and it would result in a different number for a different output. Once we have defined this function f, we can simplify other expressions using f. 4x + 10 = 2f(x) 2x 2 + 7x + 5 = (x+1)(2x+5) = (x+1)f(x) This is exactly what we do with our C++ functions. We define it, and then use it to simplify our program. Now let s take the function definition apart. - int indicates that the output is of type integer. We call this return type. - sum is the name, or the identifier, of the function, just like f is for the function above. - begin and end are arguments to the function sum, just like x is for the function f. The arguments must be within parentheses. - The function body within defines the mapping rule. - The function must return the value explicitly, depending on the input it receives. Copyright Brian Choi All Rights Reserved. Week 3, Page 8/10

9 In general, the syntax goes as follows: return_type function_name(arg1, arg2, arg3,...) function_body and the function body must include return statements such that every execution of the function ends up in a return statement. A function can also have no arguments, in which case the argument list simply becomes empty (i.e., ()). So where in the program should this function go and how do we use it? Here is an example. #include <iostream> using namespace std; int sum(int begin, int end) int temp = 0; for (int i = begin; i <= end; i++) temp += i; return temp; cout << sum(1, 20) << endl; // prints the sum cout << sum(20, 50) << endl; // prints the sum int sum1, sum2; sum1 = sum(4, 8); // stores the sum into sum1 sum2 = sum(1, 10) + sum(15, 20); // stores the sum into sum2 cout << sum1 << << sum2 << endl; return 0; Note that the function must be defined before it can be used. If the definition of sum comes after the main function, the compiler will complain that sum is an undefined identifier. If for a style reason you want to keep the main function on the top and define other functions later, you can let the computer know that there s going to be a function with a certain name first, and define later. For instance, you can do what s on the right. The bolded line is called a function prototype. #include <iostream> // I will define "sum" later. int sum(int begin, int end);... // Define it here. int sum(int begin, int end)... Copyright Brian Choi All Rights Reserved. Week 3, Page 9/10

10 Now it s time for a little practice: Question: Define a function f that takes in a real number x and returns the result of 2x + 5. Question: Use the function f and sum to find the sum from f(3) to f(72), and store it in a variable called z. Question: How many # s will you see on the screen? int mystery(int len) for (int i = 0; i < len; i++) cout << # ; if (len % 2 == 0) return len + 1; else return len + 3; int y = 5; while (y < 10) y = mystery(y); return 0; Copyright Brian Choi All Rights Reserved. Week 3, Page 10/10

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

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

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

### Discussion 1H Notes (Week 4, April 22) TA: Brian Choi Section Webpage:

Discussion 1H Notes (Week 4, April 22) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs31 Passing Arguments By Value and By Reference So far, we have been passing in

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

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

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

### 1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

Exercises with solutions. 1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl? #include b) What using statement do you always put at the top of

### CSCE 206: Structured Programming in C++

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

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

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

### Discussion 1H Notes (Week 2, 4/8) TA: Brian Choi Section Webpage:

Discussion 1H Notes (Week 2, 4/8) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs31 Variables You have to instruct your computer every little thing it needs to do even

### CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

CS31 Discussion 1E Jie(Jay) Wang Week3 Oct.12 Outline Problems from Project 1 Review of lecture String, char, stream If-else statements Switch statements loops Programming challenge Problems from Project

### Computer Programming : C++

The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2003 Muath i.alnabris Computer Programming : C++ Experiment #1 Basics Contents Structure of a program

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

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

Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

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

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

### Computer Science II Lecture 1 Introduction and Background

Computer Science II Lecture 1 Introduction and Background Discussion of Syllabus Instructor, TAs, office hours Course web site, http://www.cs.rpi.edu/courses/fall04/cs2, will be up soon Course emphasis,

### ENJOY! Problem 2 What does the following code do, in a brief English sentence? int mystery(int k) { int i = 0; if (k < 0) return -1;

Midterm Practice Problems TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs31 ENJOY! Problem 1 True False Variable names can begin with an alphabet or a digit. True False

### 6 Functions. 6.1 Focus on Software Engineering: Modular Programming TOPICS. CONCEPT: A program may be broken up into manageable functions.

6 Functions TOPICS 6.1 Focus on Software Engineering: Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5 Passing Data by Value 6.6 Focus

### C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

C++ PROGRAMMING For Industrial And Electrical Engineering Instructor: Ruba A. Salamh CHAPTER TWO: Fundamental Data Types Chapter Goals In this chapter, you will learn how to work with numbers and text,

### Consider the above code. This code compiles and runs, but has an error. Can you tell what the error is?

Discussion 1H Notes (Week 8, May 20) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs31 Dynamic Allocation of Memory Recall that when you create an array, you must know

### Programming Language. Functions. Eng. Anis Nazer First Semester

Programming Language Functions Eng. Anis Nazer First Semester 2016-2017 Definitions Function : a set of statements that are written once, and can be executed upon request Functions are separate entities

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

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

### PIC 10A Flow control. Ernest Ryu UCLA Mathematics

PIC 10A Flow control Ernest Ryu UCLA Mathematics If statement An if statement conditionally executes a block of code. # include < iostream > using namespace std ; int main () { double d1; cin >> d1; if

### The following expression causes a divide by zero error:

Chapter 2 - Test Questions These test questions are true-false, fill in the blank, multiple choice, and free form questions that may require code. The multiple choice questions may have more than one correct

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

### Chapter 4 - Notes Control Structures I (Selection)

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

### (Refer Slide Time 01:41 min)

Programming and Data Structure Dr. P.P.Chakraborty Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture # 03 C Programming - II We shall continue our study of

### 5. Control Statements

5. Control Statements This section of the course will introduce you to the major control statements in C++. These control statements are used to specify the branching in an algorithm/recipe. Control statements

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

### By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

1 By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program variables. Apply C++ syntax rules to declare variables, initialize

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

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

### COMP322 - Introduction to C++ Lecture 01 - Introduction

COMP322 - Introduction to C++ Lecture 01 - Introduction Robert D. Vincent School of Computer Science 6 January 2010 What this course is Crash course in C++ Only 14 lectures Single-credit course What this

### THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

THE INTEGER DATA TYPES STORAGE OF INTEGER TYPES IN MEMORY All data types are stored in binary in memory. The type that you give a value indicates to the machine what encoding to use to store the data in

### Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Welcome Back CSCI 262 Data Structures 2 - Review What you learned in CSCI 261 (or equivalent): Variables Types Arrays Expressions Conditionals Branches & Loops Functions Recursion Classes & Objects Streams

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

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

### Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Welcome Back CSCI 262 Data Structures 2 - Review What you learned in CSCI 261 (or equivalent): Variables Types Arrays Expressions Conditionals Branches & Loops Functions Recursion Classes & Objects Streams

### Getting started with C++ (Part 2)

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

### C++ Programming Lecture 1 Software Engineering Group

C++ Programming Lecture 1 Software Engineering Group Philipp D. Schubert Contents 1. More on data types 2. Expressions 3. Const & Constexpr 4. Statements 5. Control flow 6. Recap More on datatypes: build-in

### Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Overview - General Data Types - Categories of Words - The Three S s - Define Before Use - End of Statement - My First Program a description of data, defining a set of valid values and operations List of

### 6.1. Chapter 6: What Is A Function? Why Functions? Introduction to Functions

Chapter 6: 6.1 Functions Introduction to Functions What Is A Function? Why Functions? We ve been using functions ( e.g. main() ). C++ program consists of one or more functions Function: a collection of

### Chapter 4 Computation

Chapter 4 Computation Hartmut Kaiser hkaiser@cct.lsu.edu http://www.cct.lsu.edu/~hkaiser/spring_2011/csc1253.html Slides adapted from: Bjarne Stroustrup, Programming Principles and Practice using C++ Abstract

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

### Maciej Sobieraj. Lecture 1

Maciej Sobieraj Lecture 1 Outline 1. Introduction to computer programming 2. Advanced flow control and data aggregates Your first program First we need to define our expectations for the program. They

### Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

Plan for the rest of the semester: Programming We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems. We saw earlier that computers

### CS31 Discussion 1E. Jie(Jay) Wang Week1 Sept. 30

CS31 Discussion 1E Jie(Jay) Wang Week1 Sept. 30 About me Jie Wang E-mail: holawj@gmail.com Office hour: Wednesday 3:30 5:30 BH2432 Thursday 12:30 1:30 BH2432 Slides of discussion will be uploaded to the

### Chapter 4. Computation. Bjarne Stroustrup.

Chapter 4 Computation Bjarne Stroustrup www.stroustrup.com/programming Abstract Today, I ll present the basics of computation. In particular, we ll discuss expressions, how to iterate over a series of

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

Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

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

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

### Exam 1. CSI 201: Computer Science 1 Fall 2018 Professors: Shaun Ramsey

Exam 1 CSI 201: Computer Science 1 Fall 2018 Professors: Shaun Ramsey I understand that this exam is closed books and closed notes and is to be completed without a calculator, phone, or other computer.

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

### Introduction to C ++

Introduction to C ++ Thomas Branch tcb06@ic.ac.uk Imperial College Software Society October 18, 2012 1 / 48 Buy Software Soc. s Free Membership at https://www.imperialcollegeunion.org/shop/ club-society-project-products/software-products/436/

### In this chapter you will learn:

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

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

Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

### C++ Support Classes (Data and Variables)

C++ Support Classes (Data and Variables) School of Mathematics 2018 Today s lecture Topics: Computers and Programs; Syntax and Structure of a Program; Data and Variables; Aims: Understand the idea of programming

### CS2141 Software Development using C/C++ C++ Basics

CS2141 Software Development using C/C++ C++ Basics Integers Basic Types Can be short, long, or just plain int C++ does not define the size of them other than short

### C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

C++ Basics Data Processing Course, I. Hrivnacova, IPN Orsay The First Program Comments Function main() Input and Output Namespaces Variables Fundamental Types Operators Control constructs 1 C++ Programming

### Looping. Arizona State University 1

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

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

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

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

### Chapter 2: Basic Elements of C++

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

### PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation Ernest Ryu UCLA Mathematics Pointers A variable is stored somewhere in memory. The address-of operator & returns the memory address of the variable.

### Lab 10: Alternate Controls

_ Unit 2: Programming in C++, pages 1 of 8 Department of Computer and Mathematical Sciences CS 1410 Intro to Computer Science with C++ 9 Objectives: Lab 10: Alternate Controls The objective of this lab

### Lab Instructor : Jean Lai

Lab Instructor : Jean Lai Group related statements to perform a specific task. Structure the program (No duplicate codes!) Must be declared before used. Can be invoked (called) as any number of times.

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

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

Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers

### Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single

Functions in C++ Problem-Solving Procedure With Modular Design: Program development steps: Analyze the problem Develop a solution Code the solution Test/Debug the program C ++ Function Definition: A module

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

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

### Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

Discussion 1E Jie(Jay) Wang Week 10 Dec.2 Outline Dynamic memory allocation Class Final Review Dynamic Allocation of Memory Recall int len = 100; double arr[len]; // error! What if I need to compute the

Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style 3 2.1 Variables and Assignments Variables and

### These are notes for the third lecture; if statements and loops.

These are notes for the third lecture; if statements and loops. 1 Yeah, this is going to be the second slide in a lot of lectures. 2 - Dominant language for desktop application development - Most modern

### Week 4 EECS 183 MAXIM ALEKSA. maximal.io

Week 4 EECS 183 MAXIM ALEKSA maximal.io Agenda Functions Scope Conditions Boolean Expressions Lab 2 Project 2 Q&A Lectures 15% 36% 19% 8:30am 10:00am with Bill Arthur 10:00am 11:30am with Mary Lou Dorf

### 377 Student Guide to C++

377 Student Guide to C++ c Mark Corner January 21, 2004 1 Introduction In this course you will be using the C++ language to complete several programming assignments. Up to this point we have only provided

### CSCI 1061U Programming Workshop 2. C++ Basics

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

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

Review: Exam 1 9/20/06 CS150 Introduction to Computer Science 1 1 Your First C++ Program 1 //*********************************************************** 2 // File name: hello.cpp 3 // Author: Shereen Khoja

### LOOPS. Repetition using the while statement

1 LOOPS Loops are an extremely useful feature in any programming language. They allow you to direct the computer to execute certain statements more than once. In Python, there are two kinds of loops: while

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

Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

### Chapter 3. More Flow of Control

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

### CS 220: Introduction to Parallel Computing. Arrays. Lecture 4

CS 220: Introduction to Parallel Computing Arrays Lecture 4 Note: Windows I updated the VM image on the website It now includes: Sublime text Gitkraken (a nice git GUI) And the git command line tools 1/30/18

### Chapter 2 Basic Elements of C++

C++ Programming: From Problem Analysis to Program Design, Fifth Edition 2-1 Chapter 2 Basic Elements of C++ At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion

### PIC 10A. Review for Midterm I

PIC 10A Review for Midterm I Midterm I Friday, May 1, 2.00-2.50pm. Try to show up 5 min early so we can start on time. Exam will cover all material up to and including todays lecture. (Only topics that

### 1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

Topic 2 1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary Arithmetic Operators C++ has the same arithmetic operators as a calculator:

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

Chapter 2 C++ Basics 1 Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style Slide 2-3 2.1 Variables and Assignments 2

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

### Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

Introduction to C++ 1. General C++ is an Object oriented extension of C which was derived from B (BCPL) Developed by Bjarne Stroustrup (AT&T Bell Labs) in early 1980 s 2. A Simple C++ Program A C++ program

### Arrays in C++ Instructor: Andy Abreu

Arrays in C++ Instructor: Andy Abreu Reason behind the idea When we are programming, often we have to process a large amount of information. We can do so by creating a lot of variables to keep track of

### Objectives. In this chapter, you will:

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

### Chapter 2. C++ Basics

Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style Slide 2-2 2.1 Variables and Assignments Variables

### Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Announcements Lab Friday, 1-2:30 and 3-4:30 in 26-152 Boot your laptop and start Forte, if you brought your laptop Create an empty file called Lecture4 and create an empty main() method in a class: 1.00

### To become familiar with array manipulation, searching, and sorting.

ELECTRICAL AND COMPUTER ENGINEERING 06-88-211: COMPUTER AIDED ANALYSIS LABORATORY EXPERIMENT #2: INTRODUCTION TO ARRAYS SID: OBJECTIVE: SECTIONS: Total Mark (out of 20): To become familiar with array manipulation,