CHAPTER # 2 Part 2 PROGRAMS AND DATA 1 st semster 1436 1 King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.

Slides:



Advertisements
Similar presentations
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
Advertisements

Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
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
CMT Programming Software Applications
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
Variables, Constants and Built-in Data Types Chapter 2: Java Fundamentals.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
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.
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Input & Output: Console
Chapter 2 part #4 Operator
INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in The.
Lecture 2 Introduction to Computer Programming CUIT A.M. Gamundani Presentation Layout Data types.
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CPS120: Introduction to Computer Science
1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
Week 1 Algorithmization and Programming Languages.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Chapter 2 part #1 C++ Program Structure
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
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.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Lecture 3 Introduction to Computer Programming CUIT A.M. Gamundani Presentation Layout from Lecture 1 Background.
CPS120: Introduction to Computer Science Variables and Constants.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
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.
Chapter 2. Variable and Data type
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 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 PART #4 OPERATOR 1 st semester King Saud University College of Applied studies and Community Service Csc
Fundamentals 2 1. Programs and Data Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in.
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 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
BASIC ELEMENTS OF A COMPUTER PROGRAM
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++.
Lecture2.
Basic Elements of C++ Chapter 2.
IDENTIFIERS CSC 111.
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.
Fundamentals 2.
C++ Data Types Data Type
Chapter 2: Java Fundamentals
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Chapter # 2 Part 2 Programs And data
Chapter 2: Java Fundamentals
Chapter 2: Introduction to C++.
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Variables and Constants
Programming Fundamental-1
Presentation transcript:

CHAPTER # 2 Part 2 PROGRAMS AND DATA 1 st semster King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi Updated By: Ghadah Alhadbaa,Fatimah Alakeel, Nouf Almunyif

C++ Program Structure Page 2 #include // Preprocessor Commands using namespace std; int main( ) // main function { // Declaration section – Declare needed variables …... // Input section – Enter required data ….. // Processing section – Processing Statements …... // Output section – Display expected results ….... return 0; }// end main The part of a program that tells the compiler the names of memory cells in a program

Programs and Data Page 3  Most programs require a temporary storage for the data to be processed inside the computer.  Thus, a computer's memory is used by allocating different locations on it to hold these data  Each allocated location has three characteristics 1. State 2. Identifier 3. Data Type KeyboardScreen Processing input data output data

1.State Page 4  The state of a memory location is the current value (data) stored on it.  The state of a memory location: May be changed. In this case, this memory location is called variable. Cannot be changed. In this case, this memory location is called constant.

2.Identifiers  Identifiers are names for the entities (such as variables, constants, and functions) we create in our programs  In C++, These names, or identifiers, are required to conform to some rules. 5

2.Identifiers : Rules Page 6  It cannot begin with a digit (0 – 9).  After the firs letter it may contains: A letters a to z, A to Z, OR A digits 0 to 9, OR the underscore symbol, _ OR A combination of all or some of the above  No spaces or punctuation, except the underscore symbol, are allowed.  Reserved words/keywords such as main cannot be used.

2.Identifiers : Are Case-Sensitive Page 7  Identifiers in C++ are case-sensitive. Thus, the identifiers myNumber and mynumber, are seen as two different identifiers by the compiler.

According to the previous identifiers rules, state which if the following identifiers are right and which are wrong. letter2 21etter letter_2 int joe's variable cent_per_inch Begins with a digit Reserved word Character ’ not allowed 2.Identifiers : Examples 8

Keywords  Keywords (also called reserved words)  Are used by the C++ language  Must be used as they are defined in the programming language 9

3.Data Type  In Programming languages, a data type is a classification identifying one of various types of data  A data type determines:  the possible values for that type;  the operations that can be done on values of that type;  the meaning of the data;  the way values of that type can be stored; and  the size of allocated location(s) Page 10

Page 11  The int data type represents integers.  Integers are whole numbers.  Examples:    -35  int

Page 12  The float data type represents real numbers.  A real number has an integral part and a fractional part separated by a decimal point.  Examples:     3.4 float

Page 13  The char data type represents one individual character.  Examples:  'D'  'd‘  '5‘  '*' char

3.Data Type  The exact range of values for the fundamental types are implementation dependent; you should check your compiler documentation for the actual ranges supported by your computer  Table 3.2a, 3.b lists C++ fundamental data type along with their common bit lengths and ranges for 16-bit environment and 32-bit environment consecutively Page 14

15 Typekeyword size (byte)range characterchar1-128 to 127 Signed characterSigned char1-128 to 127 Unsigned characterunsigned char10 to 255 Integerint to Short Integershort int to Long integerlong int4-2,147,483,648 to +2,147,483,647 Unsigned integerunsigned int20 to Unsigned short integerunsigned short int20 to Unsigned Long integerunsigned long int40 to 4,294,967,295 float 4 3.4E–38 to 3.4E+38 double 8 1.7E–308 to 1.7E+308 Long double 103.4E-4932 to 1.1E+4932 Boolean boolN/ATrue or false Table 3.2 a: For 16-bit environment

16 Typekeyword size (byte)range characterchar1-128 to 127 Signed characterSigned char1-128 to 127 Unsigned characterunsigned char10 to 255 Integerint4-2,147,483,648 to +2,147,483,647 Short Integershort int to Long integerlong int4-2,147,483,648 to +2,147,483,647 Unsigned integerunsigned int40 to 4,294,967,295 Unsigned short integerunsigned short int20 to Unsigned Long integerunsigned long int40 to 4,294,967,295 float 4 3.4E–38 to 3.4E+38 double 8 1.7E–308 to 1.7E+308 Long double 103.4E-4932 to 1.1E+4932 Boolean boolN/ATrue or false Table 3.2 b: for 32-bit environment

