COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick The Programming Process rUse an editor to create a program file (source file). l contains the text of.
Advertisements

C++ Basics Variables, Identifiers, Assignments, Input/Output.
Dale/Weems/Headington
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Structure of a C program
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
Data Types.
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.
KEAN UNIVERSITY Visual C++ Dr. K. Shahrabi. Developer studio Is a self-contain environment for creating, compiling, linking and testing windows program.
CS 192 Lecture 3 Winter 2003 December 5, 2003 Dr. Shafay Shamail.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
CS2311 Computer Programming Dr. Yang, Qingxiong (with slides borrowed from Dr. Xu, Henry) Lecture 2: Basic Syntax – Part I: Variables and Constants.
C Tokens Identifiers Keywords Constants Operators Special symbols.
C-Language Keywords(C99)
 Programming Languages  First Generation Languages (Machine Language)  We Actually have to do a few things. First we have to find the operating code,
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
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.
Week 1 Algorithmization and Programming Languages.
C++ Programming, Namiq Sultan1 Chapter 2 Introduction to C++ Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
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.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Data Types, Primitive Types in C++, Variables – Declaration, Initialization, Scope Telerik Software Academy academy.telerik.com Learning and Development.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
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.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
Basic Types, Variables, Literals, Constants. What is in a Word? A byte is the basic addressable unit of memory in RAM Typically it is 8 bits (octet)
C++ Lesson 1.
Asst.Prof.Dr. Tayfun ÖZGÜR
Variables, Identifiers, Assignments, Input/Output
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Data types Data types Basic types
BASIC ELEMENTS OF A COMPUTER PROGRAM
LESSON 3 IO, Variables and Operators
University of Central Florida COP 3330 Object Oriented Programming
Compiler Construction
Computing with C# and the .NET Framework
CSE101-Lec#3 Components of C Identifiers and Keywords Data types.
بسم الله الرحمن الرحيم.
Reserved Words.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
C++ Basics.
2.1 Parts of a C++ Program.
درس برنامه‌سازي کامپيوتر
Basics of ‘C’.
Introduction to C Programming
Keywords.
Govt. Polytechnic,Dhangar
Variables, Identifiers, Assignments, Input/Output
Prof. Bhushan Trivedi Director GLS Institute of Computer Technology
Variables in C Declaring , Naming, and Using Variables.
Chapter 2: Introduction to C++.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Programming Language C Language.
Chap 2. Identifiers, Keywords, and Types
C Language B. DHIVYA 17PCA140 II MCA.
Presentation transcript:

COMPUTER PROGRAMMING

Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program. The result is “ hello world!” on the screen. In order to write programs that perform useful tasks that really save us work we need to introduce the concept of variable.

variable Thinking…. Retain the number 8 in your memory. Also memorize the number 3 in your memory as well. You have just stored two different values in your memory. Add 2 to the first number. You should have 10 which is (8+2) Subtract the second number from the result. What is the new result?

variable It is similar to what a computer can do with two variables. The same process can be expressed in C++ with the following instruction: a = 8; b = 3; a = a + 2; result = a - b;

variable this is a very simple example since we have only used two small integer values. consider that your computer can store millions of numbers like these at the same time and conduct sophisticated mathematical operations with them. There is a need to a variable. What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes it from the others.

Identifiers for example, in the previous code the variable identifiers were a, b and result. we could have called the variables any names we wanted to invent, as long as they were valid identifiers. What is an Identifier? A valid identifier is a sequence of one or more letters, digits or underscore characters (_).

Identifiers Neither spaces Nor punctuation marks or symbols can be part of an identifier. Only letters, digits and single underscore characters are valid. variable identifiers always have to begin with a letter. They can also begin with an underline character (_ ). but in some cases these may be reserved for compiler specific keywords or external identifiers. no identifiers containing two successive underscore characters. In no case they can begin with a digit.

reserved keywords Another rule that you have to consider when inventing your own identifiers is that they cannot match any keyword of the C++ language nor your compiler's specific ones, which are reserved keywords. The standard reserved keywords are: asm, auto, bool, break, case, catch, char, class, const, const_cast, continue, default, delete, do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto, if, inline, int, long, mutable, namespace, new, operator, private, protected, public, register, reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct, switch, template, this, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t, while

reserved keywords alternative representations for some operators cannot be used as identifiers since they are reserved words under some circumstances: and, and_eq, bitand, bitor, compl, not, not_eq, or, or_eq, xor, xor_eq

Notes: The C++ language is a "case sensitive" language. That means an identifier written in capital letters is not equivalent to another one with the same name but written in small letters. for example, the RESULT variable is not the same as the result variable or the Result variable. These are three different variable identifiers.

Fundamental data types the variables are stored in our computer's memory, but the computer has to know what kind of data we want to store in them. Why ? The computer is not going to reserve the same amount of memory to store a simple number as to store a single letter or a large number. memory in our computers is organized in bytes. A byte is the minimum amount of memory that we can manage in C++. A byte can store a relatively small amount of data: one single character or a small integer (generally an integer between 0 and 255).

The basic fundamental data types in C++ NameDescriptionSizeRange charCharacter or small integer. 1bytesigned: -128 to 127 unsigned: 0 to 255 short int (short) Short Integer.2bytessigned: to unsigned: 0 to intInteger.4bytessigned: to unsigned: 0 to long int (long)Long integer.4bytessigned: to unsigned: 0 to boolBoolean value. (true or false). 1byte true or false doubleDouble floating point number. 8bytes+/- 1.7e +/- 308 (~15 digits) floatFloating point number 4bytes+/- 3.4e +/- 38 (~7 digits)

Declaration of variables In order to use a variable in C++, we must first declare it specifying which data type we want it to be. The syntax to declare a new variable is to write the specifier of the desired data type (like int, bool, float...) followed by a valid variable identifier. For example: These are two valid declarations of variables. The first one declares a variable of type int with the identifier a. The second one declares a variable of type float with the identifier mynumber. Once declared, the variables a and mynumber can be used within the rest of their scope in the program. int a; float mynumber;

Declaration of variables integer data types char, short, long and int can be either signed or unsigned depending on the range of numbers needed to be represented. Signed types can represent both positive and negative values. unsigned types can only represent positive values (and zero). Example: signed int MyAccountBalance; unsigned int MyAccountBalance; By default, if we do not specify either signed or unsigned most compiler settings will assume the type to be signed,

The C++ code of the example about your memory // operating with variables #include using namespace std; int main () { // declaring variables: int a, b; int result; // process: a = 8; b = 3; a = a + 1; result = a - b; // print out the result: cout << result; // terminate the program return 0; } 7

Scope of variables All the variables that we intend to use in a program must have been declared in an earlier point in the code. A variable can be either of global or local scope. A global variable is a variable declared in the main body of the source code. a local variable is one declared within the body of a function or a block.

Scope of variables #include using namespace std; int result; int main () { int a, b; a = 8; b = 3; a = a + 1; result = a - b; cout << result; return 0; } Global variable Local variables

Initialization Global variables can be referred from anywhere in the code, even inside functions, whenever it is after its declaration. The scope of local variables is limited to the block enclosed in braces ({}) where they are declared. How to initialize a variable? first way: type identifier = initial_value ; example: ( int b=7;). second way (constructor initialization): type identifier (initial_value) ; example: (int a (0);).