Variables A piece of memory set aside to store data

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
Advertisements

Computer Science 1620 Loops.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Program Design and Development
How Create a C++ Program. #include using namespace std; void main() { cout
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
The If/Else Statement, Boolean Flags, and Menus Page 180
Libraries Programs that other people write that help you. #include // enables C++ #include // enables human-readable text #include // enables math functions.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Basic Elements of C++ Chapter 2.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
CH Programming An introduction to programming concepts.
CIS Computer Programming Logic
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
CS102 Introduction to Computer Programming Chapter 4 Making Decisions.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
CPS120: Introduction to Computer Science Decision Making in Programs.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
1 CS161 Introduction to Computer Science Topic #8.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Fundamental Programming Fundamental Programming More Expressions and Data Types.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
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.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
A Sample Program #include using namespace std; int main(void) { cout
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Bill Tucker Austin Community College COSC 1315
Chapter Topics The Basics of a C++ Program Data Types
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
REPETITION CONTROL STRUCTURE
Chapter 1.2 Introduction to C++ Programming
Variables Mr. Crone.
Selection (also known as Branching) Jumail Bin Taliba by
Bill Tucker Austin Community College COSC 1315
Suppose we want to print out the word MISSISSIPPI in big letters.
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
Introduction to Functions
User Defined Functions
And now for something completely different . . .
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Bools & Ifs.
Chapter 7 Conditional Statements
Flow Control Statements
Selection Control Structure
CPS120: Introduction to Computer Science
CS150 Introduction to Computer Science 1
Fundamental Programming
CS150 Introduction to Computer Science 1
Chap 7. Advanced Control Statements in Java
Presentation transcript:

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 used type 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; 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 return name(type param, type param, ...); done before use, so let's put it before our main return name(type param, type 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(){ } 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) cout << getAvg(3,4); 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());

Functions Pass by Reference if we include the & symbol before a parameter, we pass it in by reference. Thus, any changes made to that variable are reflected in the variable that was passed into the function int fun(int& a);

Functions void fun(int& a); main(){ int x = 0; fun(x); cout << a; //what will this output? } void fun(int& a){ a = 1

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){ } else if (grade > 69) { 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 full bool 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!?!?

Terms Keyword Parameter Identifier Expression literal Logic Expression Types Syntax Comments Loop Condition Prototype Declaration Parameter Expression Logic Expression Variable Function Compiler Control Statement Operator Definition

Operators << >> > < >= <= == = && ! != ++ – += -= ( ) { } ||

Rules Identifiers Semi-colons Declaration Brackets - 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