Page 17  When the declaration is made, memory space is allocated to store the values of the declared variable or constant.  The declaration of a variable means allocating a space memory which state (value) may change.  The declaration of a constant means allocating a space memory which state (value) cannot change. Variable/Constant Declaration

Declaring Variables: Syntax Declaration syntax: DataType Variable_1[= literal | expression] [, Variable_2,...] ;  For declaring a variable you must at least specify  Variable data type  Variable name  During declaration statement, optionally,you may :  Give the variable an initial value, and/or  Declare several variable in one declaration statement, separated by comma (if thy have thy same data type) 18

Declaring Variables: Examples Examples: int number_of_bars; double one_weight, total_weight; int num=25; int age, num_students; char letter = ‘x’ 19

Declaring Variables: Declaration Location 1- Immediately prior to use int main() { ………. int sum; sum = score1 + score 2; ……….. return 0;} 2- At the beginning int main() { int sum; ….. sum = score1 +score2; return 0; } Two locations for variable declarations 20

Declaring Variables: Initial Value Page 21  A variable may be declared:  With initial value.  Without initial value.  Variable declaration with initial value; dataType variableIdentifier = literal | expression; double avg = 0.0; int i = 1; int x =5, y = 7, z = (x+y)*3;  Variable declaration without initial value; dataType variableIdentifier; double avg; int i;

Assignment Operator  We assign a value to a variable using the basic assignment operator (=).  Assignment operator:  Stores a value in a memory.  Basically used in C++ to initialize a variable with a value OR to update it’s content with a new value  It’s syntax is as following It is always a variable identifier. It is either a literal, a variable identifier, or an expression. 22 leftSide = rightSide ; Assigning value to variable during declaration time e.g. int num=5; Assigning value to variable during declaration time e.g. int num=5; Assigning value to variable after it been declared e.g. num=6; Assigning value to variable after it been declared e.g. num=6;

 The assignment operator (=) assigns the value on the right side of the operator to the variable appearing on the left side of the operator.  The right side may be either: 1. Literal: e.g. i = 1; 2. Variable identifier: e.g. start = i; 3. Expression: e.g. sum = first + second; 23 Assignment Operator

1.Assigning Literals  In this case, the literal is stored in the memory space allocated for the variable on the left side. int firstNumber=1, secondNumber; firstNumber = 234; secondNumber = 87; A A B B firstNumber 1 1 secondNumber ??? A. A. Variables are allocated in memory. B. B. Literals are assigned to variables. firstNumber 234 secondNumber 87 Code State of Memory 24

2.Assigning Variables  In this case, the value of the variable on the right side is stored in the memory space allocated for the variable on the left side. int firstNumber=1, i; firstNumber = 234; i = firstNumber; A A B B firstNumber 1 1 i ??? A. A. Variables are allocated in memory. B. B. values are assigned to variables. firstNumber 234 i Code State of Memory 25

3.Assigning Expressions  In this case, the result of evaluating the expression (on the right side) is stored in the memory space allocated for the variable (on the left side ). int first, second, sum; first = 234; second = 87; Sum = first + second A A B B A. A. Variables are allocated in memory. B. B. Values are assigned to variables. Code State of Memory first ??? second ??? sum ??? first 234 second 87 sum

Example of Initializing and Updating Data Using Assignment Operator Code State of Memory number 100 A. 100 A. The variable is allocated in memory and initializes with the value 100 B B. The value 237 overwrites the previous value 100 int number=100; number = 237; A A B B C C number = 35; C C. The value 35 overwrites the previous value 237. number 237 number 35 27

Declaring Constants: Syntax (1) const DataType constant_Identifier= literal | expression;  const is the keyword to declare a named constant  Along with const, data type, and constant name, You must specify a value to be stored in it  Unlike variables, const value can’t be changed later.  It is common to name constants with all capital letters (i.e. no error occur when using small letters) 28

Declaring Constants: Syntax (1) Page 29  Examples const double PI = ; const int day_in_week = 7; const short int FARADAY_CONSTANT = 23060; const int MAX = 1024; const int MIN = 128; const int AVG = (MAX + MIN) / 2 ; These are constants, also called named constant. The reserved word const is used to declare constants. These are called literals. This is called expression.

Declaring Constants: Syntax (2) Page 30  The preprocessor #define is another more flexible method to define constants in a program  Syntax : #define NAME value #define WIDTH 80 #define LENGTH ( WIDTH + 10 ) what is the difference between #define and const ? The preprcessor directive #define is used to declare constants.

#define Vs. const ? #defineConst Used to store a constant value that cannot be changed during execution. A preprocessor directiveA C++ statement Does not allocate a memory area for the value. Allocates a memory area for the value. This area cannot be changed during execution. The compiler replaces every occurrence of the defined constant with its value. The compiler treats every constant as any variable but it does not change its value. 31

Character Data type 32  The char type is used to store the integer value of a member of the represent table character set. (see Figure B.1)  An example of a character value is the letter A. To declare a character variable called letter and store A in it:  char letter;  letter = 'A';  Note the assignment of the character A to the variable letter is done by enclosing the value in single quotes ‘ ’.

Character Data type  In Figure B.1  the digits at the left of the table are the left digits of the decimal equivalent (0-127) of the character code, and  The digits on the top of the table are the right digits of the decimal equivalent  Example : ‘F’ is 70, ‘&’ is 38 33

Page 34

Data Types For Numbers With Decimal Point  To declare values such as 3.4 or , use either float or double.  Which one to choose? Depending on the size of your number.  Syntax:  float c = 4.5;  double z = ; 35