View
217
Download
0
Category
Preview:
DESCRIPTION
Variables int x; int y = 0; double avgSpeed = 5.9; boot isReady = false;
Citation preview
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
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
Variables
int x;int y = 0;double avgSpeed = 5.9;boot isReady = false;
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;
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
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
Functions
void printMessage();
void printNumber(int num);
bool isRobotWorking();
double areaOfCircle(double radius);
• The declaration is also called the prototype
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
Functions
int myFunction(int param1, int param2);
int main(){
}
int myFunction(int param1, int param2){return param1 + param2;}
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
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
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);
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());
Examples
• Lab tut• Online notes
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
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
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”;
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
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);
}
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!?!?
TermsKeyword Identifier literal Types Syntax CommentsLoopConditionPrototypeDeclaration
ParameterExpressionLogic ExpressionVariableFunctionCompilerControl StatementOperatorDefinition
Operators<<>>><>=<====&&
!!=++–+=-=( ){ }||
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
Recommended