Lecture 2: Programming Basics & Variables
description
Transcript of Lecture 2: Programming Basics & Variables
![Page 1: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/1.jpg)
LECTURE 2:PROGRAMMING BASICS & VARIABLES
CSC 107 – Programming For Science
![Page 2: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/2.jpg)
Announcements
Initial set of slides had wrong day for midterm Correct date on syllabus and in Angel
Midterm is on October 21
Class time is not 60 minutes, but 75 minutes I screwed this up on Tuesday and wanted to
apologize If time permits, still have Tuesday’s actual
activity(otherwise I will post it & solution to Angel)
![Page 3: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/3.jpg)
History of C
Dennis Ritchie developed C from 1969 – 1973
Based upon B (& other) earlier languages Since its creation, language grown
organically Tradition of adding features beyond standard
as desired
![Page 4: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/4.jpg)
History of C++
Bjarne Stroustrup created to add “objects” Also included many other improvements to
language Name is inside joke: "++" is increment
operator in C Updated for quick growth
2.0 release in 1989 1998 adopted as ISO standard C++ 201x in development now
![Page 5: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/5.jpg)
C Versus C++C++ is designed to be
as compatible with C as possible, thereby
providing a smooth transition from C
![Page 6: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/6.jpg)
C Versus C++
C++
C
![Page 7: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/7.jpg)
C Versus C++
C
![Page 8: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/8.jpg)
C Versus C++
Latest definition of C added most C++ features Not classes & objects, these only found in
C++ For this reason, also not a part of CSC 107 Differences now minimal and easily
avoided Once objects removed, C++ just
“looser” C Removes annoying restrictions that had
been in C Since makes life easier, often supported in
C anyway
![Page 9: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/9.jpg)
![Page 10: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/10.jpg)
Computers are VERY, VERY stupid
![Page 11: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/11.jpg)
Computers have no common-sense They will only do what you tell them to do NOT what you want them to do, which
often differs
While this is true for every computer does Programming highlights exactly how this
happens As you will see, C++ does nothing to
prevent issues
![Page 12: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/12.jpg)
Computers have no common-sense They will only do what you tell them to do NOT what you want them to do, which
often differs
![Page 13: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/13.jpg)
Case-Sensitivity
Example of computers being very literal And language not helping by fixing what you
say
main, Main, & MAiN treated as different words Case of the letters matters, not just the
words Could be different, so C++ won’t change Main to main
Can help prevent easy mistakes from swapping names
With just a little practice, becomes second nature
![Page 14: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/14.jpg)
“Whitespace”
One (very small) way C++ actually helps you C++ treats whitespace equally – spaces,
enters, & tabs Whether 1 or 1000000000000 – all will be
ignored Cannot use in symbol, whitespace
splits words Treats these as different “: :” and “::”
Spaces between words needed, but not counted Wecansplitwordsbutthecomputercannot
![Page 15: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/15.jpg)
“Whitespace”
One (very small) way C++ actually helps you C++ treats whitespace equally – spaces,
enters, & tabs Whether 1 or 1000000000000 – all will be
ignored Cannot use in symbol, whitespace
splits words Treats these as different “: :” and “::”
Spaces between words needed, but not counted Wecansplitwordsbutthecomputercannot
![Page 16: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/16.jpg)
Your First C++ Program
#include <iostream>using std::cout;
int main() { /* Hi, Mom. This is a comment
that goes over 2 line. */ std::cout << “Hello world!”; return 0; // This comment goes to the line’s end}
![Page 17: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/17.jpg)
#include Statements
#include <iostream>using std::cout;
/* Hi, Mom. This is a comment that goes over 2 line. */int main() { std::cout << “Hello world!”; return 0; // This comment goes to the line’s end}
Nearly every C++ file begins with this directive May add more #include to include other
files Contents of included file usable as if it
were here Easy way to copy ideas across multiple
files Programs can use two types of #include
statements Include system file using #include <filename>
#include “filename” includes a file you wrote
![Page 18: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/18.jpg)
Watch Me Pull a Rabbit
#include <iostream>using std::cout;
/* Hi, Mom. This is a comment that goes over 2 line. */int main() { std::cout << “Hello world!”; return 0; // This comment goes to the line’s end}
For now, automatically start each file with this line Details are unimportant – consider it
magic
![Page 19: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/19.jpg)
Watch Me Pull a Rabbit
#include <iostream>using std::cout;
/* Hi, Mom. This is a comment that goes over 2 line. */int main() { std::cout << “Hello world!”; return 0; // This comment goes to the line’s end}
For now, automatically start each file with this line Details are unimportant – consider it
magic
![Page 20: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/20.jpg)
Your First C++ Program
#include <iostream>using std::cout;
int main() { /* Hi, Mom. This is a comment
that goes over 2 line. */ std::cout << “Hello world!”; return 0; // This comment goes to the line’s end}
![Page 21: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/21.jpg)
Using Commands
#include <iostream>using std::cout;
/* Hi, Mom. This is a comment that goes over 2 line. */int main() { std::cout << “Hello world!”; return 0; // This comment goes to the line’s end}
More “magic”, but using has less important purpose Tells compiler we are lazy & save some
typing Two types of using statements to choose
from Specify single shortcut with using std::cout
using std; gives you a list of shortcuts to use
Unlike #include statements, using never required Do not worry about it – will not be using
them
![Page 22: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/22.jpg)
Your First C++ Program
#include <iostream>using std::cout;
int main() { /* Hi, Mom. This is a comment
that goes over 2 line. */ std::cout << “Hello world!”; return 0; // This comment goes to the line’s end}
![Page 23: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/23.jpg)
main Function
#include <iostream>using std::cout;
int main() { /* Hi, Mom. This is a comment
that goes over 2 line. */ std::cout << “Hello world!”; return 0; // This comment goes to the line’s end
}
All C++ programs contain function called main Tells computer where to start running
program Code inside the braces will be what is
executed For the moment, consider this more “magic”
![Page 24: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/24.jpg)
main Function
#include <iosteam>using std::cout;
int main() { /* Hi, Mom. This is a comment
that goes over 2 line. */ std::cout << “Hello world!”; return 0; // This comment goes to the line’s end
}
All C++ programs contain function called main Tells computer where to start running
program Code inside the braces will be what is
executed For the moment, consider this more “magic”
![Page 25: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/25.jpg)
Comments
Vital for writing and maintaining any program Not required to run program - only for
human eyes Computer simply ignores anything in a
comment Use to describe code in simple English
Sie konnen auch auf Deutsch screiben o U c%d wrte n txt msg
Should be used liberally I add comments where cannot see what
code does Impossible to have too many comments, if
readable
![Page 26: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/26.jpg)
Comments in C++
Double slash comments continue to line’s enda = a – 4; // Hi, Mom!// This entire line is a comment!
/* … */ comments can be on one or more linesa = a - /* Hi, Mom! */ 4;/* This comment takes an entire line. *//* This is a really long comment that * goes on to multiple lines. The stars on * lines 2 and on are optional, but * makes things easier to read. */
![Page 27: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/27.jpg)
Pre-processor Directives
Code “pre-processed” before compilation No need to request it --- automatically
occurs Easier-to-read code results from this
process Just like using comments -- notice a recurring
theme? Pre-processor directives start with #
One directive per line & nothing else on the line
Directives should not span multiple lines
![Page 28: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/28.jpg)
Symbolic Constants
Directive can be used to name a constant Any/all lines BELOW directive can use this
constant Pre-processor replaces name with value
Compiler sees value as if that were written there
When reading the code, programmer sees name
Makes code much easier to read, write, debug
Names traditionally in all CAPITAL letters THIS IS NOT REQUIRED, but common
convention
![Page 29: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/29.jpg)
What You Write And Work With#define PI 3.1415962#define AVOGADRO 6.022E23 #define MY_NAME “Matthew Hertz”#define DUMB_EXAMPLE MY_NAMEdouble area = PI * (r * r);cout << MY_NAME;cout << DUMB_EXAMPLE;
![Page 30: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/30.jpg)
What The Compiler Sees
#define PI 3.1415962#define AVOGADRO 6.022E23 #define MY_NAME “Matthew Hertz”#define DUMB_EXAMPLE MY_NAMEdouble area = PI * (r * r);cout << MY_NAME;cout << DUMB_EXAMPLE;
![Page 31: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/31.jpg)
What The Compiler Sees
#define AVOGADRO 6.022E23 #define MY_NAME “Matthew Hertz”#define DUMB_EXAMPLE MY_NAMEdouble area = 3.1415962 * (r * r);cout << MY_NAME;cout << DUMB_EXAMPLE;
![Page 32: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/32.jpg)
What The Compiler Sees
#define AVOGADRO 6.022E23 #define MY_NAME “Matthew Hertz”#define DUMB_EXAMPLE MY_NAMEdouble area = 3.1415962 * (r * r);cout << MY_NAME;cout << DUMB_EXAMPLE;
![Page 33: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/33.jpg)
What The Compiler Sees
#define MY_NAME “Matthew Hertz”#define DUMB_EXAMPLE MY_NAMEdouble area = 3.1415962 * (r * r);cout << MY_NAME;cout << DUMB_EXAMPLE;
![Page 34: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/34.jpg)
What The Compiler Sees
#define DUMB_EXAMPLE “Matthew Hertz”double area = 3.1415962 * (r * r);cout << “Matthew Hertz”;cout << DUMB_EXAMPLE;
![Page 35: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/35.jpg)
What The Compiler Sees
double area = 3.1415962 * (r * r);cout << “Matthew Hertz”;cout << “Matthew Hertz”;
![Page 36: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/36.jpg)
What The Compiler Sees
double area = 3.1415962 * (r * r);cout << “Matthew Hertz”;cout << “Matthew Hertz”;
![Page 37: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/37.jpg)
Variables
Variable names location to store data Memory location's initial value is unknown Assignments update memory location with
new value Memory location updated by assignment
ONLY When variable is used in program…
…uses current value at that memory location
Just about everything (interesting) uses variables
![Page 38: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/38.jpg)
Variable Declarations
Variables must be declared before can be used Way of getting computer to make space for
variable States how to interpret memory in future
uses Allows the compiler to check if uses are
legal Declarations must include two pieces:
Each variable must have legal, unique name
Type of data that the variable stores
![Page 39: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/39.jpg)
Variable Names
Begin with letter or underscore (_) Then use any letters, numbers, or
underscore C++ case-sensitive when naming
variables Will treat as different Mass, mass, & masS
Unique name* needed for each variable Computer wouldn't know which of 1,000 bobs to use
Reserved words are… reserved and can't be used Includes all type names on p. 83 of book void, unsigned, class also reserved words
![Page 40: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/40.jpg)
Variable Name Conventions
Usually names begin with lowercase letter Helps clarify variables & symbolic constants
Provide good idea of what variable stores Split multiple uses into multiple variables
Some things always make for bad names tmp, b, l (lowercase letter L) Anything would not say to parents and/or
priest
![Page 41: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/41.jpg)
Variable Name Conventions
Usually names begin with lowercase letter Helps clarify variables & symbolic constants
Provide good idea of what variable stores Split multiple uses into multiple variables
Some things always make for bad names tmp, b, l (lowercase letter L) Anything would not say to parents and/or
priest
![Page 42: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/42.jpg)
Data Types
Each variable also has data type How program treats variable’s value
defined by this Single true or false value held by bool C/C++ defines 7 numeric data types
Integer types: short, int, long, long long Decimal types: float, double, long double Ranges for each type is not really
standardized Non-negative versions using unsigned
______ char data type can hold a character
![Page 43: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/43.jpg)
Representing Text
Most computers you find follow ASCII standard American Standard Code for Information
Interchange 256 (= 28) possible characters in extended
definition Since computers are stupid, need to set
fixed size Only use 0s & 1s within computer – all
it knows Number still stored, but character is
displayed For number 97, a is printed Prints & for number 38 For number 55, 7 is printed
![Page 44: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/44.jpg)
ASCII Table
![Page 45: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/45.jpg)
There Is No Character
For computer, there are no characters Add to actual number just like normal
addition: ’M’ + 3 = 77 + 3 = 80 (’P’) ’0’ + 5 = 48 + 5 = 53 (’5’) 9 + ’1’ = 49 + 9 = 58 (’:’) ’1’+’0’ = 49 + 48= 97 (’a’)
Can also use to subtract, divide, any other operation
![Page 46: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/46.jpg)
Writing Variable Declarations Single variable declared as: type name;double goodNameExample;short bad;
Can also declare multiple variables at once:int i, j;long double k, l, m, n, o, p;float thisIsAReallyLongName, thisIsAnotherLongName;
![Page 47: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/47.jpg)
Writing Variable Declarations Could also specify initial value for
variable Variable, constant, literal, or expression can
be usedint i = 0.0;long j = -1;long double k = -0.000232847812;long l = j, many, minusJ = -j;char c = ‘i’;char newLine = ‘\n’;char tab = ‘\t’;
![Page 48: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/48.jpg)
Writing Variable Declarations Could also specify initial value for
variable Variable, constant, literal, or expression can
be usedint i = 0.0;long j = -1;long double k = -0.000232847812;long l = j, many, minusJ = -j;char c = ‘i’;char newLine = ‘\n’;char tab = ‘\t’;
![Page 49: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/49.jpg)
Constants
Constants very similar to variables Must be declared with a data type and
unique name const data_type var_name declares
variable Value of constant fixed when declared,
however Variables & constants treated and used
similarly
![Page 50: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/50.jpg)
Your Turn
Get in groups & work on following activity
![Page 51: Lecture 2: Programming Basics & Variables](https://reader035.fdocuments.us/reader035/viewer/2022062501/5681603d550346895dcf6057/html5/thumbnails/51.jpg)
For Next Lecture
Read sections 5.1 – 5.4 & 5.6, 6.1 – 6.6 for Tues. What is this cout thing, anyway? Can we read in input from the keyboard? What operations exist for us to use with
variables? What do we mean by order of operations?
Week #1 weekly assignment due Tuesday Problems available on Angel – covered 1st
two already If problem takes more than 10 minutes,
TALK TO ME!