Variables Symbol representing a place to store information

Slides:



Advertisements
Similar presentations
Outline 2.1 Introduction 2.2 Basics of C Programs
Advertisements

Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Data types and variables
Introduction Computer program: an ordered sequence of instructions whose objective is to accomplish a task. Programming: process of planning and creating.
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
Basic Elements of C++ Chapter 2.
Objectives You should be able to describe: Data Types
Chapter 2 Getting Started in C Programming
A First Book of ANSI C Fourth Edition
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Numeric Types, Expressions, and Output ROBERT REAVES.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 2: Using Data.
Lecture #5 Introduction to C++
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Lesson 6 Getting Started in C Programming Hu Junfeng 2007/10/10.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
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.
CS115 FALL Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3.
CGS 3460 Servers n rain.cise.ufl.edu n sand.cise.ufl.edu n shine.cise.ufl.edu n thunder.cise.ufl.edu n storm.cise.ufl.edu.
Data Types Declarations Expressions Data storage C++ Basics.
CGS 3460 Input and Output Revisited. CGS 3460 Display Integer Variables – I #include Preprocessor: interact with input/output of your computer Start point.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
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.
INTRODUCTION TO ‘C’ PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.
Java-02 Basic Concepts Review concepts and examine how java handles them.
C Building Block Chapter 2. Variables A variable is a space in the computer’s memory set aside for a certain kind of data and given a name for easy reference.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
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.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Chapter 3 Structured Program Development in C Part II C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1.
Tokens in C Keywords Identifiers Constants
ITEC113 Algorithms and Programming Techniques
Revision Lecture
Variable Symbol represents a place to store information
Fundamental Data Types
C Programming Language
Lectures on Numerical Methods
Fundamental Data Types
Module 2 Variables, Data Types and Arithmetic
Lexical Elements & Operators
DATA TYPES There are four basic data types associated with variables:
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Presentation transcript:

Variables Symbol representing a place to store information name value memory location Example: someone’s age An integer variable age; age = 20; Contrasted with constants No change in constants Computer memory age 20

Why use Variables Remembers a value Results from calculation Example: int v1, v2, sum; v1 = 50; v2 = 30; sum = v1 + v2; (= means assign a new value) Provides an easy way (name) to access your computer's memory Computer memory v1 50 v2 30 sum 80

Variable Declaration A variable must be declared before it can be used. Tell the compiler that you want to reserve some memory in which to store data of some specified type. Usually declared at the start of each function. Declaration format: type name = initial_value; type name1 = initial_value1, name2 = initial_value2, …; Initial values in declaration are optional.

Declaration of Variables type name = initial_value; type name1 = initial_value1, name2 = initial_value2, …; { int v1, v2, sum; v1 = 50; v2 = 30; sum = v1 + v2; } { int v1= 50, v2 = 30, sum; sum = v1 + v2; }

Basic Variables types in C int  integer float  single-precision floating point double double-precision floating point char  one byte character

Variable Names Contains one or more characters, A-Z, a-z 0-9 _ Must start with a non-digit character As always, case-sensitive

Variable Names – cont. Cannot be C's keywords Size Limitation int, main, while, etc Size Limitation Maximum of 63 characters for a variable name Sometimes 31 or 8 in very old C Case sensitive: upper and lower case characters are regarded as different floating int Int main3 4yi

int: integer Variables For integral values only 10, -5, 1000 no fraction 10.2 no commas 12,000 Ranges Machine dependent Minimum: 16 digits in a literal integer 32 bits of storage Signed: −2,147,483,648 to +2,147,483,647 Unsigned: 0 to +4,294,967,295 Maybe 64 on others

Operations for int type Declaration int x, y, z; Assignment y = 10; z = 5; Operations Plus: + x = y + z; Minus: - x = y – z; Muliply: * x = y * z; Divide: / x = y / z; Modulus x = y % z; result of y/z will be truncated

Display Integer Variables – I Preprocessor: interact with input/output of your computer #include <stdio.h> int main() Start point of the program { } Start and finish of function int value1, value2, sum; Declear Variables value1 = 50; value2 = 30; Define Values sum = value1 + value2; Summation printf(“The sum of 50 and 30 is %i\n“, sum); Print the value of an integer variable Printing results return 0; Finish and return value 0

