CS 31 Discussion, Week 2

10
CS 31 Discussion, Week 2 Faisal Alquaddoomi, [email protected] Office Hours: BH 2432, MW 4:30-6:30pm, F 12:30-1:30pm (today)

description

CS 31 Discussion, Week 2. Faisal Alquaddoomi, [email protected] Office Hours: BH 2432, MW 4:30-6:30pm, F 12:30-1:30pm (today). What’s been covered so far?. Program Structure Reading input ( cin ) and producing output ( cout ) Variables Expressions Types ( int , double, string) - PowerPoint PPT Presentation

Transcript of CS 31 Discussion, Week 2

Page 1: CS 31 Discussion, Week 2

CS 31 Discussion, Week 2

Faisal Alquaddoomi, [email protected]

Office Hours: BH 2432,MW 4:30-6:30pm,

F 12:30-1:30pm (today)

Page 2: CS 31 Discussion, Week 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.)

Page 3: CS 31 Discussion, Week 2

Program Structure

• Program consists of the following in order:– Includes, e.g. “#include <iostream>”– 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

Page 4: CS 31 Discussion, Week 2

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?

Page 5: CS 31 Discussion, Week 2

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

Page 6: CS 31 Discussion, Week 2

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

Page 7: CS 31 Discussion, Week 2

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

Page 8: CS 31 Discussion, Week 2

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

Page 9: CS 31 Discussion, Week 2

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

Page 10: CS 31 Discussion, Week 2

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