Variables T.Najah Al_Subaie Kingdom of Saudi Arabia

Slides:



Advertisements
Similar presentations
Chapter 2 Part B CISS 241. Take a few moments and work with another student to develop an algorithm for a program that will add two whole numbers (integers)
Advertisements

CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
Three types of computer languages
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
Chapter 2 Data Types, Declarations, and Displays
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
Chapter 01: Introduction to Computer Programming
Introduction to C++ Programming
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
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.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
INTRODUCTION Kingdom of Saudi Arabia Princess Nora bint Abdul Rahman University College of Computer Since and Information System CS240.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
1.  By the end of this section you should: ◦ Understand what the variables are and why they are used. ◦ Use C++ built in data types to create program.
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.
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
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
CHAPTER 1: INTRODUCTION C++ Programming. CS 241 Course URL: Text Book: C++ How to Program, DETITEL & DEITEL, eighth Edition.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
LECTEURE # 5 : STRUCTURED PROGRAMMING VARIABLES, INPUT, MEMORY المتغيرات, المدخلات, الذاكرة By Mr. Ali Edan.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Chapter 2 of C++ How to Program, 10/e © by Pearson Education, Inc. All Rights Reserved.
Chapter # 2 Part 2 Programs And data
Variables, Identifiers, Assignments, Input/Output
© by Pearson Education, Inc. All Rights Reserved.
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Basics (Variables, Assignments, I/O)
Chapter 1: Introduction to computers and C++ Programming
Introduction to C++ Programming
What Actions Do We Have Part 1
Introduction to C++ Programming
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Computing Fundamentals
Basic Elements of C++.
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
IDENTIFIERS CSC 111.
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
Basics (Variables, Assignments, I/O)
INPUT & OUTPUT scanf & printf.
Introduction to C++ Programming
1.13 The Key Software Trend: Object Technology
Variables Kingdom of Saudi Arabia
Variables, Identifiers, Assignments, Input/Output
Introduction to C++ Programming
Chapter # 2 Part 2 Programs And data
Chapter 2: Introduction to C++.
C++ Programming Lecture 3 C++ Basics – Part I
Programs written in C and C++ can run on many different computers
What Actions Do We Have Part 1
Capitolo 1 – Introduction C++ Programming
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

Variables T.Najah Al_Subaie Kingdom of Saudi Arabia Prince Norah bint Abdul Rahman University College of Computer Since and Information System CS240 Variables T.Najah Al_Subaie

Objectives: By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program variables. Apply C++ syntax rules to declare variables, initialize them, make an assignments operations. Understand memory allocation process. Apply C++ syntax rules to read user input using cin.

Do you remember this ?? Write a program that displays the sum of two integers entered at the keyboard: Define the problem precisely. Write the psudocode that will solve the problem Use an Editor to create source code in C++. Use the compiler to Check that source code obeys the language rules. If no errors: Execute your program.

Variables In order to define places in memory where the three values will be stored, we need to defined three variables for number1, number2 and sum.

Variables Variable: Name(Identifier) Type: Location on computer’s memory to store data then use and change its value in a program. Name(Identifier) Series of letters, digits, underscores Not a keyword( int, float, double char, void, return main) Start with a letter Case sensitive Meaningful Type: Programmer defined Built-in

What is a data type? When we wish to store data in a C++ program, we have to tell the compiler which type of data we want to store. The data type will have characteristics such as: The range of values that can be stored. and the operations that can be performed on variables of that type.

C++ Built-in Data Types Called fundamental types or primitives types: Numerical (integer and floating point) Character Logical (Boolean)

C++ Built-in Data Types size Type 1 byte Bool 2 bytes Unsigned short int Short int 4 bytes Unsigned long int Long int Int char float 8 bytes double

Data Types (Cont.)

bool Data type Has two values (true) and (false). Manipulate logical expressions. true and false are called logical values. bool, ture, and fasle are reserved words. For example: bool isEven = false; bool keyFound = true;

char Data Type Used for characters: letters, digits, and special symbols. Each character is enclosed in single quotes. Some of the values belonging to char data type are: ‘A’,’a’,’0’,’*’,’+’,’$’,’&’. A blank space is a character and is written ‘ ‘, with a space left between the single quotes.

int Data Type The integer type is used for storing whole numbers. We can use signed, unsigned or plain integer values as follows: signed int index = 4182; signed int temperature = -32; unsigned int count = 0; int height = 100; int balance = -67;

Floating-Point Types Floating point types can contain decimal numbers. Examples: 1.23, -.087. There are three sizes: float (single-precision) double (double-precision) and long double (extended-precision). Examples: float Temp= 37.623; double fahrenheit = 98.415; long double accountBalance = 1897.23;

Variable declaration All variables must be declared anywhere in program with a name and data type before they used. Begin with a data type then variable name. Variables of the same type can be declared in Multiple lines One line separated by commas. Data type VarName; int num1; int num2; int num3; int num1,num2,num3;

Initializing Variables Variables can be initialized when declared: int first=13, second=10; char ch= ‘ ‘; double x=12.6, y=123.456; first and second are int variables with the values 13 and 10 respectively. ch is a char variable whose value is empty. x and y are double variables with 12.6 and 123.456 respectively.

Memory Concepts Variable names such as : number1, number2 and sum correspond to locations in the computer’s memory. Every variable has four parts: Type, name, size and value. Example: char letter=‘ A’; Type? Name? Size? Value?

Memory Concepts Placing new value into variable (memory location), overwrites old value- called destructive. Reading value of variable in memory-called nondestructive.

Memory Concepts int number1; int number2; int sum; number1 number2 sum

Memory Concepts: After executing these instructions cin>>number1; // assume user entered 45 cin>>number2; // assume user entered 72 sum=number1+number2; After executing these instructions 45 number1 72 number2 117 sum

Using cin Namespace: std:: Standard input stream object. Specifies using a name that belong to “namespace” std Can be removed through use of using statement. Standard input stream object. std::cin Connected to keyboard Defined in input/output stream library <iostream>

Using cin Stream extraction operator >> Value to left (left operand) inserted into right operand. Waits for user to input value then press enter key Examples: cin>>num1; Inserts the standard input from keyboard into the variable num1. Prints message before cin statement to direct the user to take a specification called prompt. cin and cout facilitate interaction between user and program.

Example: //variable declaration int number1; //first integer to add //Fig. 2.5: fig02_05.cpp //addition program that displays the sum of two integers #include<iostream> //allow program to perform input and output //function main begins program execution int main(){ //variable declaration int number1; //first integer to add int number2; //second integer to add int sum; //sum of number1 and number2

Example (Cont.) cout<< “Enter first integer: “; //prompt user for data cin>> number1;//read first integer from user into mumber1 cout<< “Enter second integer: “;//prompt user for data cin>> number2; ;//read first integer from user into mumber1 sum=number1+number2; //add the numbers ; store result in sum cout<< “Sum is “ << sum << endl; //display sum; end line return 0; } //end function main

Examples: #include <stream> int main { cout << "If this text", cout >> " appears on your display, "; cout << " endl;" cout << 'you can pat yourself on ' << " the back!" << endl. return 0; ) In the above program, find the errors (if any) then, give the program output.