1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Computer Programming w/ Eng. Applications
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
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
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Introduction to C Programming CE Lecture 2 Basics of C programming.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
CS150 Introduction to Computer Science 1
A First Book of ANSI C Fourth Edition
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
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.
1 CSC103: Introduction to Computer and Programming Lecture No 6.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
CHAPTER # 2 Part 2 PROGRAMS AND DATA 1 st semster King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
Primitive Variables.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
VARIABLES AND DATA TYPES Chapter2:part1 1. Objectives: By the end of this section you should: Understand what the variables are and why they are used.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
1 8/31/05CS150 Introduction to Computer Science 1 Hello World!
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
Lecture 3 Introduction to Computer Programming CUIT A.M. Gamundani Presentation Layout from Lecture 1 Background.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Chapter # 2 Part 2 Programs And data
What Actions Do We Have Part 1
BASIC ELEMENTS OF A COMPUTER PROGRAM
ITEC113 Algorithms and Programming Techniques
ICS103 Programming in C Lecture 3: Introduction to C (2)
Lecture2.
CMSC 104, Section 4 Richard Chang
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.
INPUT & OUTPUT scanf & printf.
Introduction to C++ Programming
Variables in C Topics Naming Variables Declaring Variables
CS150 Introduction to Computer Science 1
C++ Data Types Data Type
Variables in C Topics Naming Variables Declaring Variables
Chapter # 2 Part 2 Programs And data
CS150 Introduction to Computer Science 1
What Actions Do We Have Part 1
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:

1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have

2 09/03/04CS150 Introduction to Computer Science 1 Today  On Wednesday I showed you a C++ program that converts distances from miles to kilometers  What are the main components of that program?  Today we will o learn how C++ stores data o The different types of data that C++ can store

3 09/03/04CS150 Introduction to Computer Science 1 Declaration Statements  The declaration statements in the program are o const double KM_PER_MILE = 1.609; o double miles; o double kms;  With the above statements I am declaring three things o KM_PER_MILE to store the conversion rate that never changes o miles to store the distance in miles as given by the user o kms to store the distance in kilometers as calculated by the program

4 09/03/04CS150 Introduction to Computer Science 1 Declaration Statements  Variable declarations o Allocate space for data to be used in the program o The data can be changed o double miles; o double kms;  Constant declaration o Allocate space for data that cannot be changed o const double KM_PER_MILE = 1.609;

5 09/03/04CS150 Introduction to Computer Science 1 Variable Declaration  Variables are declared by stating o Type of data (data type) o Name to identify the variable (identifier) o Semicolon (;) o data-type identifier; o double miles;

6 09/03/04CS150 Introduction to Computer Science 1 Variable Declaration  If there is more than one variable of a single data type then you o State the data type o List the variable identifiers (names) separated by commas o Semicolon o data-type identifier1, identifier2; o double miles, kms;

7 09/03/04CS150 Introduction to Computer Science 1 Data types and Identifiers  Data types o C++ can store many different types of data o A data type also defines what operations can be performed on data of that type o We will start with the three primitive data types  int (whole numbers)  double (real numbers)  char (characters) o These data types must be in lower case  Identifiers o Valid variable names in C++

8 09/03/04CS150 Introduction to Computer Science 1 int  The int data type is used to store whole numbers, both positive and negative  int ’s are finite (why?), i.e. they have a limited range that is implementation dependent  int is short for integer  Examples of int ’s are: 123, -23, 0, 2352  An int without a sign (+ or - ) is assumed to be positive  2,353 is not an int, 2353 is an int  What operations can be performed on integers?

9 09/03/04CS150 Introduction to Computer Science 1 double  The double data type is used to store real numbers, both positive and negative  Real numbers can contain fractional parts  double ’s are finite  Examples of double ’s are: 1.0, -2.3, -.3, 12E5, -1E-2  A double without a sign (+ or - ) is assumed to be positive  2, is not a double, is a double

10 09/03/04CS150 Introduction to Computer Science 1 char  The char data type is used to store single characters (letters, digits, special characters)  char values are enclosed in single quotes  Examples of char ’s are: ‘A’, ‘a’, ‘*’, ‘2’, ‘$’

11 09/03/04CS150 Introduction to Computer Science 1 Examples  Remember, the format for declaring variables is: o data-type identifier;  You can declare variables of the different data types as follows o int num1; o double num2; o char letter ;

12 09/03/04CS150 Introduction to Computer Science 1 Identifiers  C++ does place limits on what names you can call your variables  Rules o Identifiers must begin with a letter or an underscore o Identifiers must consist of letters, numbers and underscore, nothing else o Identifiers cannot be a reserved keyword

13 09/03/04CS150 Introduction to Computer Science 1 Reserved Keywords  These are words that are reserved by C++ to implement various features  Examples of keywords that we have seen so far are int, double, const, return  A list of C++ keywords can be found on page 75 of your textbook

14 09/03/04CS150 Introduction to Computer Science 1 Identifiers  Identifiers are case sensitive o int num1; o int Num1; o num1 and Num1 are different variables  You should always try to use meaningful variable names  If you have a variable that represents the width, then call it width not w

15 09/03/04CS150 Introduction to Computer Science 1 Identifiers  Which of the following declarations are invalid and why? o char Letter1; o char 1letter; o double inches, kms; o double inches*num; o int joe’s; o Int cent_per_inch; o double two-dimensional; o char hello; o int return; o size int;

16 09/03/04CS150 Introduction to Computer Science 1 Variable Declarations  All the variable declarations that we have seen are of the form o data-type identifier;  This form declares a variable of the specific type, gives it the specific name (identifier) and allocates the space in memory to store the value of this variable  However, no value has been assigned to this variable as yet

17 09/03/04CS150 Introduction to Computer Science 1 Variable Declarations  When declaring multiple variables of the same type, it is preferable to place each variable declaration on a line along with a comment specifying the use of the variable o double miles; // Distance in miles from user o double kms; // Distance in kms

18 09/03/04CS150 Introduction to Computer Science 1 Constant Declarations  Constants are declared by stating o const o Type of data (data type) o Name to identify the variable (identifier) o = o Value assigned to this constant that will never change o Semicolon (;) o const data-type identifier = value; o const double KM_PER_MILE = 1.609;

19 09/03/04CS150 Introduction to Computer Science 1 Constant Declarations  Constant values never change o KM_PER_MILE will always be  In C++ we typically place constant declarations before any other declarations in the program

20 09/03/04CS150 Introduction to Computer Science 1 Example  Can you spot what is incorrect in the following program: int main() { const int pi = 3.14; double num; int i,j; num = e2; i = 4,000; ch = “b”; j = i; pi = 5; return 0; }

21 09/03/04CS150 Introduction to Computer Science 1 Problem  The problem specified at the end of class on Wednesday required us to write a program to calculate the area of a circle.  What constant declarations does our program need?  What variable declarations does out program need?

22 09/03/04CS150 Introduction to Computer Science 1 Summary  In today’s lecture we discovered o How Data that is used by a program can be declared and stored o The difference between constants and variables o What constitute valid identifier names o The three primitive data types; int, double, char  We have covered p of your textbook