Download presentation
Presentation is loading. Please wait.
Published byKory Pearson Modified over 8 years ago
1
CS 31 Discussion, Week 2 Faisal Alquaddoomi, faisal@cs.ucla.edufaisal@cs.ucla.edu Office Hours: BH 2432, MW 4:30-6:30pm, F 12:30-1:30pm (today)
2
What’s been covered so far? Program Structure Reading input (cin) and producing output (cout) Variables Expressions Types (int, double, string) Operators (arithmetic, boolean) Control Structures (if, for, while, etc.)
3
Program Structure Program consists of the following in order: – Includes, e.g. “#include ” – Global statements, e.g. “using namespace std;” – Functions Most important one now: int main() { } – As a matter of good practice, main() should always end with “return 0;” even if it’s not strictly required
4
Input and Output Output: cout << “Hello, world!” << endl; – What is endl? Input: cin >> someVariable; – Depending on the “type” of someVariable, cin will read differently – Why is there a problem with reading a number followed by a string?
5
Variables Places to put a value – But what’s a “value”? We’ll get to this with expressions and types The rule of thumb is that a variable must be “declared” before it is used – Declaration: “int myVariable;” – Use: “myVariable = 32;” – Otherwise, the compiler produces the familiar “undefined identifier” message
6
Expressions An expression is a “value” mentioned before Can be as simple as 32, or as complicated as 46+(12/3)*8 “Resolving” an expression means figuring out what its actual value is when it appears in the program – “int myVariable = 80*1000;” – When the above appears, 80*1000 is computed and stored into myVariable Variables can appear in expressions – Resolving the expression uses the value of the variable at the time of resolution
7
Types The expressions referred to earlier have “types”, which are kinds of values Sample expressions with their types: – Integer (int): 50+12*3 – Decimal (double): 3.5 + 0.7 – String: “Hello!” Variables also have types, e.g. “int myVar;” is an integer – A variable can only store expressions of its type – In an expression, a variable confers its type to the expression cout and cin are intelligent enough to deal with all the basic types
8
Operators Two kinds of operators (for now): – Arithmetic (+, -, *, /) Used for computing mathematical expressions – Boolean (>, =, <=, ==, !=) Used for performing comparisons Expressions can be a single value, but are usually composed of many values strung together by operators – (5+3) < 5 – (5+3) and 5 are expressions on their own, but ‘<‘ strings them together into a larger expression
9
Operator Precedence Just like in math, operators are not simply computed left to right – Ex: 5+3*2, the 3*2 occurs first, then 5+6 Precedence (lowest to highest): – (+,-),(*,/),(>, =,<=,==,!=) Operators take “operands” and produce a value – The final output value of the operator depends on the types of its operands The type of an expression is the value produced by the operator with the highest precedence – (5+3)*0.25 is 2.0, which is a double since the final multiplication had a double as one of its operands
10
Control Structures Aside from just printing, reading, and storing, we need some way to produce different results for different inputs (beyond just expressions) If: “if something is true, do this (else do this)” We also need a way to repeat something an unknown number of times – While and for are used for this – Will discuss these with examples
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.