Download presentation
Presentation is loading. Please wait.
Published byMaximillian Norman Modified over 9 years ago
1
CSC 107 – Programming For Science
2
Announcements
3
The Week’s Goal
4
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
5
Variables, Constants, & More General CasesExamples Variable Declaration dataType name; dataType name = value; dataType name, anotherName; dataType name = value, anotherName; int count; bool monkey = true; char help,letter; char a=‘a’,letter; Constant Declaration const dataType name = value; const double PI=3.1; Symbolic Constant #define NAME value #define AGE 34
6
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 char data type used to store a character
7
Variable Names Begin with letter or underscore ( _ ) Then use any letters, numbers, or underscore Unique name * needed for each variable Computer wouldn't know which of 1,000 bob s 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
8
Program Outline Once upon a time… … some stuff happens… … and they all lived happily ever after
9
Program Outline Once upon a time… All programs must begin somewhere Defines what is worked upon during rest of program For non-trivial programs, requires receiving input When starting program, first steps always same: 1. What is the input? 2. What will the input look like? 3. How will the data be entered?
10
Reading From The Keyboard Easiest to get input from the keyboard Reading from files possible; discussed later in term C++ lacks standard, so writing GUI much harder C++ defines cin to get user’s input As easy to use as delivering food to Granny’s house When cin hit, program waits until it has input User must press enter for line to be able to be read Editing not seen by program; only receives final line
11
Programming Using cin Used to read one or more values at once: cin >> variable ; cin >> variable1 >> variable2 ; Reads where last cin stopped reading input Automatically skips past whitespace Data type of variable determines what is read Stops reading at first non-usable value in input If input is not usable, will set variable equal to 0
12
cin Example >>
13
cin Example >>
14
cin Example >>
15
Program Outline Once upon a time… Get the input using cin from the keyboard Can use cin any time, but usually precedes cool stuff
16
Program Outline Once upon a time… … some stuff happens… Want to do something with variable declared C++ has large variety of statements to do this First statement covered in today’s lecture
17
Program Outline
18
Assignments Variable declaration creates “box” to store data Box can get values placed in it using assignments General form of assignment is variable = expression; Computer works by first evaluating expression Single value must result from this expression Value of variable set to this result
19
What Is The Expression ? Simplest expressions are literal values Examples: double d; int i; char doe; i = 6; i = 7; d = -7; d = 34.5691; doe = ‘a’; doe = ‘0’; 125612.345-56‘a’
20
What Is The Expression ? Examples of other simple expressions double d; int i; char doe; i = 6; i = 7; d = -i; i = d; d = 34.5691; i = d; doe = ‘0’; // 0 == ASCII 48 i = doe; doe = i;
21
Data Types Assignments are legal only if always safe C++ defines ordering of legal assignments long double double float long int short char Legal to assign to higher type
22
What Is The Expression ? Can also include basic arithmetic operators Addition +i = 4 + 6; Subtraction -d = i – 2.3; Multiplication *i = 120 * 8; Division /d = 4.0 / i; Modulus %i = 120 % 8; Modulus computes remainder between two integers: 4 % 5 equals 4 5 % 4 equals 1 9 % 3 equals 0 12823 % 812 equals 643
23
Tracing A Program Important for understanding & debugging code Step-by-step execution of program shown To see what is happening, done via pencil-and-paper Execute each line of program like computer does Within trace, add row whenever variable declared Update variable’s value each time it is assigned Off to side, show any output from cout statements
24
Program Trace 1 int x = 4 + 2; 2 int y = 8 * 1; 3 double z = y – 3; 4 x = x + 1; 5 y = 7 % x; 6 z = y + 1.0 / 2.0; 7 z = 8.0 / 4 + x * x; 8 y = (x – 3) * (y + 2); 9 y = x / 4; 10 cout << x << “ ” << y << “ ” << z << endl;
25
Integer Division Dividing two integers computes an integer Literals or variables does not matter, only their type.0 Important to remember, 12 is integer & 12.0 is not C++ ignores result after decimal to get integer 2 / 5 equals 0 (the.4 was thrown away) 5 / 2 equals 2 (the.5 was thrown away) 16 / 4 equals 4 -5 / 2 equals -2 (the.5 was thrown away) 2.0 / 5 equals 0.4 ( 2.0 is not an integer!)
26
Floating Point Arithmetic Operations using decimal has decimal result Even if whole number is result of the operation For example, all the assignments to i are illegal: int i; double d = i; i = 6.0 / 3.0; i = 2.0 * d; i = d + 1; i = 4 * 2.0; i = (d * 1) + 5; i = 8 + (9 * 3) – (2 / 1.0) * 4 + 2;
27
Priority of Operations Equations can become very complex 4 + 5 * 6 * 9 - 2 + 1 = …? Very Very strict order of operations used by computer ( ) Solve from inner- to outermost + (positive) & - (negative) Solve from right to left * & % & / (division) Solve from left to right + (addition) & - (subtraction) Solve from left to right use lots of parentheses My suggestion: use lots of parentheses
28
Compound Assignment Operators Short simple operators that allow us to be lazy Save some typing for several common actions Lowest priority operation; expression evaluated first OperatorEquivalent C++ Expression a += 2;a = a + 2;a = a + 2; b -= d;b = b – d;b = b – d; c *= 4 + 5.6;c = c * (4 + 5.6); d /= 0.3 * e;d = d / (0.3 * e);
29
How To Shoot Yourself in Foot Also increment ( ++ ) & decrement ( -- ) operators Use with variables only; no exceptions possible Used anywhere to save typing an additional line Two different ways these operators applied v = ++b % c; b = b + 1; v = b % c; c = f * --h; h = h – 1; C = f * h; a = b++ * c; a = b * c; b = b + 1;
30
Program Outline … and they all lived happily ever after All good processing comes to end & report results Shows program worked and provides feedback Results takes many forms, focus on printing today When starting program, last part asks: 1. What must be output? 2. How can output be presented best? 3. Will it be pretty?
31
Using cout to Print Already seen how to print text using cout cout << “Hello World” << endl; Prints out whatever is placed between quotes endl goes to next line and prints out immediately Use escape sequences for fancier text output \n newline (move to start of next line) \t tab (go to next column that is multiple of 8) \\ \ (backslash character) \” “ (quotation mark)
32
Can Print Out Multiple Items cout can also print out value of variables int bob = 2; double j = 4.5; char var = ‘a’; cout << “Hello ”; cout << bob << endl; cout << j << endl; cout << var << endl; cout << j << “ is not ” << bob << endl; cout << bob << “ equals bob” << endl; cout << var << bob << j << endl;
33
But Can It Be Used? cout built to provide basics needed to work Prints out as many digits as needed No extra spaces or tabs used Scientific notation cannot be used Often want to format results Significant digits can matter Tables make reading faster
34
Real World Strikes Again TrollPrincess
35
First Way To Format Output #include using namespace std; int main() { int k = 4; cout << k << endl; cout.width(4); cout << k << endl; cout << k << endl; cout.setf(ios::showpos); cout << k << endl; cout.width(3); cout.setf(ios::left); cout << k << k << endl; }
36
Second Way To Format Output #include #include using namespace std; int main() { int k = 4; cout << k << endl; cout << setw(4) << k << endl; cout << k << endl; cout.setf(ios::showpos); cout << k << endl; cout.setf(ios::left); cout << setw(3) << k << k << endl; }
37
Your Turn Get in groups of 3 & work on following activity
38
For Next Lecture Read sections 6.7 for Thursday What do we mean by order of operations? Week #1 weekly assignment due today Problems available on Angel If problem takes more than 10 minutes, TALK TO ME! Week #2 weekly assignment posted today Will be due week from today
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.