C++ Reference NYU Digital Electronics Lab Fall 2016
|
|
- Carmel Campbell
- 5 years ago
- Views:
Transcription
1 C++ Reference NYU Digital Electronics Lab Fall 2016 Updated on August 24, 2016 This document outlines important information about the C++ programming language as it relates to NYU s Digital Electronics lab. In this class, you ll be using the Teensy 3.2 microcontroller, an Arduino-compatible device that can be programmed using C++. Your instructor will cover different programming concepts throughout the semester as they become relevant, however this supplemental guide is a great way to refresh yourself on old material or to get a leg up on newer material if you choose to do so. C++ is one of the most commonly used programming languages in the world, and it is very popular for audio processing because of its flexibility and fast performance. This document does not cover everything you could ever know about C++, but it does provide a solid foundation upon which you can build and use to create some really cool programs! If you have further questions or encounter problems with this document (including typos or inaccuracies), please contact your instructor.
2 CONTENTS 2 Contents 1 Programming in Arduino 3 2 Variables 5 3 Basic Math Addition & Subtraction Multiplication & Division Exponents Parentheses Example Conditional Statements Condition true or false if Statements if-else Statements if-else if Statements AND & OR Operators Loops for Loops while Loops Functions Creating a Function - Example Using a Function - Example Further Topics 26
3 1 PROGRAMMING IN ARDUINO 3 1 Programming in Arduino Throughout this class, you ll be writing your code in Arduino s interactive development environment (IDE). Simply put, an IDE is a piece of software that allows you to both write code in a text editor and to run or deploy the code to the hardware that will run it for us (in this case, the Teensy). When you first create a new program in Arduino (called a sketch), you will see two functions: setup and loop. As the image indicates, setup contains a block of code that will run just once. This happens at the very beginning of your program. After the code within setup finishes, the code within loop will then execute. Unlike setup, the code within
4 1 PROGRAMMING IN ARDUINO 4 loop will run indefinitely. The code will execute line-by-line until it reaches the end, and then repeat from the beginning again. This will continue happening until you manually stop the program. The setup and loop functions are specific to Arduino. That is, if you try to use the above C++ code for any purpose other than for Arduino or Teensy, your code will not compile. In other words, your computer will basically say, What is this thing called setup and this other thing called loop? I have never heard of these, and I refuse to run your program until you tell me what these words mean!" You know, if computers could talk...
5 2 VARIABLES 5 2 Variables Think of a variable as a container that holds a specific value. Remember this from math class? y = x + 2 In this case, x and y are both variables. x is a container into which you can store any numeric value. The variable y then takes on a value that is two greater than x. The same concept applies to programming. 1 i n t num = 4 ; 2 double anothernum = ; 3 char myletter = ' b ' ; 4 S t r i n g myword = " Hello, World! " ; 5 bool sampleboolval = true ; There are several things to note from the above: A variable can have one of many data types. In the above code, we have: int: a numeric integer value double: a numeric decimal value char: a single character for display or naming purposes. Must be enclosed by single quotes String: a collection or group of characters. Must be enclosed by double quotes bool: either true or false. Shorthand for boolean.
6 2 VARIABLES 6 You must specify the data type of a variable when you create it You can name a variable whatever you d like with a few exceptions: The name must begin with an alphabetic character (no numbers or other symbols) You may not use words that are reserved by C++ or by Arduino (e.g. you cannot name a variable loop since Arduino already has a function by that name) You can also change a variable s value throughout your code. For example, the following code is completely valid: 1 // I n i t i a l i z e a v a r i a b l e with a value o f 0 2 i n t currentvalue = 0 ; 3 // I n i t i a l i z e another v a r i a b l e with a value o f 3 4 i n t addthisval = 3 ; 5 / Add the two t o g e t h e r and s t o r e the r e s u l t 6 back i n t o the f i r s t v a r i a b l e / 7 currentvalue = currentvalue + addthisval ; Here are several more takeaways: Once you create a variable (line 2), you do not need to specify its type again when referencing it later (line 7) You can add comments to your code (lines 1, 3, 5, 6). These lines are not executed by the program and exist solely to help the reader better understand what the code is doing. Consider this how a programmer takes notes directly
7 2 VARIABLES 7 in her code. Comment a single line using // or multiple lines by enclosing them by /* */ When assigning a value to a variable, the expression(s) to the right of the equals sign gets evaluated first. The value of the right side is then stored into the variable on the left side. In line 7, the variable currentvalue is updated by adding its own value to that of addthisval. Thus, the old value of 0 gets replaced by a new value of 3.
8 3 BASIC MATH 8 3 Basic Math C++ can perform many arithmetic and complex mathematical operations and follows the standard order of operations, a.k.a. PEMDAS: 1. Parentheses 2. Exponents 3. Multiplication & Division 4. Addition & Subtraction 3.1 Addition & Subtraction C++ performs these with the standard + and - operator, e.g. 1 i n t mynum1 = 5 ; 2 i n t mynum2 = 3 ; 3 i n t r e s u l t = mynum1 + mynum2; // the r e s u l t i s 8 4 i n t r e s u l t 2 = mynum1 mynum2; // the r e s u l t i s Multiplication & Division C++ uses an asterisk * for multiplication and a forward slash / for division, e.g. 1 double mynum1 = 1 0 ; 2 double mynum2 = 4 ; 3 double r e s u l t = mynum1 mynum2; // the r e s u l t i s double r e s u l t 2 = mynum1 / mynum2; // the r e s u l t i s 2. 5
9 3 BASIC MATH Exponents We use the pow()* function in C++ to perform exponential operations, e.g. 1 double mynum1 = 2 ; 2 double mynum2 = 3 ; 3 double r e s u l t = pow(mynum1, mynum2) ; //2^3 = 8 4 double r e s u l t 2 = pow(mynum2, mynum1) ; // 3^2 = 9 *If you re programming in C++ outside of Arduino, you may have to perform an additional step in order to make the pow() function available for use. However, this extra step will not be required in this class. See the Functions section of this document for more information on how functions work in general. 3.4 Parentheses Not surprisingly, parentheses in C++ serve the purpose you d expect, i.e. to group numbers & and expressions together, e.g.: 1 double mynum1 = 6 ; 2 double mynum2 = 2 ; 3 double r e s u l t = mynum + mynum2 7 ; // = 20 4 double r e s u l t = (mynum + mynum2) 7 ; // (6+2) 7 = Example Here s an example that uses all of the above operations. 1 double r e s u l t = 2 ( ) / pow (9, 2 ) ; 2 //The value o f r e s u l t as above i s roughly 1.849
10 4 CONDITIONAL STATEMENTS 10 4 Conditional Statements 4.1 Condition true or false A conditional expression in programming basically asks (and answers) the question, "Is this thing true?" For example: 1 bool myresult = (9 < 1 4 ) ; Here, we are asking the question, "Is it true that 9 is less than 14?" Since the answer is yes, the boolean variable myresult will have the value of true. 1 bool anotherresult = (3 >= 1 7 ) ; Here, we are asking the question, "Is it true that 3 is greater than or equal to 17?" Since the answer is no, the variable anotherresult will have the value of false. The < and >= used above are relational operators, meaning that they define relationships between two values. Here are the such operators we ll be using in this class: > Greater than >= Greater than or equal to < Less than <= Less than or equal to == Equal to*
11 4 CONDITIONAL STATEMENTS 11!= Not equal to *Note that == is used to compare two values, whereas a single = is used to assign a value. For example, myvar = 5 gives a value of 5 to the variable myvar, whereas myvar == 5 asks the question "does myvar contain the value 5?" 4.2 if Statements In many situations, you ll want to take a specific action only if a certain condition is true. For example, imagine you want to display a message if someone presses a button. Here, the word if is followed by a conditional expression within parentheses. If this condition evaluates to true, then the code within the curly braces will execute. In this particular case, we are asking the question, "Is the buttonispressed variable equal to true?" or in simpler terms, "Has the button been pressed?" If the answer to this question is yes (i.e. true), then the program will display the message on line 18. If the answer to that question is no (i.e. false), then lines do not execute, meaning that we ll just skip ahead to line 20 and resume the rest of the program from there.
12 4 CONDITIONAL STATEMENTS if-else Statements Now, suppose that we still want to display a message if the button is pressed, however we also want to display a different message if the button is not pressed. Here, we use an if-else statement. The above example shows us that we can execute one of two different pieces of code depending on what has happened in the program so far. Here, we use the same if to specify a condition and a block of code that executes if that condition is true. In addition, we append an else with another block of code to the end of the previous block of code. The else is basically telling the program to do something if that previous condition is not true. In other words, we will always execute ONE of those two blocks of code. Not both. Not neither. Table 1 below shows a more detailed breakdown of what this is doing.
13 4 CONDITIONAL STATEMENTS 13 C++ Jargon if the condition in parentheses is true...then, execute the code within the curly braces else...execute the code within the next set of curly braces Our Example (Line 16) If someone pressed the button (Lines 17-19)...then, display a message that the button has been pressed (Line 20) Otherwise (i.e. if the button has not been pressed) (Lines 21-23)...Display a waiting message Table 1: What the above if-else statement is doing 4.4 if-else if Statements In an if-else statement, we always execute one of two mutually exclusive blocks of code. This makes the assumption that we have two different states, i.e. there are only 2 possibilities for what can happen. However, what if there are more than two possible states? For instance, instead of a button (states: pushed and not pushed), what if we had a knob that controls volume, and we want to display a message depending on whether the volume is too soft (0 40), too loud (80 100) or just right (41 79)? In this case, we have three different states and could therefore use an if-else statement with multiple conditions. For example:
14 4 CONDITIONAL STATEMENTS 14 In non-coding terms, the program is doing this: You can add as many else ifs to your if statements as you d like. This allows you to work with as many different scenarios as you need. For example:
15 4 CONDITIONAL STATEMENTS AND & OR Operators Section 4.4 basically showed us how C++ allows us to do things like this: If the music is soft Complain - "I can t hear!" Otherwise, if the music is at a comfortable volume Relax and listen Otherwise, if the music is too loud Yell at those damn kids to turn down their jukebox
16 4 CONDITIONAL STATEMENTS 16 However, sometimes we want to be able to do something only if multiple, simultaneous conditions are true. Consider the following example: If the music is soft Complain - "I can t hear!" Otherwise, if the music is loud AND the station is set to $ummer Hitz Start partying! Otherwise Turn the radio off The keyword in this case is AND, meaning that we would only start partying if both conditions (the music is loud as well as the radio being set to a specific station) are true. In C++, we use double ampersands && to indicate an AND operator. Let s make this more applicable to us music tech people. Here s a simple example of how this concept might be applied to using a limiter plugin on an audio track in our favorite DAW. 1 i f ( ( pluginisenabled == true ) && ( trackvolume >= 1. 0 ) ) 2 { 3 trackvolume = 1. 0 ; 4 } Here, we see whether our plugin is enabled and whether the audio track is at a volume we deem to be too loud. In the case that both and ONLY both of those things are
17 4 CONDITIONAL STATEMENTS 17 true, we set the volume to a maximum value of 1.0. Again, this is an oversimplified example that nevertheless illustrates the concept. Let s consider a different case. Suppose that we want to make sure that a clarinet player is playing notes within a certain frequency range. Frequencies between 220 Hertz and 440 Hertz are okay with us. Anything else is unacceptable, and we want to yell at the musician if we hear anything outside of this range. This situation lends itself well to using an OR operator, which C++ performs with double pipes. 1 i f ( ( c l a r i n e t F r e q u e n c y < 220) ( c l a r i n e t F r e q u e n c y > 440) ) 2 { 3 S e r i a l. p r i n t l n ( "You ' re not playing the r i g h t notes! " ) ; 4 } Here, either condition can be true in order for the angry computer to yell at the clarinet player. If a note falls below 220 Hertz, you get an angry message. If a note goes above 440 Hertz, you get the same angry message.
18 5 LOOPS 18 5 Loops It s common to find yourself wanting your program to do more or less the same thing a bunch of times. Consider the simple case of printing the numbers 1 through 10. Sure, we technically could do something like this: However, this is horribly inefficient. We could instead do this with one of two types of loops that are very common in C++ as well as in many other languages. These types are the for loop and the while loop. 5.1 for Loops Take the example of printing the numbers 1 through 10. Rather than printing each line individually, we can use a for loop to do this more efficiently. The construction of this type of of loop is shown below.
19 5 LOOPS Use the word for to indicate that this is a for loop. 2. Within parentheses, we initialize a variable to a certain value. Here, we create an integer variable i and set it equal to 1. We end this section with a semicolon. 3. This is the condition under which the content of the loop will execute. Here, as long as i is less than or equal to 10, we will do all the "stuff" within the curly braces below. The condition is followed by another semicolon. 4. Everything within curly braces is the "stuff" that will happen repeatedly. 5. In this case, the stuff to happen is that the current value of i will be displayed to the user. 6. After the stuff in step 5 happens, this statement is executed. Here, after the value of i is printed to the user, the value of i will increase by 1 (i++ is a shorthand way to say i = i + 1). 7. Repeat steps 3 6 until the condition in step 3 is no longer true.
20 5 LOOPS 20 Still confused? Let s take this example through a few iterations of the loop. 1. We create a variable called i and set it equal to Check the condition. What s the value of i? 1. Is that less than or equal to 10? Yes? Okay then - let s do the stuff in the curly braces. 3. The value of i is 1. Display 1 to the user. 4. Increment i by 1. i is now Check the condition. What s the value of i? 2. Is that less than or equal to 10? Yes? Okay then - let s do the stuff in the curly braces. 6. The value of i is 2. Display 2 to the user. 7. Increment i by 1. i is now Check the condition. What s the value of i? 3. Is that less than or equal to 10? Yes? Okay then - let s do the stuff in the curly braces. 9. The value of i is 3. Display 3 to the user. 10. Increment i by 1. i is now Skip ahead a bit for brevity i is now Check the condition. What s the value of i? 9. Is that less than or equal to 10? Yes? Okay then - let s do the stuff in the curly braces. 14. The value of i is 9. Display 9 to the user.
21 5 LOOPS Increment i by 1. i is now Check the condition. What s the value of i? 10. Is that less than or equal to 10? Yes? Okay then - let s do the stuff in the curly braces. 17. The value of i is 10. Display 10 to the user. 18. Increment i by 1. i is now Check the condition. What s the value of i? 11. Is that less than or equal to 10? NO! STOP THE PRESSES! THE CONDITION IS FALSE! WE ARE EXITING THE LOOP! This may all seem daunting at first, but with a little bit of practice and repetition, the structure of a for loop will start to make sense. 5.2 while Loops Compared to its aforementioned cousin, the while loop is probably simpler to understand yet requires more setup in order to execute. Here s one way to use a while loop to do our same 1 10 printing example.
22 5 LOOPS Remember how the for loop s structure includes creating a variable? Well, when using a while loop, we have to do that part beforehand. Here, we create an integer variable i and set it equal to 1, just like in the previous example. 2. Here, we specify the word while followed by a condition. In this case, we are checking whether the value of i is less than or equal to Again, curly braces surround the "stuff" we do within the loop. 4. Just like in the for loop, we print the current value of i to the user. 5. After we show i to the user, we then increment i by Keep doing everything within the curly braces (3) until the condition specified in (2) is no longer true. The for loop and while loop examples are two different ways of doing the exact same thing. In fact, many things that can be done using a for loop can also be done using a while loop. In many cases, the type of loop to use is based purely on the programmer s preference.
23 6 FUNCTIONS 23 6 Functions Functions are self-contained modules of code that exist for very specific purposes. For example, C++ comes with a function called abs() that takes the absolute value of a number. This is useful because it saves us the trouble of having to write out very specific instructions to manually calculate the absolute value every time we want to do so. For example, if we need to do 100 absolute value calculations, having a function lets us go from this: "Hey computer: take this number and give me the same number if it s positive, but give me that number multiplied by negative one if it s negative. Also, do this 100 times." to this: "Hey computer: use the abs() function on this number. Do this 100 times." Clearly, the second option is easier, and this becomes even more useful when we have functions that do tasks much more complex than computing an absolute value. 6.1 Creating a Function - Example Here s a simple, working example of a function in C++:
24 6 FUNCTIONS The function s name is addtwonumbers(). The parentheses after the name indicate that this is a function. 2. A function can have as many or as few parameters as you d like. Each parameter s data type must be specified. This particular function has two parameters, both of which are integers. 3. A function s contents, i.e. everything that the function actually does, must be enclosed by curly braces. 4. The return type indicates what type of value the function will give back to the user. A type of void indicates no return value. This particular function returns a single integer to the user. 5. We use the C++ return keyword to tell the function to give the value stored in the variable result back to the user. In this case, the variable result stores the sum of the two arguments entered by the user.
25 6 FUNCTIONS Using a Function - Example In the above example, we create two variables, each of them storing a different integer value. On line 14, we create a variable called result and set its value equal to the result of running our function on our other two numbers. The variables myfirstnumber and mysecondnumber are passed to the function, the function adds the two variables values together (because we told it to when we defined it), and the return value is stored in the variable result. By the time line 14 is finished running, the value of result will be 8. By the time line 15 is finished running, the value of result will be...?
26 7 FURTHER TOPICS 26 7 Further Topics As mentioned earlier, this document does not discuss every single aspect of C++ programming. Other topics covered throughout the class may include things like: Arrays Variable and Function Scope Libraries...and much more! If you re interested in pursuing more with C++, there are many available, free resources at your disposal. In particular, your NYU tuition pays for a membership to Lynda.com, which contains video lessons on C++ and plenty of other topics. Your instructors are always here to help. Reach out to them with any questions you have about this material.
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 informationJava Programming Fundamentals - Day Instructor: Jason Yoon Website:
Java Programming Fundamentals - Day 1 07.09.2016 Instructor: Jason Yoon Website: http://mryoon.weebly.com Quick Advice Before We Get Started Java is not the same as javascript! Don t get them confused
More informationIntro. 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 informationCoding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.
Coding Workshop Learning to Program with an Arduino Lecture Notes Table of Contents Programming ntroduction Values Assignment Arithmetic Control Tests f Blocks For Blocks Functions Arduino Main Functions
More informationWhat is Iteration? CMPT-101. Recursion. Understanding Recursion. The Function Header and Documentation. Recursively Adding Numbers
What is Iteration? CMPT-101 Week 6 Iteration, Iteration, Iteration, Iteration, Iteration, Iteration,... To iterate means to do the same thing again and again and again and again... There are two primary
More informationFundamentals of Programming Session 4
Fundamentals of Programming Session 4 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2011 These slides are created using Deitel s slides, ( 1992-2010 by Pearson Education, Inc).
More informationT H E I N T E R A C T I V E S H E L L
3 T H E I N T E R A C T I V E S H E L L The Analytical Engine has no pretensions whatever to originate anything. It can do whatever we know how to order it to perform. Ada Lovelace, October 1842 Before
More informationChapter 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 informationIntro. Speed V Growth
Intro Good code is two things. It's elegant, and it's fast. In other words, we got a need for speed. We want to find out what's fast, what's slow, and what we can optimize. First, we'll take a tour of
More informationIntro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming
Intro to Programming Unit 7 Intro to Programming 1 What is Programming? 1. Programming Languages 2. Markup vs. Programming 1. Introduction 2. Print Statement 3. Strings 4. Types and Values 5. Math Externals
More information4. Java Project Design, Input Methods
4-1 4. Java Project Design, Input Methods Review and Preview You should now be fairly comfortable with creating, compiling and running simple Java projects. In this class, we continue learning new Java
More informationSAMLab Tip Sheet #1 Translating Mathematical Formulas Into Excel s Language
Translating Mathematical Formulas Into Excel s Language Introduction Microsoft Excel is a very powerful calculator; you can use it to compute a wide variety of mathematical expressions. Before exploring
More informationDivisibility Rules and Their Explanations
Divisibility Rules and Their Explanations Increase Your Number Sense These divisibility rules apply to determining the divisibility of a positive integer (1, 2, 3, ) by another positive integer or 0 (although
More informationChapter 1 Operations With Numbers
Chapter 1 Operations With Numbers Part I Negative Numbers You may already know what negative numbers are, but even if you don t, then you have probably seen them several times over the past few days. If
More informationMemory Addressing, Binary, and Hexadecimal Review
C++ By A EXAMPLE Memory Addressing, Binary, and Hexadecimal Review You do not have to understand the concepts in this appendix to become well-versed in C++. You can master C++, however, only if you spend
More informationSkill 1: Multiplying Polynomials
CS103 Spring 2018 Mathematical Prerequisites Although CS103 is primarily a math class, this course does not require any higher math as a prerequisite. The most advanced level of mathematics you'll need
More informationProgramming for Engineers Introduction to C
Programming for Engineers Introduction to C ICEN 200 Spring 2018 Prof. Dola Saha 1 Simple Program 2 Comments // Fig. 2.1: fig02_01.c // A first program in C begin with //, indicating that these two lines
More informationChapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-3 Flow Of Control Flow of control refers to the
More informationIntroduction to TURING
Introduction to TURING Comments Some code is difficult to understand, even if you understand the language it is written in. To that end, the designers of programming languages have allowed us to comment
More informationCS50 Supersection (for those less comfortable)
CS50 Supersection (for those less comfortable) Friday, September 8, 2017 3 4pm, Science Center C Maria Zlatkova, Doug Lloyd Today s Topics Setting up CS50 IDE Variables and Data Types Conditions Boolean
More informationQUIZ: What value is stored in a after this
QUIZ: What value is stored in a after this statement is executed? Why? a = 23/7; QUIZ evaluates to 16. Lesson 4 Statements, Expressions, Operators Statement = complete instruction that directs the computer
More informationTOPIC 2 INTRODUCTION TO JAVA AND DR JAVA
1 TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson, and instructor materials prepared
More informationThese 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
More informationCS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG
CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG 1 Notice Assignments Reading Assignment: Chapter 3: Introduction to Parameters and Objects The Class 10 Exercise
More informationPre-Algebra Notes Unit One: Variables, Expressions, and Integers
Pre-Algebra Notes Unit One: Variables, Expressions, and Integers Evaluating Algebraic Expressions Syllabus Objective: (.) The student will evaluate variable and numerical expressions using the order of
More informationFull file at
Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class
More information2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...
Remembering numbers (and other stuff)... Let s talk about one of the most important things in any programming language. It s called a variable. Don t let the name scare you. What it does is really simple.
More informationREVIEW. The C++ Programming Language. CS 151 Review #2
REVIEW The C++ Programming Language Computer programming courses generally concentrate on program design that can be applied to any number of programming languages on the market. It is imperative, however,
More information3 The L oop Control Structure
3 The L oop Control Structure Loops The while Loop Tips and Traps More Operators The for Loop Nesting of Loops Multiple Initialisations in the for Loop The Odd Loop The break Statement The continue Statement
More informationLecture 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 informationModule 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 informationA function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).
CIT Intro to Computer Systems Lecture # (//) Functions As you probably know from your other programming courses, a key part of any modern programming language is the ability to create separate functions
More informationCS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style
CS125 : Introduction to Computer Science Lecture Notes #4 Type Checking, Input/Output, and Programming Style c 2005, 2004, 2002, 2001, 2000 Jason Zych 1 Lecture 4 : Type Checking, Input/Output, and Programming
More informationcis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1
topics: introduction to java, part 1 cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 cis20.1-fall2007-sklar-leci.2 1 Java. Java is an object-oriented language: it is
More informationIntroduction. 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 informationPart II Composition of Functions
Part II Composition of Functions The big idea in this part of the book is deceptively simple. It s that we can take the value returned by one function and use it as an argument to another function. By
More informationMITOCW watch?v=0jljzrnhwoi
MITOCW watch?v=0jljzrnhwoi The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high quality educational resources for free. To
More informationMath 25 and Maple 3 + 4;
Math 25 and Maple This is a brief document describing how Maple can help you avoid some of the more tedious tasks involved in your Math 25 homework. It is by no means a comprehensive introduction to using
More informationSTUDENT LESSON A12 Iterations
STUDENT LESSON A12 Iterations Java Curriculum for AP Computer Science, Student Lesson A12 1 STUDENT LESSON A12 Iterations INTRODUCTION: Solving problems on a computer very often requires a repetition of
More informationIntroduction to the C++ Programming Language
LESSON SET 2 Introduction to the C++ Programming Language OBJECTIVES FOR STUDENT Lesson 2A: 1. To learn the basic components of a C++ program 2. To gain a basic knowledge of how memory is used in programming
More informationC++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5
C++ Data Types Contents 1 Simple C++ Data Types 2 2 Quick Note About Representations 3 3 Numeric Types 4 3.1 Integers (whole numbers)............................................ 4 3.2 Decimal Numbers.................................................
More information(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 informationLESSON 2 VARIABLES, OPERATORS, EXPRESSIONS, AND USER INPUT
LESSON VARIABLES, OPERATORS, EXPRESSIONS, AND USER INPUT PROF. JOHN P. BAUGH PROFJPBAUGH@GMAIL.COM PROFJPBAUGH.COM CONTENTS INTRODUCTION... Assumptions.... Variables and Data Types..... Numeric Data Types:
More informationSelec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal
Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program
More informationPLT Fall Shoo. Language Reference Manual
PLT Fall 2018 Shoo Language Reference Manual Claire Adams (cba2126) Samurdha Jayasinghe (sj2564) Rebekah Kim (rmk2160) Cindy Le (xl2738) Crystal Ren (cr2833) October 14, 2018 Contents 1 Comments 2 1.1
More informationBoolean Expressions. Is Equal and Is Not Equal
3 MAKING CHOICES Now that we ve covered how to create constants and variables, you re ready to learn how to tell your computer to make choices. This chapter is about controlling the flow of a computer
More information12. Pointers Address-of operator (&)
12. Pointers In earlier chapters, variables have been explained as locations in the computer's memory which can be accessed by their identifer (their name). This way, the program does not need to care
More informationCOMP 110 Project 1 Programming Project Warm-Up Exercise
COMP 110 Project 1 Programming Project Warm-Up Exercise Creating Java Source Files Over the semester, several text editors will be suggested for students to try out. Initially, I suggest you use JGrasp,
More informationSlide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto
CS 170 Java Programming 1 The Switch Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Menu-Style Code With ladder-style if-else else-if, you might sometimes find yourself writing menu-style
More informationJava Bytecode (binary file)
Java is Compiled Unlike Python, which is an interpreted langauge, Java code is compiled. In Java, a compiler reads in a Java source file (the code that we write), and it translates that code into bytecode.
More informationThe Very Basics of the R Interpreter
Chapter 2 The Very Basics of the R Interpreter OK, the computer is fired up. We have R installed. It is time to get started. 1. Start R by double-clicking on the R desktop icon. 2. Alternatively, open
More informationC: How to Program. Week /Mar/05
1 C: How to Program Week 2 2007/Mar/05 Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers
More information6.096 Introduction to C++
MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. 6.096 Lecture 3 Notes
More informationUNIT- 3 Introduction to C++
UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage
More informationLanguage Reference Manual
ALACS Language Reference Manual Manager: Gabriel Lopez (gal2129) Language Guru: Gabriel Kramer-Garcia (glk2110) System Architect: Candace Johnson (crj2121) Tester: Terence Jacobs (tj2316) Table of Contents
More informationBoolean Expressions. Is Equal and Is Not Equal
3 MAKING CHOICES ow that we ve covered how to create constants and variables, you re ready to learn how to tell your computer to make choices. This chapter is about controlling the flow of a computer program
More information2 nd Week Lecture Notes
2 nd Week Lecture Notes Scope of variables All the variables that we intend to use in a program must have been declared with its type specifier in an earlier point in the code, like we did in the previous
More informationGeneral Syntax. Operators. Variables. Arithmetic. Comparison. Assignment. Boolean. Types. Syntax int i; float j = 1.35; int k = (int) j;
General Syntax Statements are the basic building block of any C program. They can assign a value to a variable, or make a comparison, or make a function call. They must be terminated by a semicolon. Every
More informationSTATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration
STATS 507 Data Analysis in Python Lecture 2: Functions, Conditionals, Recursion and Iteration Functions in Python We ve already seen examples of functions: e.g., type()and print() Function calls take the
More informationThe for Loop. Lesson 11
The for Loop Lesson 11 Have you ever played Tetris? You know that the game never truly ends. Blocks continue to fall one at a time, increasing in speed as you go up in levels, until the game breaks from
More informationChapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-3 Flow Of Control Flow of control refers to the
More informationUnit. Programming Fundamentals. School of Science and Technology INTRODUCTION
INTRODUCTION Programming Fundamentals Unit 1 In order to communicate with each other, we use natural languages like Bengali, English, Hindi, Urdu, French, Gujarati etc. We have different language around
More informationThe name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.
Mr G s Java Jive #2: Yo! Our First Program With this handout you ll write your first program, which we ll call Yo. Programs, Classes, and Objects, Oh My! People regularly refer to Java as a language that
More informationBits, Words, and Integers
Computer Science 52 Bits, Words, and Integers Spring Semester, 2017 In this document, we look at how bits are organized into meaningful data. In particular, we will see the details of how integers are
More informationCMSC 201 Fall 2016 Lab 09 Advanced Debugging
CMSC 201 Fall 2016 Lab 09 Advanced Debugging Assignment: Lab 09 Advanced Debugging Due Date: During discussion Value: 10 points Part 1: Introduction to Errors Throughout this semester, we have been working
More informationChapter 17. Fundamental Concepts Expressed in JavaScript
Chapter 17 Fundamental Concepts Expressed in JavaScript Learning Objectives Tell the difference between name, value, and variable List three basic data types and the rules for specifying them in a program
More informationLesson Plan. Preparation
Math Practicum in Information Technology Lesson Plan Performance Objective Upon completion of this lesson, each student will be able to convert between different numbering systems and correctly write mathematical
More informationFirst Java Program - Output to the Screen
First Java Program - Output to the Screen These notes are written assuming that the reader has never programmed in Java, but has programmed in another language in the past. In any language, one of the
More informationIntro to Computer Programming (ICP) Rab Nawaz Jadoon
Intro to Computer Programming (ICP) Rab Nawaz Jadoon DCS COMSATS Institute of Information Technology Assistant Professor COMSATS IIT, Abbottabad Pakistan Introduction to Computer Programming (ICP) What
More informationExpressions and Data Types CSC 121 Spring 2015 Howard Rosenthal
Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types
More informationComputer 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
More informationIntroduction 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 informationAN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University
AN OVERVIEW OF C, PART 3 CSE 130: Introduction to Programming in C Stony Brook University FANCIER OUTPUT FORMATTING Recall that you can insert a text field width value into a printf() format specifier:
More informationChapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers 2.4 Memory Concepts 2.5 Arithmetic
More informationSimple Java Programming Constructs 4
Simple Java Programming Constructs 4 Course Map In this module you will learn the basic Java programming constructs, the if and while statements. Introduction Computer Principles and Components Software
More informationC++ 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
More information\n is used in a string to indicate the newline character. An expression produces data. The simplest expression
Chapter 1 Summary Comments are indicated by a hash sign # (also known as the pound or number sign). Text to the right of the hash sign is ignored. (But, hash loses its special meaning if it is part of
More informationSchool of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn
CPS109 Course Notes 6 Alexander Ferworn Unrelated Facts Worth Remembering Use metaphors to understand issues and explain them to others. Look up what metaphor means. Table of Contents Contents 1 ITERATION...
More informationJQuery and Javascript
JQuery and Javascript Javascript - a programming language to perform calculations/ manipulate HTML and CSS/ make a web page interactive JQuery - a javascript framework to help manipulate HTML and CSS JQuery
More informationLecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18
Assignment Lecture 9 Logical Operations Formatted Print Printf Increment and decrement Read through 3.9, 3.10 Read 4.1. 4.2, 4.3 Go through checkpoint exercise 4.1 Logical Operations - Motivation Logical
More informationCSE 142 Su 04 Computer Programming 1 - Java. Objects
Objects Objects have state and behavior. State is maintained in instance variables which live as long as the object does. Behavior is implemented in methods, which can be called by other objects to request
More informationVisual C# Instructor s Manual Table of Contents
Visual C# 2005 2-1 Chapter 2 Using Data At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion Topics Additional Projects Additional Resources Key Terms
More informationSection 0.3 The Order of Operations
Section 0.3 The Contents: Evaluating an Expression Grouping Symbols OPERATIONS The Distributive Property Answers Focus Exercises Let s be reminded of those operations seen thus far in the course: Operation
More informationComp 11 Lectures. Mike Shah. June 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 26, / 57
Comp 11 Lectures Mike Shah Tufts University June 26, 2017 Mike Shah (Tufts University) Comp 11 Lectures June 26, 2017 1 / 57 Please do not distribute or host these slides without prior permission. Mike
More informationIf Statements, For Loops, Functions
Fundamentals of Programming If Statements, For Loops, Functions Table of Contents Hello World Types of Variables Integers and Floats String Boolean Relational Operators Lists Conditionals If and Else Statements
More informationCS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG
CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG 1 Notice Prepare the Weekly Quiz The weekly quiz is for the knowledge we learned in the previous week (both the
More informationCONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction
CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics COMP-202 Unit 1: Introduction Announcements Did you miss the first lecture? Come talk to me after class. If you want
More informationKey Concept: all programs can be broken down to a combination of one of the six instructions Assignment Statements can create variables to represent
Programming 2 Key Concept: all programs can be broken down to a combination of one of the six instructions Assignment Statements can create variables to represent information Input can receive information
More informationMaciej 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
More informationThe C++ Language. Arizona State University 1
The C++ Language CSE100 Principles of Programming with C++ (based off Chapter 2 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University
More informationChapter 2, Part I Introduction to C Programming
Chapter 2, Part I Introduction to C Programming C How to Program, 8/e, GE 2016 Pearson Education, Ltd. All rights reserved. 1 2016 Pearson Education, Ltd. All rights reserved. 2 2016 Pearson Education,
More informationC++ 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
More informationC# Programming Tutorial Lesson 1: Introduction to Programming
C# Programming Tutorial Lesson 1: Introduction to Programming About this tutorial This tutorial will teach you the basics of programming and the basics of the C# programming language. If you are an absolute
More informationUnit E Step-by-Step: Programming with Python
Unit E Step-by-Step: Programming with Python Computer Concepts 2016 ENHANCED EDITION 1 Unit Contents Section A: Hello World! Python Style Section B: The Wacky Word Game Section C: Build Your Own Calculator
More informationGetting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners
Getting Started Excerpted from Hello World! Computer Programming for Kids and Other Beginners EARLY ACCESS EDITION Warren D. Sande and Carter Sande MEAP Release: May 2008 Softbound print: November 2008
More informationIntroduction to Computation for the Humanities and Social Sciences. CS 3 Chris Tanner
Introduction to Computation for the Humanities and Social Sciences CS 3 Chris Tanner Lecture 4 Python: Variables, Operators, and Casting Lecture 4 [People] need to learn code, man I m sick with the Python.
More informationLOOPS. 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
More informationBASIC ELEMENTS OF A COMPUTER PROGRAM
BASIC ELEMENTS OF A COMPUTER PROGRAM CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING LOGO Contents 1 Identifier 2 3 Rules for naming and declaring data variables Basic data types 4 Arithmetic operators
More informationAdd Subtract Multiply Divide
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
More informationLearn Ninja-Like Spreadsheet Skills with LESSON 9. Math, Step by Step
EXCELL MASTERY Learn Ninja-Like Spreadsheet Skills with LESSON 9 Doing Math, Step by Step It s Elementary, My Dear Ninja There is a scene in the short story The Crooked Man, where Sherlock Holmes accurately
More information