C# - variablesBjørk Busch1 Variables Basic types (also defined as classes with methods) : Integer-types Int, long, byte, sbyte, short, ushort, uint, ulong.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Neal Stublen C# Data Types Built-in Types  Integer Types byte, sbyte, short, ushort, int, uint, long, ulong  Floating Point Types.
Constants and Data Types Constants Data Types Reading for this class: L&L,
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Types and Variables. Computer Programming 2 C++ in one page!
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Java Syntax Primitive data types Operators Control statements.
CS1061 C Programming Lecture 5: Building Blocks of Simple Programs A. O’Riordan, 2004.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
JavaScript, Third Edition
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.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Chapter 2: Introducing Data Types and Operators.  Know Java’s primitive types  Use literals  Initialize variables  Know the scope rules of variables.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
2440: 211 Interactive Web Programming Expressions & Operators.
Java Methods. Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another.
C-Language Keywords(C99)
Chapter 3: Data Types and Operators JavaScript - Introductory.
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.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Chapter 2: Using Data.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
STATEMENTS AND FLOW OF EXECUTION CHAPTER 11 OF APRESS A PROGRAMMERS GUIDE TO C SHARP 5.0.
 Character set is a set of valid characters that a language can recognise.  A character represents any letter, digit or any other sign  Java uses the.
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.
Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical.
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
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.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
Copyright Curt Hill Variables What are they? Why do we need them?
Getting Started Java Fundamentals CSC207 – Software Design Summer 2011 – University of Toronto – Department of Computer Science.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
C++ Basics Tutorial 5 Constants. Topics Covered Literal Constants Defined Constants Declared Constants.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
1.2 Primitive Data Types and Variables
ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
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.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
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.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
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.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
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.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Basic Introduction to C#
Java Variables and Types
Yanal Alahmad Java Workshop Yanal Alahmad
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Scope, Objects, Strings, Numbers
Computing with C# and the .NET Framework
IDENTIFIERS CSC 111.
Basics of ‘C’.
An overview of Java, Data types and variables
Review for Exam 1 Spring 2007 CS 101/CS 101-E.
Module 2 Variables, Assignment, and Data Types
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Unit 3: Variables in Java
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

C# - variablesBjørk Busch1 Variables Basic types (also defined as classes with methods) : Integer-types Int, long, byte, sbyte, short, ushort, uint, ulong Real-types float, double, decimal (fixed decimal) Bool-types boolean: true / false Char-types char: ascii and unicode (16 bit) Object types: Always references (pointer) to object, allocated dynammic Special: String-types string: object with lots of methods, constant value, operator-overload, acts nearly like basic types. Concatenation with + operator

C# - variablesBjørk Busch2 Calculation on numeric types Integer-types ’+’: add fx x = y+z, where z is set to value of y+z ’-’: sutract fx x = y-z, where z is set to value of y+z ’*’: multiply fx x = y*z, where z is set to value of y*z ’/’: int divide fx x = y/z, where z is set to value of kvotient of y/z ’%’: modulus divide fx x = y%z, where x is set to value of rest of y/z Normal precedens-rulls are uses including paranteses () Real-types As for the integer-types except the folowing ’/’: real divide fx x = y/z, where z is set to value of y/z with decimals

C# - variablesBjørk Busch3 Calculation on numeric variables Short form of calculation Unary operators ’++’: increment fx ++x or x++, where z is incrementet by 1 ’--’: decrement fx --x or x-, where z is decrementet by 1 Binary operators ’+=’: add fx x += y, where x is set to value of x+y ’-=’: sutract fx x -= y, where x is set to value of x+y ’*=’: multiply fx x *= y, where x is set to value of x*y ’/=’: int divide fx x /= y, where x is set to value of kvotient of x/y ’%=’: modulus divide fx x %= y, where x is set to value of rest of x/y

C# - variablesBjørk Busch4 Comparation on numeric values – result is boolean ’x == y’: return true when x is equal to y, else false ’x != y’: return true when x is not equal to y, else false ’x < y’: return true when x is less than y, else false ’x <= y’: return true when x is less than or equal y, else false ’x > y’: return true when x is greater than y, else false ’x >= y’: return true when x is greater than or equal y, else false

C# - variablesBjørk Busch5 Calculation on boolean values Unary operators ’!x’: not x, returns true if x==false, otherwise false Binary operators ’x || y’: x or y, returns true if x or y is true, else false ’x && y’: x and y, return false if x and y is false, else true Normal precedens-rulls are uses including paranteses () Example:

