Module 2 Variables, Data Types and Arithmetic

Slides:



Advertisements
Similar presentations
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Advertisements

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 and variables
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Chapter 2 Data Types, Declarations, and Displays
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Objectives You should be able to describe: Data Types
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
C Tokens Identifiers Keywords Constants Operators Special symbols.
C-Language Keywords(C99)
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
Chapter 2 Variables.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
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.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Numbers in ‘C’ Two general categories: Integers Floats
C++ Lesson 1.
Asst.Prof.Dr. Tayfun ÖZGÜR
Chapter 2: Basic Elements of C++
Chapter 2 Variables.
Chapter 2: Introduction to C++
Data types Data types Basic types
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chap. 2. Types, Operators, and Expressions
Tokens in C Keywords Identifiers Constants
ITEC113 Algorithms and Programming Techniques
Fundamental of Programming (C)
C Short Overview Lembit Jürimägi.
Computing with C# and the .NET Framework
Java Programming: From Problem Analysis to Program Design, 4e
By: Syed Shahrukh Haider
Chapter 2: Introduction to C++
Introduction to C Programming
Chapter 2: Introduction to C++
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.
Starting JavaProgramming
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.
Variables in C Topics Naming Variables Declaring Variables
Basics of ‘C’.
Beginning C Lecture 2 Lecturer: Dr. Zhao Qinpei
Chapter 2: Basic Elements of Java
Introduction to C Programming
Chapter 2 Variables.
Lectures on Numerical Methods
Variables in C Declaring , Naming, and Using Variables.
elementary programming
Chapter 2: Introduction to C++.
WEEK-2.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Programming Language C Language.
Primitive Types and Expressions
Chapter 2 Variables.
Chapter 2 Primitive Data Types and Operations
C Language B. DHIVYA 17PCA140 II MCA.
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

Module 2 Variables, Data Types and Arithmetic

Naming Rules and Conventions Contain only letters, digits and underscore Cannot begin with a digit Are case-sensitive Begin with lowercase Do not begin with underscore Multi-word variables www.umbctraining.com @UMBC Training Centers 2013

@UMBC Training Centers 2013 Keywords Commonly used keywords break double goto short typedef case else if signed union char enum int sizeof unsigned continue extern long static void default float register struct while do for return switch   www.umbctraining.com @UMBC Training Centers 2013

@UMBC Training Centers 2013 Other Rules Variables may be declared anywhere in your code, but must be declared before they are used Global variables are permitted and declared outside of any block www.umbctraining.com @UMBC Training Centers 2013

@UMBC Training Centers 2013 Basic Data Types int integer values, no decimal point float floating point values with decimal part double same as float, but more precision char a single character _Bool boolean values 0 (false) and 1 (true) This a C99 data type addition www.umbctraining.com @UMBC Training Centers 2013

@UMBC Training Centers 2013 Type Specifiers long Applied to int to give a (possibly) larger range of values Also applicable to double long long Applied to int to give an even larger range of values short Applied to int to (possibly) limit the range of values unsigned Applied to int to indicate only zero and positive values signed (default type) Applied to int to specify positive, negative and zero values allowed www.umbctraining.com @UMBC Training Centers 2013

@UMBC Training Centers 2013 Integral Data Types The table below shows the usual sizes and ranges of the integral data types on a 32-bit machine Your sizes and ranges may vary Data Type Size (in bytes) Range of values signed short int 2 -32768 to 32767 unsigned short int 0 to 65535 signed int 4 -2147483648 to 2147483647 unsigned int 0 to 4294967295 signed long int unsigned long int signed long long int 8 -9 x 1018 to 9 x 1018 unsigned long long int 0 to 18 x 1018 www.umbctraining.com @UMBC Training Centers 2013 7

Floating Point Data Types The table below shows the usual sizes and ranges of the floating point data types on a 32-bit machine Your sizes and ranges may vary Data Type Size (in bytes) Precision Values float 4 Approximately 6 digits Approximately -10^38 to 10^38 double 8 Approximately 15 digits Approximately -10^308 to 10^308 long double Machine Dependent Even more precision ??? www.umbctraining.com @UMBC Training Centers 2013 8

@UMBC Training Centers 2013 Booleans Data type: _Bool Size in bytes: (probably) 1 Range of values: 0 , 1 stdbool.h Defines bool data type Defines true and false www.umbctraining.com @UMBC Training Centers 2013

@UMBC Training Centers 2013 Literal Constants Decimal (base 10) integer constant 1345 1345L Octal (base 8) constant 0173 Hexadecimal (base 16) constant 0x12F4C Floating Point constant 7.23 723e-2 String constant “Hello World” Character constant ‘z’ ‘\n’ www.umbctraining.com @UMBC Training Centers 2013

