LECTEURE # 5 : STRUCTURED PROGRAMMING VARIABLES, INPUT, MEMORY المتغيرات, المدخلات, الذاكرة By Mr. Ali Edan.

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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 1 – Introduction to Computers and C++ Programming Outline 1.6 Machine Languages, Assembly Languages,
Introduction Kingdom of Saudi Arabia Shaqra University
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline History of C and C++ C++ Standard Library Object Technology Basics.
 2008 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Machine Languages, Assembly Languages, and High-level Languages Three types of computer languages 1.Machine.
Chapter 2: Introduction to C++.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
Introduction to C++ Programming
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
CHAPTER 2 BASIC ELEMENTS OF C++. In this chapter, you will:  Become familiar with the basic components of a C++ program, including functions, special.
Chapter 01: Introduction to Computer Programming
Introduction to C++ Programming
COMPUTER SCIENCE I C++ INTRODUCTION
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.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 3: Requirements Specification, C++ Basics.
Chapter 02 (Part III) Introduction to C++ Programming.
 2008 Pearson Education, Inc. All rights reserved. 1 CISC 1600 – Computer Science I Fall 2010 Introduction to C++ Programming Chapters 1 and 2 (Deitel.
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++.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
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.
C++ How to Program, Late Objects Version, 7/e © by Pearson Education, Inc. All Rights Reserved.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
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
Announcements Starting next week class 6-8 on Thursday Homework 1 on the web  Due January 29 – next class meeting  Homework policy No late assignments.
 2006 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING Dr. Shady Yehia Elmashad.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
 2008 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
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 02 (Part II) Introduction to C++ Programming.
 2006 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Chapter 2 of C++ How to Program, 10/e © by Pearson Education, Inc. All Rights Reserved.
© by Pearson Education, Inc. All Rights Reserved.
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1: Introduction to computers and C++ Programming
Introduction to C++ Programming
Introduction to C++ Programming
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Introduction to C++ Systems Programming.
Computing Fundamentals
Basic Elements of C++.
Introduction to C++ Programming
Basic Elements of C++ Chapter 2.
Chapter 2 Elementary Programming
Introduction to C++ Programming
1.13 The Key Software Trend: Object Technology
Variables Kingdom of Saudi Arabia
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Introduction to C++ Programming
Chapter 2: Introduction to C++.
Programs written in C and C++ can run on many different computers
Capitolo 1 – Introduction C++ Programming
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

LECTEURE # 5 : STRUCTURED PROGRAMMING VARIABLES, INPUT, MEMORY المتغيرات, المدخلات, الذاكرة By Mr. Ali Edan

Content 2  Variables المتغيرات  C++ Built in data types انواع البيانات والمتغيرات  C++ variable declaration syntax طريقة تعريف المتغيرات  cin statement امر ادخال البيانات  Local and global variables المتغير المحلي والمتغير العمومي  Scope resolution operator (::) الاداة  Variables and RAM المتغيرات والذاكرة  Defining constants الثوابت

Variable  Location on computer’s memory to store data then use and change its value in a program  جزء من الذاكرة لخزن نوع محدد من البيانات  Each variable has كل متغير لدية 1. Name (identifier) اسم Series of letters, digits, underscores Not a keyword Start with a letter Case sensitive Meaningful 2. Type نوع Programmer-defined احد انواع البيانات 3

Determine which of the following variables name is valid or invalid : _under_bar_z2h22 2h67h2his_account_total t587m her_sales3gj7 abc 4

C++ Built-in Data Types  Called fundamental types or primitives types: numeric, character, logical الانواع الاساسية : ارقام, حرفي, منطقية 5

الانواع المنطقية bool Data Type  Has two values, true and false  قيمتين صح و خطأ  Manipulate logical (Boolean) expressions  تستخدم في المعادلات المنطقية  true and false are called logical values  قيم هذا المتغيرات هي true & false  bool, true, and false are reserved words  الكلمات المحجوزة هي true,false, bool 6

نوع char Data Type  Used for characters  letters, digits, and special symbols  حروف, رموز, رموز خاصة  Each character is enclosed in single quotes  Examples: 'A', 'a', '0', '*', '+', '$', '&'  A blank space is a character and is written ' ' with a space left between the single quotes 7

Declaring Variables  All variables must be declared anywhere in program with a name and data type before they used  كل انواع المتغيرات يحب ان تعرف قبل ان تستخدم  Syntax rule: begin with a data type then variable name  النوع يكتب قبل اسم المتغير  Variables of the same type can be declared in  Multiple lines  One line separated by commas 8 int num1; int num2; int num3; int num1, num2, num3; int num1; int num2; int num3; int num1, num2, num3; dataType varName;

اعطاء قيم للمتغيرات Initializing Variable  Variables can be initialized once declared  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 , respectively 9 int first=13, second=10; char ch=' '; double x=12.6, y= ; int first=13, second=10; char ch=' '; double x=12.6, y= ;

Using cin  Namespace  std:: Specifies using a name that belongs to “namespace” std Can be removed through use of using statements  Standard output stream object  std::cin “Connected” to keyboard Defined in input/output stream header file 10

Using cin (cont.)  Stream extraction operator >>  Value to left (left operand) inserted into right operand  Waits for user to input value then press Enter key  Example std::cin >> num1; Inserts the standard input from keyboard into 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 11

12 Enter first integer 45 Enter second integer 72 Sum is // Fig. 2.5: fig02_05.cpp 2 // Addition program that display the sum of two numbers. 3 #include // allow program to perform input and output 4 5 // function main begins program execution 6 int main() 7 { 8 // variable declaration 9int number1; // first integer to add 10 int number2; // second integer to add 11 int sum; // sum of number1 and number std::cout << "Enter first integer: \n"; // prompt user for data 14 std::cin >> number1; // read first integer from user to number std::cout << "Enter second integer: \n"; // prompt user for data 17 std::cin >> number2; // read second integer from user to number sum = number1 + number2; // add the numbers; stor result in sum std::cout << "Sum is " << sum << std::endl; // display sum; end line return 0; // indicate that program ended successfully 24 } // end function main Declare integer variables.Use stream extraction operator with standard input stream to obtain user input. Stream manipulator std::endl outputs a newline, then “flushes output buffer.” Concatenating, chaining or cascading stream insertion operations. Calculations can be performed in output statements: alternative for lines 19 and 21: std::cout << "Sum is " << number1 + number2 << std::endl;

Variable Scope  Portion of the program where the variable can be used  Scope can be  Local  Global 13

Local Variables  Defined within a module  Can be seen and used only by module itself  Store temporally in memory  Erased when the module terminates 14 int main() { int i; char a; return 0; } int main() { int i; char a; return 0; }

Global Variables  Defined outside any module  Used and seen by all modules  Variable name can be duplicated within and outside a modules  Differentiate between them by using unary scope resolution operator (::) 15 int i; int main() { char a; return 0; } int i; int main() { char a; return 0; }

Exercise Write a program that declares two constant A and B 2. Initialize A =1 and B= Declare an int named C and float named D 4. Initialize C =A and D=B 5. Write statements to print C and D to screen

End 17