Display Integer Variables – II #include <stdio.h> int main() { int value1, value2, sum; value1 = 50; value2 = 30; sum = value1 + value2; return 0; } printf(“The sum of %i and %i is %i\n“, value1, value2, sum);

int: integer Variables – Special Case I Starting with digit “0” Octal notation. Base 8, not 10 0,1,2,3,4,5,6,7 0177 = 1*64 + 7*8 + 7 = 127 0256 = ? Display %i – print out the decimal value %o – print out the octal value

int: integer Variables – Special Case II Starting with digit “0x” Hexadecimal notation. Based 16, not 10 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F 0x177 = 1*256 + 7*16 + 7 = 375 0xAF = ? 0x2AF = ? Display %i – print out the decimal value %x – print out the hexadecimal value

Example - I #include <stdio.h> int main() { int a, b, c, d; d = 0xAF; printf(“The four numbers are %i, %i, %o, %x\n”, a, b, c, d); printf(“The four decimal numbers are %i, %i, %i, %i\n”, a, b, c, d); }

Example – II #include <stdio.h> int main() { int a, b, c, f; f = a/b; printf(“%i / %i = %i\n”, a, b, f); f = b/a; printf(“%i / %i = %i\n”, b, a, f); f = c/a; printf(“%i / %i = %i\n”, c, a, f); }

float: single-precision Variables For values containing a fractional value 3., 125.8, -0.1 Scientific notation 2.25e-3 = 2.25 * 10-3 = 0.00225 Use e or E for exponent no commas used in big numbers

float Variables - II Ranges IEEE floating-point standard ±3.4×1038

float Variables - III Display %f – print out the decimal value %e – print out the scientific notation %g – let printf decide the format -4 < value of exponent < 5: %f format Otherwise: %e format

Operations for float type Declaration float x, y, z; Assignment y = 10.00; z = 5.8; Calculation Plus: + x = y + z; Minus: - x = y – z; Muliply: * x = y * z; Divide: / x = y / z; result of y/z will NOT be truncated

Example – I #include <stdio.h> int main() { int a, b, c; float f; a = 10; b = 20; c = a/b; printf(“%i / %i = %i\n”, a, b, c); f = a/b; printf(“%i / %i = %f\n”, a, b, f); }

double: double-precision Variables Similar to float More storage space (IEEE floating-point standard) float variables: e+f+1 = 32 double variables: e+f+1 = 64 Ranges ±1.79769×10308 Same display method as float Operation similar as float

char: character Variables For single character Enclosing the character within a pair of ‘ ’ ‘a’ ‘;’ ‘P’ ‘\n’ ‘1’ Display %c

Example – II #include <stdio.h> int main() { int a, b, f; char c; a = 36; b = 52; c = ‘a’; printf(“The three numbers are %i , %i, %i\n”, a, b, c); printf(“The three characters are %c , %c, %c\n”, a, b, c); }

Finish the Code #include <stdio.h> int main(void) { int integerVar = 100; float floatingVar = 331.79; double doubleVar = 8.44e+11; char charVar = ‘W’; printf(“integerVar = %? \n”, integerVar); printf(“floatingVar = %? \n”, floatingVar ); printf(“doubleVar = %? \n”, doubleVar ); printf(“doubleVar = %? \n”, doubleVar ); // scientific notation printf(“charVar = %? \n”, charVar ); return 0; }

Special variable types short usually use less space add “h” after “%” in printf long, long long usually use more space use “l” to indicate add “l” after “%” in printf signed, unsigned specify whether is a signed quantity use “u” to indicate unsigned %u for unsigned

Assignment Operators Join the arithmetic operators Examples: Format: op= Examples: count = count + 10; count += 10; count = count - 5; count -= 5; a /= b + c; a = a / (b + c);

Unary Operators M += 1; M = M + 1; ++M; M++; Unary plus / minus + / - Example: -a Unary increment/decrement ++ / -- M += 1; M = M + 1; ++M; M++;

Arithmetic Operators add: +, minus: -, multiply: *, divide: /, modulus: % Parentheses (grouping): ( ) Unary plus / minus + - Unary increment/decrement ++ --

Operator Precedence Precedence Order for Operators with higher precedence are evaluated first Operators with same precedence are evaluated from left to right In decreasing precedence ( ) unary increment (++), unary decrement (--) unary plus (+), unary minus (-) multiply (*), divide(/), modulus(%) add(+), minus(-) Order for c = -a * b a + b * c / d