@UMBC Training Centers 2013 Named Constants #define preprocessor directive Literal string substitution #define VOTING_AGE 18 #define GREETING “Hello!” #define PI 3.14159 #define QUIT ‘q’ Typically DO NOT end with semi-colon www.umbctraining.com @UMBC Training Centers 2013

@UMBC Training Centers 2013 Exercises Questions Text pg 40 - #2, #3 Just write down your answers They do not involve coding www.umbctraining.com @UMBC Training Centers 2013

Variable Declarations int height = 72; long maxNumberOfPassengers; char middleInitial = ‘L’, newLine = ‘\n’; double averageScore = 0.0; unsigned int nrRows = 5, nrColumns = 10; short average; www.umbctraining.com @UMBC Training Centers 2013

Displaying Basic Types printf characters for basic type output Integers %d, %i %u (unsigned) Floating Point %f – default (6 decimal places) %e – scientific notation %g – automatically chooses between %f and %e format Characters %c – a single character Booleans %d, %i, %u www.umbctraining.com @UMBC Training Centers 2013

@UMBC Training Centers 2013 In-Class Example VarBasicOutput project Look at floatingVar and it’s results Also look at the boolean output as an integer what’s the difference between %d and %i pg 26 in book www.umbctraining.com @UMBC Training Centers 2013

@UMBC Training Centers 2013 Operator Definitions Operand An expression which is manipulated by an operator Operator A symbol that represents the action to be taken Unary, Binary or Ternary www.umbctraining.com @UMBC Training Centers 2013

@UMBC Training Centers 2013 In-Class Example VarDataDemo project www.umbctraining.com @UMBC Training Centers 2013

@UMBC Training Centers 2013 Arithmetic Addition x = 3 + 2.5; y = z + w; Subtraction x = 3.7 – 2; d = 5 – z; Multiplication x = 3.6 * 2.5; x = 5 * y; Division x = 3 / 2; x = 3 / 2.0; Modulus x = 3 % 2; y = z % 6; www.umbctraining.com @UMBC Training Centers 2013

@UMBC Training Centers 2013 Exploring Division Integer Division int / int int 3 / 2 = 1 Floating point division float / float float 3.0 / 2.0 = 1.5 Mixed Division int / float float 3 / 2.0 = 1.5 float / int float 3.0 / 2 = 1.5 www.umbctraining.com @UMBC Training Centers 2013

@UMBC Training Centers 2013 Type Conversion int i1, i2 = -150; float f1 = 123.125, f2; i1 = f1; f1 = i2; f1 = i2 / 100; f2 = i2 / 100.0; f2 = (float)i2 / 100; www.umbctraining.com @UMBC Training Centers 2013

@UMBC Training Centers 2013 Binary Operators Two operands x = x + 5 ; y = y * 6; z = z – (x + 2); Shortcut notation x += 5; y *= 6; z -= x + 2; www.umbctraining.com @UMBC Training Centers 2013

@UMBC Training Centers 2013 Pre/post-increment x++ (post-increment) int z, x = 5; x++; z = 3 * x++; ++x (pre-increment) ++x; z = 3 * ++x; www.umbctraining.com @UMBC Training Centers 2013

@UMBC Training Centers 2013 Pre/post-decrement x-- (post-decrement) int z, x = 5; x-- z = 3 * x-- --x (pre-decrement) --x; z = 3 * --x; www.umbctraining.com @UMBC Training Centers 2013

Precedence and Associativity Level Operators Associativity Operation 1 () expr++,expr -- L R Parentheses 2 ++expr, --expr R L Unary Operators 3 +,- 4 *, /, % Binary Operators 5 +, - 6 =, -=, +=, -=, *=, /=, %= Assignment operators www.umbctraining.com @UMBC Training Centers 2013

@UMBC Training Centers 2013 In-Class Exercises Solve these expressions http://www.csee.umbc.edu/courses/undergraduate/201/spring09/misc/arithmetic.shtml www.umbctraining.com @UMBC Training Centers 2013

@UMBC Training Centers 2013 Lab Exercises Text Questions pg 40 #1 not all do first one, then another of your choice #2-8 Ex1-ArithmeticPractice.docx—from CMSC 201 Ex2-Coins.docx – quarters, nickels, dime, pennies to dollars and cents www.umbctraining.com @UMBC Training Centers 2013

@UMBC Training Centers 2013 www.umbctraining.com @UMBC Training Centers 2013