1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

Slides:



Advertisements
Similar presentations
Neal Stublen C# Data Types Built-in Types  Integer Types byte, sbyte, short, ushort, int, uint, long, ulong  Floating Point Types.
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.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
CMT Programming Software Applications
1 Chapter 3 Arithmetic Expressions. 2 Chapter 3 Topics l Overview of Java Data Types l Numeric Data Types l Declarations for Numeric Expressions l Simple.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Data types and variables
The Fundamentals of C++ Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
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
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
String Escape Sequences
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
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.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
2440: 211 Interactive Web Programming Expressions & Operators.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Numeric Types, Expressions, and Output ROBERT REAVES.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Chapter 2: Using Data.
The Fundamentals of C++ Chapter 2: Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Week 1 Algorithmization and Programming Languages.
Beginning C++ Through Game Programming, Second Edition
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Mathematical Calculations in Java Mrs. G. Chapman.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
At the end of the module the students should be able to:  Familiarize themselves with the different uses of constants, operators, and expressions in.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX.
Primitive Variables.
Copyright Curt Hill Variables What are they? Why do we need them?
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Chapter 2 Variables.
INT213-Week-2 Working with Variables. What is variable
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Mathematical Calculations in Java Mrs. C. Furman.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
GUJARAT KNOWLEDGE VILLAGE Faculty Name:- Prof H.M.Patel Students Name:- SONI VISHAL THAKKER BHAVIN ZALA JAYDIP ZALA.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
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.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
© 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 Variables.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Java Programming: From Problem Analysis to Program Design, 4e
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Introduction to C++ Programming
Basics of ‘C’.
Primitive Types and Expressions
Unit 3: Variables in Java
Chapter 2 Variables.
Presentation transcript:

1 Values & Variables

2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords can not be used as variable names

3 Keywords

4 How to define a variable int x = 3; Type Variable Value Semicolon: End of statement

5 Ways to define a variable int nInteger; string sString; int nInteger = 42; string sString = "This is a string!"; int nInteger; string sString;... nInteger = 42; sString = "This is a string!"; Double quotes represents a string

6 Necessity to set a value to a variable string sValueless; MessageBox.Show(sValueless); Error!

7 Variable Types Simple types Integers Floating point numbers Characters Strings

8 Integers short 2 bytes (–32,768 32,767) short sval = -12; ushort 2 bytes (0 65,535) ushort sval= 12; int 4 bytes (–2,147,483,647 2,147,483,647) int nval = ; uint4 bytes (0 4,294,967,295) uint nval = 12500; long 8 bytes long lVal = ; ulong 8 bytes Ulong lVal =

9 Floating Point Numbers float 4 bytes float fVal = -1,2; double 8 bytes double dVal = ; decimal 8 bytes decimal dVal = ;

10 Expressions Expressions are used for performing operations over variables. Return a value of known type. Two types of expressions Operators Functions

11 Arithmetic operations They need more than one variable. Performs mathematical operations + (addition operation) - (subtraction operation) * (multiplication operation) / (division operation) % (modulus operation) ….

12 Arithmetic operations Abbreviations int m = 5; int n = 4; m = m + n; equals m += n; In other words in the end of both expressions m will have value of 9 and the value of n will not be changed.

13 Increment and decrement operations They operate on one variable ++ is increment operator i++; i = i + 1; -- is decrement operator i --; i = i – 1; Prefix and postfix operators will yield to different results. i.e. “i++” and “++i” are not same.

14 Increment and decrement operations ++k The result of the operation is the value of the operand after it has been incremented. k++ The result of the operation is the value of the operand before it has been incremented. --k The result of the operation is the value of the operand after it has been decremented. k-- The result of the operation is the value of the operand before it has been decremented.

15 Example int k=0, m; m = ++k; Values of m and k Values of m and k will be 1 int k=0, m; m = k++; m m will be 0 and k k will be 1 int k=5, m, n=2; m = --k + n; m will be 6 and k will be 4 int k=0, m, n=7; m = k n; m will be 6 and k will be 1 and n will be 6

16 Exercise What will be the values of the variables after code piece below is executed? int i, j, k; i = 2; j = 3 + i++; k = i; i *= ++k + j--; i /= k j;

17 Exercise Assuming that line of codes are independent, what will be the value of variable m after each line is executed? int i = 0, j = 6, k = 4, m = 5; m = k i;m = k i; m *= j % 4;m *= j % 4; m += k++ + (j-- * ++i);m += k++ + (j-- * ++i);

18 Order of Operations Rules that defines which procedures should be performed first. In C# language some operators have execution privilege over others. To predict the result of an expression first we should know the order of operations.

19 Example PEMDAS phrase may help to remember the order. P Parenthesis E Exponent MMultiplication DDivision AAddition SSubtraction P Parenthesis E Exponent MMultiplication DDivision AAddition SSubtraction * / 5 = ? 1 + (2 * 3) – (4 / 5)

20 Example(result) If we use all numbers in integer type then the result will be integer(In other words fraction will be removed) 4/5 = 0 (integer division) 1 + (2 * 3) – (4 / 5) 7 7

21 Exercise Different data types may yield different results in same operations. Write and execute the codes in the next slides. Explain the difference between results.

22 Exercise (continues)

23 Exercise (continues)

24 Characters char 1 byte0-256 'a' 'z' 'A' 'Z' '?' '0' '9' Special characters are represented by using “\” prefix. '\n' : new line '\t' : tab '\'' : single quote '\\' : backslash

25 Strings (Character Arrays) Sequence of characters. Example: “Hello!” “first line\n second line \n third line” “” Empty string

26 Strings “string” Class Unicode – 16 bit Example: string myString = “Hello!”; Verbatim strings string myString disk”;

27 string operations Appending two strings Result: “Hello world!”

28 string operations Searching within a string int IndexOf () Result: 1 Exercise: Find the usage of LastIndexOf() function and write an example by using this function.

29 string operations Retrieve a substring from a string string Substring() Result : “llo”

30 Exercise Put your name and surname into two string variables. Concatenate two strings. Write the result to the console.

31 DateTime C# language has built-in “DateTime” structure to represent date and time values. We can store “year, month, day, hour, minute, second” values in a DateTime structure.

32 Creating a DateTime Object DateTime dt = new DateTime(year, month, day); Type Variable name Creating a new object Initial values

DateTime Fundamentals 33 Functions and Properties AddDays, AddMonths, AddYears DateTime.Now DayOfWeek TimeSpan

34 Example A new DateTime object is created

35 Constants Their values can not be changed. They have types. We can use them in expressions bur can not alter them. Defined by using “const” keyword before variable type. Their values can be set only during definition line. const int nVar = 34;

36 Example