Variables A piece of memory set aside to store data When declared, the memory is given a name by...

23
Variables A piece of memory set aside to store data When declared, the memory is given a name by using the name, we can access the data that sits in memory we can retrieve the value, we can change the value

description

Variables int x; int y = 0; double avgSpeed = 5.9; boot isReady = false;

Transcript of Variables A piece of memory set aside to store data When declared, the memory is given a name by...

Page 1: Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.

Variables

A piece of memory set aside to store data When declared, the memory is given a name by using the name, we can access the data

that sits in memory we can retrieve the value, we can change the

value

Page 2: Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.

Variables• a variable must be declared before it can be

usedtype name;type name = value;• type - the type of data to be stored

– integers (int, long)– real numbers(float, double)– characters (char)– logic (bool)

• name - a legal identifier• value - a literal value

Page 3: Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.

Variables

int x;int y = 0;double avgSpeed = 5.9;boot isReady = false;

Page 4: Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.

Variables

• Once declared, you just use the name to access the data

double avgSpeed = 3.5;double time = 100.0;double distTravelled = 0.0;

cout << avgSpeed;avgSpeed += 1;cout << avgSpeed;

distTravelled = avgSpeed * time;

Page 5: Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.

Functions

• Functions are blocks of code that perform a specific task

• Functions are assigned a name and can be called many times

• Functions can accept input and provide an output

• Functions MUST be declared before they can be used

• Functions that are declared MUST also be defined

Page 6: Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.

Functions

• Declaring– done before use, so let's put it before our main

return name(param,param, ...);

– return - the type of output we expect• void means no output, or• int, float, double, long, bool mean return of that

type– name - a legal identifier– param - a variable to represent the input

Page 7: Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.

Functions

void printMessage();

void printNumber(int num);

bool isRobotWorking();

double areaOfCircle(double radius);

• The declaration is also called the prototype

Page 8: Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.

Functions

• If we declare a function, we must also define it– give it some instructions to complete its task

• We usually define it near the end of out code, after the main

• the definition starts with the same prototype as before

– has a block of code– if an output is needed, must call return

Page 9: Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.

Functions

int myFunction(int param1, int param2);

int main(){

}

int myFunction(int param1, int param2){return param1 + param2;}

Page 10: Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.

Functions

• Inside the definition we can have any legal code

– variable declarations– operations– calls to other functions

• The parameter variables exist as if they were declared inside the function

• A function that has an output MUST call return– once return is called, the function is over

Page 11: Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.

Functions

• Using functions– we use functions to wrap up a block of code– this block of code is then simply called by its

name– the function can be called once, or multiple

times– when a function is called, we jump to its

definition and execute its code, once finished we return to where the call originated and continue from there

Page 12: Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.

Functions• Parameters

– parameters are variables that hold the input to the function

– when you call a function with parameters you MUST provide values to those variables

int getAvg(int num1, int num2);– to call

getAvg(3,4);

int y = 2;int x = 3;getAvg(y,x);

Page 13: Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.

Functions

• Returns (output)– functions that return a value are used in the

same manner as variables– the value can be output to the screen, put into a

variable, sent into another function, used in an expression

cout << getAvg(3,4);double y = areaOfCircle(r);int minTemp = calcTemp(getPressure());

Page 14: Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.

Examples

• Lab tut• Online notes

Page 15: Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.

If statements

• Put a condition on a block of code• IF the condition is TRUE, the code will be

executed

if(x > 3){cout << “Success!” << endl;

}

• allows us to program decisions and choice

Page 16: Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.

If Statements

• We can also include other options• The computer will evaluate each options

condition until it finds one that is true• once a successful condition is met, the IF

statement is finished• options are written else if• a “catch-all” option is written else

Page 17: Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.

if (grade < = 100 && grade > 79){cout << “you got an A”;

}else if (grade > 69) {

cout << “you got a B”;}else if (grade > 59){

cout << “you got a C”;}else if (grade > 49)

cout << “you got a D”;else{

cout << “you FAILED”;}cout << “endl”;

Page 18: Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.

While Loops

• Allows us to attach a condition to a block of code

• The code will be executed over and over again, until the condition becomes false

• if the condition never becomes false, the code never stops

• if the condition is never true, the code will never be executed

Page 19: Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.

While Loops

//stores data, return false when bank is fullbool storeData(double in);

… ....

bool readData = true;while(readData == true){

cout << “enter your number: “ ;cin >> inputData;readData = storeData(inputData);

}

Page 20: Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.

Logical Expressions

• Evaluate to either true or false• operators

– >,>=, <, <=, !, ==

• expressions can be combined– && means both conditions are true– || means either condition is true

• REMEMBER OUR TRUTH TABLE!?!?

Page 21: Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.

TermsKeyword Identifier literal Types Syntax CommentsLoopConditionPrototypeDeclaration

ParameterExpressionLogic ExpressionVariableFunctionCompilerControl StatementOperatorDefinition

Page 22: Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.

Operators<<>>><>=<====&&

!!=++–+=-=( ){ }||

Page 23: Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.

Rules

Identifiers- letters, numbers, _- can't start with number,

or _- should make sense

Semi-colons- all expressions end

with a ;

Declaration- Functions and

variables must be declared, before they are used

Brackets- if you start a bracket,

finish a bracket