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.

Slides:



Advertisements
Similar presentations
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Advertisements

True or false A variable of type char can hold the value 301. ( F )
Computer Science 1620 Loops.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
Computer Science 1620 Functions. Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2)
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
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.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
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
1 Chapter 9 Scope, Lifetime, and More on Functions.
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.
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.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
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.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
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.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
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.
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.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
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.
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
Bill Tucker Austin Community College COSC 1315
Suppose we want to print out the word MISSISSIPPI in big letters.
Basic Elements of C++.
Variables A piece of memory set aside to store data
Basic Elements of C++ Chapter 2.
Introduction to Functions
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Bools & Ifs.
Chapter 7 Conditional Statements
Flow Control Statements
Selection Control Structure
CS150 Introduction to Computer Science 1
Fundamental Programming
Repetition Statements (Loops) - 2
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; 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 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 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 Identifier literal Types Syntax Comments Loop Condition Prototype Declaration Parameter Expression Logic Expression Variable Function Compiler Control Statement Operator Definition

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