ITEC113 Algorithms and Programming Techniques

Slides:



Advertisements
Similar presentations
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Advertisements

Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Javascript Essentials How do I write it??  Start Homesite  Between the start and end BODY tags type: 
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
Data types and variables
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Chapter 2 Data Types, Declarations, and Displays
String Escape Sequences
Basic Elements of C++ Chapter 2.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
Variables and Data Types
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?
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
ITEC113Algorithms and Programming Techniques C programming: Variables, Expressions Part I.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
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.
COMP Primitive and Class Types Yi Hong May 14, 2015.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
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.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 2: Basic Elements of C++
ITEC113 Algorithms and Programming Techniques
Chapter Topics The Basics of a C++ Program Data Types
Variables Mr. Crone.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Tokens in C Keywords Identifiers Constants
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Chapter 2: Problem Solving Using C++
Basic Elements of C++.
Data Types, Identifiers, and Expressions
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Multiple variables can be created in one declaration
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2: Introduction to C++
C++ for Engineers and Scientists Second Edition
Basic Elements of C++ Chapter 2.
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.
Data Types, Identifiers, and Expressions
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
2.1 Parts of a C++ Program.
Chapter 2: Basic Elements of Java
Chapter 2 Variables.
Lectures on Numerical Methods
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Chapter 2: Introduction to C++.
Primitive Types and Expressions
Variables and Constants
Programming Fundamental-1
Presentation transcript:

ITEC113 Algorithms and Programming Techniques C programming: Variables, Expressions Part I

Objectives To understand what variables are initialization/garbage values naming conventions To learn about the frequently used data types To understand the components of an assignment Statements arithmetic expressions To learn about frequently used operators, operator precedence

VARIABLES int x; char gender; float avg; float sum=0; char name[10]; Variables are basic data objects manipulated in a program. Each variable has to be declared before use. Each variable has a name and a data type. You can give initial value (variable initialization) on variable declaration. Examples: int x; char gender; float avg; float sum=0; char name[10]; int *fp;

VARIABLES Variable declaration allocates a cell in the main memory whose size is determined by the data type For example for int 4 bytes are used, for double 8 bytes are used When the variable is created in the main memory it contains garbage value This is due to the existence of 1’s and 0’s in the memory. 1 means high voltage, 0 means low voltage. It is a good idea to initialize variables before first usage. A variable name is the symbolic representation of the memory location that is allocated on variable declaration

Rules on Variable Names: DO NOT use reserved words as variable names (e.g. if, else, int, float, case, for, …). The first character has to be a letter or underscore. It can not be a numeric digit. The second and the other characters of the name can be any letter, any number, or an underscore “_”. Examples  Some valid names:   my_name, m113_1, salary, bluemoon , _at   Some invalid names: my name, my-name , 1stmonth , salary! , guns&roses ,

Tradition on Variable Names: These are NOT rules but you can increase the quality of your program by using them! Select related and meaningful names indicating tasks of the variables. Do not use variable names that exceed 8 characters. Use small case letters for variable names.  Upper case letters are mostly used in the names of symbolic constants.

Variable Declaration: Variable declaration is used to introduce the system to the variables that the programmer decides to use on the rest of the program. On variable declaration, variable name, data type are declared. Also you can give the initial value of the variable on its declaration.   Example : int k ; int m=15; float fnumber= 1.75; char ch=’w’ ;

Data Types of the Variables : A variable data type specifies: The kind of value a variable can store The set of operations that can be applied to the variable   There are 3 main different data types and their derivations for declaration in ANSI–C.    Main Data types Derived Data Types integer short, long float Double char

Data Types and Sizes : (Continued) Integers (int) : Integers are all numeric values that have no fractional or decimal components. Integer numbers may be positive or negative. Examples : 13 7 –6 208 1024 C compiler allocates 4 bytes (32 bits) to an integer (int) variable. An integer variable can store values in the range –32,768 through 32,767 Derived Integers : short, long and unsigned are data types derived from int, and used to keep integer values. The sizes of long and short is differentiated from int. The data type unsigned is used only with positive integers.

