CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

C++ Basics Variables, Identifiers, Assignments, Input/Output.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Constants and Data Types Constants Data Types Reading for this class: L&L,
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Structure of a C program
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
CS150 Introduction to Computer Science 1
String Escape Sequences
Data Types.
CPS120: Introduction to Computer Science Lecture 8.
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Chapter 2: Using Data.
CPS120: Introduction to Computer Science
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
CHAPTER # 2 Part 2 PROGRAMS AND DATA 1 st semster King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
Copyright Curt Hill Variables What are they? Why do we need them?
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
COMP 102 Programming Fundamentals I Presented by : Timture Choi COMP102 Lab 011.
CPS120: Introduction to Computer Science Variables and Constants.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
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.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
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
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
BASIC ELEMENTS OF A COMPUTER PROGRAM
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.
Chapter 2: Introduction to C++
IDENTIFIERS CSC 111.
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
CMSC 104, Section 4 Richard Chang
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.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Variables in C Topics Naming Variables Declaring Variables
Basics of ‘C’.
C++ Data Types Data Type
Chapter 2: Java Fundamentals
Variables in C Topics Naming Variables Declaring Variables
Variables in C Declaring , Naming, and Using Variables.
Chapter # 2 Part 2 Programs And data
Chapter 2: Java Fundamentals
Chapter 2: Introduction to C++.
Primitive Types and Expressions
Variables in C Topics Naming Variables Declaring Variables
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
Variables in C Topics Naming Variables Declaring Variables
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Variables in C Topics Naming Variables Declaring Variables
Variables and Constants
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B

Variables Used to store values in virtually every computer program Used for “remembering” things during program execution Variables have names, types and values Values can change during execution

Data Types You need to first choose an appropriate data type when you use a variable.Values can either be: whole numbers decimal numbers letters (i.e. characters) whole words (i.e. string values

Choosing a Type Most computer languages have a select number of different data types You must select the proper data type for each variable that you use in a program in order to program efficiently This decreases memory (RAM) usage This increases the speed of your program

Data Types - Whole Numbers To store whole numbers in a variable, we use a variable of the int data type. An int variable uses 4 bytes of memory. An int variable can store a number as low as -2,147,483,648. An int variable can store a number as high as 2,147,483,647.

Data Types - Decimal Numbers To store decimal numbers in a variable, we use a variable of the double data type A double variable uses 8 bytes of memory A double variable can store a number as low as -1.7 x A double variable can store a number as high as 1.7 x A double variable can store a number with up to 15 digits of precision (significant digits)

Data Types - Characters To store a letter or a single character (such as #, $, *, etc.), we use a variable of the char data type. A char variable only uses 1 byte of memory. A char variable can only hold one letter, digit, or character.

Data Types – Words / Phrases To store a word or phrase (string value), we use a variable that is a string Technically string is not a data type You can think of it as a data type for now

Data Types – True and False The data type bool is useful to store true and false values Alternatively, we can simply use an int variable with either a 1 value (to represent true) or a 0 value (to represent false) if necessary

Other Data Types unsigned char, short, unsigned int, long, and unsigned long for whole numbers float and long double for decimal values

Using Variables in C++ Variables must be declared before they are used in C++. Get into the habit of doing this at the top of your functions char grade; // a students semester grade int numStudents; // number of students in our class double price; // price of item string userName; // user's name

Variable Names Variable names are technically known as identifiers Choose your own variable names but you must be careful to use valid ones. Otherwise, the compiler will be confused and errors will result. When choosing your variable names: do not use keywords that are defined in the programming language (Reserved Words) do not include spaces or other disallowed characters do not use more than 31 characters do begin the identifier with a letter Remember, C++ is completely case sensitive

Conventions for Naming Variables Use a conventional method of making your variables easy to read at a quick glance. For example: 1.Begin variable identifiers with lowercase letters (eg. score) if you wish to use more than one word within the identifier, you must capitalize the following words or parts of words (eg. semesterGrade, testScore) 2.Separate successive words with underscore characters ( _ ) (eg. semester_grade, card_value) 3.Hungarian notation Begin with type (eg. iTestScore)

Common Reserved Words break case char const default do double else extern float for if int long return switch void while

Initializing Variables C++ does not automatically initialize all variables to the value 0 If you do not initialize a variable to a certain value, the variable will have an indeterminate value that can corrupt the logic of your programindeterminate value You should usually initialize your variables at the same time that you declare them. This is done with a declaration statement that is also an initialization statement int numberOfPizzas = 3; double monthlyCarPayment = 685; char letterGrade = 'A'; string firstName = "Paul";

Constants Sometimes you need to use the same value many times throughout a program. In this case, it is proper to use a constant rather than a variable Constants allow you to give a name to a value used several times in a program The value never changes

Use of Constants (Literals) Numeric Characters 'a' '7' '*' Strings (a sequence of symbols "I will be an better person "

Naming Constants Constants are defined in a way that is similar to variables Select a data type and give the constant a name Any valid identifier name can be used to name a constant Start with letter or underscore Can’t use reserved words

Conventions for Naming Constants Traditionally, all uppercase letters have been used when naming constants Use the underscore character ( _ ) between consecutive words. This allows other programmers to be able to "pick out" your constants at a quick glance Examples: const double PI = const double PA_SALES_TAX = 0.06 const int SPEED_OF_LIGHT = ; // commas can't be used here

Type Compatibilities You cannot store a value of one type in a variable of a different type – a type mismatch occurs Promotion occurs automatically You can typecast Supply the name of the data type you want to use to interpret the variable followed by the variable placed in parenthesis C = PI * float (diameter);