Variable Declarations

Slides:



Advertisements
Similar presentations
Ch. 3 Variables VB.Net Programming. Data Types Boolean – True or False values Short – Small integers (+/- 32,767) Integer – Medium-size integers (+/-
Advertisements

Constants and Data Types Constants Data Types Reading for this class: L&L,
Dale/Weems/Headington
Data Types and Expressions
Types and Variables. Computer Programming 2 C++ in one page!
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
CS150 Introduction to Computer Science 1
Basic Elements of C++ Chapter 2.
Data Types. Every program must deal with data The data is usually described as a certain type This type determines what you can do with the data and how.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Cosc175/data1 Data comprised of constants and variables information stored in memory Each memory location has an address Address - number identifying a.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Input & Output: Console
1 Please switch off your mobile phones. 2 Data Representation Instructor: Mainak Chaudhuri
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
CPS120: Introduction to Computer Science
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
C++ Programming: Basic Elements of C++.
Java Simple Types CSIS 3701: Advanced Object Oriented Programming.
Primitive Variables.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.
Copyright © – Curt Hill Types What they do.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
C++ Basics Tutorial 5 Constants. Topics Covered Literal Constants Defined Constants Declared Constants.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
COMP 102 Programming Fundamentals I Presented by : Timture Choi COMP102 Lab 011.
CPS120: Introduction to Computer Science Variables and Constants.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Chapter One Lesson Three DATA TYPES ©
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Primitive Data Types 1 In PowerPoint, point at the speaker icon, then click the "Play" button.
Chapter 2. Variable and Data type
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Fundamentals 2.
Chapter # 2 Part 2 Programs And data
Chapter Topics The Basics of a C++ Program Data Types
Data Types, Variables & Arithmetic
BASIC ELEMENTS OF A COMPUTER PROGRAM
Lecture 2 Data Types Richard Gesick.
Documentation Need to have documentation in all programs
Variables A variable is a placeholder for a value. It is a named memory location where that value is stored. Use the name of a variable to access or update.
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
Chapter 2.
IDENTIFIERS CSC 111.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Chapter 2 Variables.
C++ Data Types Data Type
Chapter 2: Java Fundamentals
Unit 6 - Variables - Fundamental Data Types
Data Types Imran Rashid CTO at ManiWeber Technologies.
Chapter # 2 Part 2 Programs And data
Chapter 2: Java Fundamentals
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Unit 3: Variables in Java
Variables and Constants
Data in a C++ Program Numeric data and character data
Presentation transcript:

Variable Declarations

Variables input values output values store values modify values

C++ Variable Syntax declare variables before you use them variable names must contain only alpha-numeric characters and the underscore variable names must not begin with a numeric character

Example Names Legal Illegal x, y, xyz_Hello, bob, joe12 12joe, hi-there, go!

Good Practices Declare variables at the inside top of the main function Give the variables meaningful names Good: age, tirePressure, and lateral_distortion Not so good: x Use comments to add further meaning int age; // how long the tire has been installed on the car

Integer Types: short example declaration: memory allocation: short shoeSize; short shoeSize = 5; memory allocation: 2 bytes range of values -215 to +215 or ~-32k to ~32k

Integer Types: integer example declaration: int numCarsOnHwy; int numBoatsOnLake = 5001; memory allocation: 2 or 4 bytes depending on the system Missouri S&T’s systems use 4 bytes range of values: -231 to +231 or ~-2billion to ~2billion

Integer Types: long example declaration: memory allocation: long desk_length = 123; long numKeys; memory allocation: 4 bytes range of values: -231 to +231 or ~-2billion to ~2billion

Floating Point Types: float example declaration: float bodyTemp = 98.6; float shoeSize; memory allocation: 4 bytes precision: 6 significant figures (decimal)

Floating Point Types: double example declaration: double weight_of_mountain = 746538433.55; // tons memory allocation: 8 bytes precision: 15 significant figures (decimal)

Floating Point Types: long double example declaration: long double feather_wt = 0.000000000000032; // lbs memory allocation: Not standardized precision: Very!

Non-Numeric Types: character example declaration: char continueResponse = ‘y’; memory allocation: 1 byte range of values: a char type can take on the 256 possible values of the ASCII character set

Non-Numeric Types: string example declaration: string firstName = “Clayton”; memory allocation: varies on string size note: you must use the double quotes for string variable initializations and the single quotes (or ticks) for characters

Non-Numeric Types: boolean example declaration: bool quit = false; bool passed = true; memory allocation: 1 byte note: The words true and false are reserved in C++.

Constants Used when a variables value should never be altered Examples const float PI = 3.14159; const double DISTORTION_COEFFICIENT = 5.662398; const float TAX_RATE = 0.023; PI = 4; // NO, will not compile

Const Examples const short TWO = 2; // bad float val1, val2, average; average = (val1 + val2) / TWO; const short TWO = 3; // even worse float val1, val2, val3, average; average = (val1 + val2 + val3) / TWO; const short DIVISOR = 2; // better

End of Session