C# - variablesBjørk Busch6 Variables declaration Variables can be declared as class variable ~ one instance per class object variable ~ one instance per object of the class parameter variable ~ one instance per start of method local variable (declared inside a scope in a method) ~ one instance per declaration. Variables can be simple types (int, float,….., boolean and char) objects (references to) for parameters also special ref and out (references to variable), comes later in the course

C# - variablesBjørk Busch7 Excamples of variable declarations class MyClass { static int classvar; // a class variabel is declared as static int objectvar; // a object (instance) variable is declared without static keyword public void MyMethod (int parametervar) // a normal value parameter declaration { int localvar; // a normal local variabel declaration for (int forvar=1; forvar < 10; ++forvar) // a local variablel declaration lives in for-scope { ……………… } { int localscopevar // a variabel declaration for a local scope ……………… }

C# - variablesBjørk Busch8 Livetimes of variables Class variable Lives from the first reference to the class to the end of application Object variable Lives from the the object i created with new and until there is no referece to the object. Garbage-collection is done automatic. After the last reference to the object is gone, it is up to the garbage- collector to free the allocated memory. Continued

C# - variablesBjørk Busch9 Livetimes of variables (continued) Parameter-variable (value parameter) Lives from the the beginning of the method until it returns Local variables Lives from it is declared to end of the scope. The scope is starting with ”{” and end with ”}” Declaration in a for-construction lives inside the loop Objects may live longer if other references to it og shorter if it is set to null.

C# - variablesBjørk Busch10 Reference and names of variables in an object method class MyClass { static int x, y, z; int u, v, w; public void MyObjectMethod (int x, int u, int a) // allowed { // not allowed int x,u,a; while name already declared as parameter int y, v; // allowed local variables - same name as in class and object int l; // allowed - new name l = x; // x is the parameter variable l = y; // y is the local variable l = z; // z is the class variable l = w; // w is the object (class instanse) variable l = MyClass.x; // x is the class variable l = this.u; // u is the object (class instanse) variable }

C# - variablesBjørk Busch11 Reference and names of variables in an class method class MyClass { static int x, y, z; int u, v, w; public static void MyClassMethod (int x, int u, int a) // allowed { // not allowed int x,u,a; while name already declared as parameter int y, v; // allowed local variables - same name as in class and object int l; // allowed - new name l = x; // x is the parameter variable l = y; // y is the local variable l = z; // z is the class variable l = w; // w is the object (class instanse) variable l = MyClass.x; // x is the class variable // not allowed l = this.u; // u is the object (class instanse) variable }

C# - variablesBjørk Busch12 Encapsulation of object and class variables Variables of a class or object may be public: can be accessed direct outside the class / object internal: can be accessed direct outside the class / object but only from within the namespace protected: can be accessed direct from a inherited class but else not private: can not be accessed outside the class, neither from inherited class readonly: an extra keyword to the above. A readonly variable can only be set when it is definined or in the constructors (like java final except may be set in constructors).

C# - variablesBjørk Busch13 Converting of numeric variables Typecasting from real to integer (y is a float, double..) int x = (int) float; long x = (long) float; Converting from real and integer to string (x and y is float, int ….) string s = x.ToString ( ) string s = x.ToString ( formating-string ) - Fx x.ToString(”F2”) and y.ToString(”N5”) string s = string.Format ( formating-string, values ) ~ C’s sprintf - Fx string.Format ( ”x is {0:F2} and y is {1:N5}”, x, y ) Console.Writeline(( formating-string, values ) ~ C’s printf - Fx Console.Writeline( ”x is {0:F2} and y is {1:N5}”, x, y ) Converting from string to real and integer (s is a string with the number) int x = int.Parse ( s ); float y = float.Pase ( s );

C# - variablesBjørk Busch14 Formating codes (ordinary used) C, c currency D, d decimal E, e Scientific exponential F, f fixed-point G, g general N, n number R, r roundtrip (format is to be converted back) X, x hexadecimal = position-allocation for leading zero # position-allocation for significant number. decimal point char, group separator % percent char ; section separator

C# - variablesBjørk Busch15 Common escape characters for printing \' Single quote \" Double quote \\ Backslash \0 Null \a Allert \b Backspace \f Formfeed \n Newline \r Carriage return \t Horizontal tab \v Vertical tab