Data Types and Sizes : (Continued) The sizes of long and short is differentiated from int. The data type unsigned is used only with positive integers. Data Types Bytes Used int 4 Bytes short 2 Bytes double 8 Bytes unsigned

Data Types and Sizes : (Continued) Real Numbers : C compiler uses float and double data types for storing real numbers. The float data type requires 4 bytes and has a precision of seven digits This means after the decimal point you can have seven digits Example: 3.14159 534.322344 0.3333333 0.1234567 The double data type requires 8 bytes and has a precision of fifteen digits Example : -3738.7878787878 3.141592653589790 0.123456789123456 

Data Types and Sizes : (Continued) We can use Scientific Notation to represent real numbers that are very large or very small in value. The letters e or E is used to represent times 10 to the power. Example: 1.23 x 10 5 is represented in C as 1.23e5 or 1.23e+5 or 1.23E5 1 x 10 -9 is represented in C as 1e-9

Data Types and Sizes : (Continued) Character : ( char ) Characters constants are usually used enclosed single quotes Example: ‘A’ , ‘7’, Only one byte of memory location is used by a character variable. In ASCII code is used to represent uniquely any one of the available 255 characters Example: A is represented by decimal 65 or 8-bit binary 0 1 0 0 0 0 0 1

Data Types and Sizes : (Continued) Categories of characters : Alphabetic Letters : ( Upper case : ‘A’ , ‘B’, …….. ‘Z’ ) ( Lower case : ‘a’ , ‘b’, …….. ‘z’ ) Numeric digits ( ‘1’,’2’,’3’,…………,’9’,’0’ ) Special Characters ( blank, ‘+’,’#’,’-‘,’_’,……..) Control Characters ( ‘\n’ , ‘\t’ , ……. )

Assignment Statements The ‘=‘ sign is an assignment operator. Assignment statements replace old values of the variables with the new ones An assignment statement assigns a value or a computational result to a variable. Example cnt = 1; sum = 0; ch = ‘Y’;   sum = sum + 1; avg = sum / cnt; stores values 1 and 0 to cnt and sum. stores character ‘Y’ to ch stores computational results to sum and avg

Expressions Arithmetic Expressions involve arithmetic operators such as *,+,-,/,%: Example : a * 5 + b % 4 Relational Expressions involve relational operators that compare two values such as >,<,== etc: Example: a > b Logical Expressions involve the logical and and or operators && and || and are used to combine relational expressions: Example: ( a > b && c == 7 )

Arithmetic Expressions In the Assignment Statement: M = a * 5 + b % 4 ; The expression to the right of the assignment operator ( = ) involves an arithmetic operation that combines arithmetic operands with arithmetic operators. The most commonly used arithmetic operators are: Addition (+) Operator Subtraction (-) Operator multiplication (*) Operator division (/) Operator remainder (%) Operator For real or integer numbers For integer numbers only

Operator Precedence Rules Arithmetic expressions inside parentheses are executed first (left to right). Unary operators ( minus signs and plus signs) are executed before multiplications, divisions and remainder operations. Additions and subtractions are executed last. Operators Associativity Priority Level ( , ) Left to Right Highest + , - ( unary ) Right to Left * , / , % + , - Lowest parentheses -ve and +ve signs Mult. Div., and mod. Add and subtract

Operator Precedence Rules:Examples ? =3 + 5* 4 Evaluated as 3 + (5*4) and the result is 23 ? = 8 / (5 – 2) Evaluated as 8 / 3 and the result is 2 ? = 8 + 12 % 5 Evaluated as 8 + (12%5) and the result is 10 ? = 6 * 5 / 2 + 2 Evaluated as ( (6*5) /2) + 2 and the result is 17 ? = 9 – 4 + 2 * 6 Evaluated as 9 – 4 + (2*6) and the result is 17 ? = 1 + 2 * (3 + 4) Evaluated as 1 + (2 * (3+4)) and the result is 15 ? = 5 * 2 + 9 % 4 Evaluated as (5*2) + (9 % 4) and the result is 11 ? = 5 * 2 % ( 7 – 4) Evaluated as (5 * 2) % (7 – 4) and the result is 1

That’s it for now! Next Lecture: More on variables, data types and expressions