By: Md Rezaul Huda Reza UNIVERSITY of South Asia 2 Md Rezaul Huda Reza UNIVERSITY of South Asia 2  Brief.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Advertisements

Data Types and Expressions
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
C# Programming: From Problem Analysis to Program Design1 3 Data Types and Expressions C# Programming: From Problem Analysis to Program Design 2 nd Edition.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
C# Programming: From Problem Analysis to Program Design1 Data Types and Expressions C# Programming: From Problem Analysis to Program Design 3rd Edition.
Chapter 2: Introduction to C++.
ASP.NET Programming with C# and SQL Server First Edition
JavaScript, Third Edition
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Basic Elements of C++ Chapter 2.
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.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Module 1: Introduction to C# Module 2: Variables and Data Types
CIS Computer Programming Logic
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.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Programming in C#. I. Introduction C# (or C-Sharp) is a programming language. C# is used to write software that runs on the.NET Framework. Although C#
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
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.
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.
By: Md Rezaul Huda Reza UNIVERSITY of South Asia 2 Md Rezaul Huda Reza UNIVERSITY of South Asia 2 2 Program.
Lecture #5 Introduction to C++
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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
Chapters 2 & 3. .NET Software development model that allows applications created in disparate programming languages to communicate Universal data access.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Microsoft Visual C#.NET: From Problem Analysis to Program Design1 Chapter 3 Data Types and Expressions Microsoft Visual C#.NET: From Problem Analysis to.
Programming with Microsoft Visual Basic th Edition
1.2 Primitive Data Types and 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.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
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.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
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 Topics The Basics of a C++ Program Data Types
Computing Fundamentals
Basic Elements of C++.
Java Programming: From Problem Analysis to Program Design, 4e
Basic Elements of C++ Chapter 2.
Starting JavaProgramming
Chapter 2: Basic Elements of Java
PHP.
Data Types and Expressions
Data Types and Expressions
elementary programming
Chapter 2: Introduction to C++.
Presentation transcript:

By: Md Rezaul Huda Reza

UNIVERSITY of South Asia 2 Md Rezaul Huda Reza UNIVERSITY of South Asia 2  Brief introduction to Microsoft.NET  Language versus platform neutrality  Evolution of C#  C# Variables. Decision and iteration statements

UNIVERSITY of South Asia 3 Md Rezaul Huda Reza UNIVERSITY of South Asia 3  Quite difficult to remember  Visual Basic was THE Microsoft development language  C++ for “serious” programming  Microsoft Foundation Classes for use with C++ for creating GUIs  ASP (now referred to as “classic” ASP)  Visual Studio  IIS  SQL Server 3

UNIVERSITY of South Asia 4 Md Rezaul Huda Reza UNIVERSITY of South Asia 4  A programming infrastructure created by Microsoft for building, deploying, and running applications and services that use.NET technologies, such as desktop applications and Web Applications.  Not an operating system  Offers Multi-language Independence  One application can be written in more than one language  Includes over 2,500 reusable types (classes)  Enables Creation of dynamic Web pages and Web services 4

UNIVERSITY of South Asia 5 Md Rezaul Huda Reza UNIVERSITY of South Asia 5  A new(ish) language - C#  We could call it “Java in disguise with a dash of C++”  Common Intermediate Language (CIL)  Common Language Runtime (CLR)  language integration  shared libraries  potential platform independence  Simplified deployment of Windows applications 5

UNIVERSITY of South Asia 6 Md Rezaul Huda Reza UNIVERSITY of South Asia 6  The ability of different languages to interoperate (e.g. call each other’s code, share libraries) .NET really does support multiple languages  Developed by Microsoft:  C#  C++/CLI  VB.NET  J# 6

UNIVERSITY of South Asia 7 Md Rezaul Huda Reza UNIVERSITY of South Asia 7 7 Program written in any.NET supported language(s) (e.g. VB.NET, C#, J# etc) Program written in any.NET supported language(s) (e.g. VB.NET, C#, J# etc) Common Intermediate Language (CIL) (.exe or.dll) Common Intermediate Language (CIL) (.exe or.dll) compile Common Language Runtime Executes code, garbage collects etc Common Language Runtime Executes code, garbage collects etc execute

UNIVERSITY of South Asia 8 Md Rezaul Huda Reza UNIVERSITY of South Asia 8  Has the rapid graphical user interface (GUI) features of previous versions of Visual Basic  Has the added power of C++  C#’s class library is called the.NET Framework Class Library = FCL  object-oriented class libraries similar to Java’s APIs 8

UNIVERSITY of South Asia 9 Md Rezaul Huda Reza UNIVERSITY of South Asia 9  Can be used to develop a number of applications  Windows desktop applications  Database access components  Dynamic web pages  Software components  Web services  Console based applications  Mobile applications 9 In VAD (COMP1565)

UNIVERSITY of South Asia 10 Md Rezaul Huda Reza UNIVERSITY of South Asia 10  On all machines at Greenwich  Can be obtained via Student Intranet (see IT Support | Free MSDN Software). This method involves “burning” 3 CDs  If you have broadband, you can download the Microsoft Visual C# Express Edition from: msdn.microsoft.com/vstudio/express/visualcsharp / 10

UNIVERSITY of South Asia 11 Md Rezaul Huda Reza UNIVERSITY of South Asia 11  Development tool that contains a rich set of productivity and debugging features  Supports managed and unmanaged applications  Supports C#, C++, VB.NET, …  Many useful tools and wizards  Windows Forms Designer  ASP.NET Web Forms Designer  Web Services support  SQL Server integration with ADO.NET and XML  not part of the.NET Framework

UNIVERSITY of South Asia 12 Md Rezaul Huda Reza UNIVERSITY of South Asia 12  A project is a group of files which together make up a program being developed  When finished, a program can be delivered in some compact form (eg as a.exe file)  There are different types of applications:  Windows Forms (GUI) applications  Web-related applications  Console applications – no graphics,  others 12

UNIVERSITY of South Asia 13 Md Rezaul Huda Reza UNIVERSITY of South Asia 13 For how to get here - see the Lab “Hello World”

UNIVERSITY of South Asia 15 Md Rezaul Huda Reza UNIVERSITY of South Asia 15  A command that performs an action  They are found inside methods  A method is a named sequence of statements  Must follow a set of rules = syntax  Example of syntax rules: You must terminate all statements with “;”  The specification of what statements do is known as semantics  The trick to good programming is learning its syntax and semantics  Extra reading:  Syntax vs semantics: what's the difference? Syntax vs semantics: what's the difference? 15

UNIVERSITY of South Asia 16 Md Rezaul Huda Reza UNIVERSITY of South Asia 16  Identifiers are names of various program elements in the code that uniquely identify an element.  names of things like variables or fields.  specified by the programmer.  Keywords are reserved words in the C# language.  they can't be used as identifiers.  Examples : class, public, or void 16

UNIVERSITY of South Asia 17 Md Rezaul Huda Reza UNIVERSITY of South Asia 17

UNIVERSITY of South Asia 18 Md Rezaul Huda Reza UNIVERSITY of South Asia 18  A Variable represents a storage location that holds a value.  A “box” holding temporary information  Has a unique name  The value of a variable can be changed through assignment or through use of the ++ and -- operators.  A variable must be definitely assigned before its value can be obtained. 18

UNIVERSITY of South Asia 19 Md Rezaul Huda Reza UNIVERSITY of South Asia 19  Combination of alphabetic characters (a–z, and A–Z), numeric digits (0–9), and the underscore ( _ ) The name must start with a letter or an underscore  No embedded spaces concatenate (append) words together  Keywords cannot be used  Be descriptive with meaningful names Eg footballTeamScore  Convention: start with lower case 19

UNIVERSITY of South Asia 20 Md Rezaul Huda Reza UNIVERSITY of South Asia 20  In order to use a variable in your program, the compiler must be aware of it  Once the compiler knows about a variable, it would reserve an amount of memory space for that variable  How much memory space should the compiler reserve? – it depends what data type of variable is of. 20

UNIVERSITY of South Asia 21 Md Rezaul Huda Reza A data type is an amount of space needed to store the information related to a particular variable.  C# is a type-safe language,  the C# compiler guarantees that values stored in variables are always of the appropriate type 2 types  value  Reference 21

UNIVERSITY of South Asia 22 Md Rezaul Huda Reza UNIVERSITY of South Asia 22

UNIVERSITY of South Asia 23 Md Rezaul Huda Reza Value types  byte: Holds 8-bit unsigned integers. holds positive numbers.  short: Holds 16-bit signed integers.  int: Holds 32-bit signed integers  long: Holds 64-bit signed integers.  char: Holds 16-bit Unicode characters.  float: Holds a 32-bit signed floating-point value. mandatory F suffix  double: 64-bit signed floating-point value. optional D suffix  decimal: 128-bit signed floating-point value. Good for financial calculations. mandatory M suffix  bool: Boolean type. I can have the value: true or false. Reference types – more in future lectures  String A string is an empty space, a character, a word, or a group of words 23

UNIVERSITY of South Asia 24 Md Rezaul Huda Reza UNIVERSITY of South Asia 24  Syntax: DataType VariableName;  Specify the type at the beginning.  Give it a name  Examples: int studentCount; // number of students in the class int ageOfStudent double priceOfTicket; // cost of a movie ticket bool raining; string s; // string declaration string helloString ;//declaration 24

UNIVERSITY of South Asia 25 Md Rezaul Huda Reza UNIVERSITY of South Asia 25  assign a value to a variable with the operator “= “  Syntax variable = expression;  An expression can be  Another variable  Compatible literal value  Mathematical equation  Call to a method that returns a compatible value  Combination of one or more items in this list  Question: Where should a statement be placed inside the code? 25

UNIVERSITY of South Asia 26 Md Rezaul Huda Reza UNIVERSITY of South Asia 26  Local variables can be given an inferred "type" of var instead of an explicit type. var VariableName = [ a value];  The compiler specifies its data type.  you must initialize the variable (provide a value for it). var s = "Hello world"; // s is compiled as a string var i = 5; // i is compiled as an int  Question: Why do you think you must initialize the variable if you declare is as a ‘var’? 26

UNIVERSITY of South Asia 27 Md Rezaul Huda Reza int x; //this is a comment (in green) long LandArea; //declaration x = -3; // assignment double y = 3.0 ; //declaration and assignment LandArea = L; // note the L float Distance = F; // note the F decimal HourlySalary; HourlySalary = 24.25M; string CustomerName; CustomerName = “G Almas"; float totalAmount = 23.57F; var Number1 = F; var Number2 = ; var Number3 = M;  Question:  What types are  Number1  Number2  Number3 ?? 27

UNIVERSITY of South Asia 28 Md Rezaul Huda Reza UNIVERSITY of South Asia 28 int myNumber, count, minIntValue, newValue; char firstInitial; string aSaying, fileLocation; bool isFinished = false; // initialised at declaration count = 2; myNumber = (45 - count) /2; newValue =25; minIntValue = ; firstInitial = ‘B’; aSaying = “First day of the rest of your life!\n "; fileLocation placed before a string literal signal that the characters inside the double quotation marks should be interpreted verbatim (literally). This is useful for file paths*/ count = newValue + myNumber; 28 Q: What is the value of count?

UNIVERSITY of South Asia 29 Md Rezaul Huda Reza UNIVERSITY of South Asia 29  Arithmetic: +, -,*, /,%  String concatenation +  Assignment =, +=, -=, *=, /=  Unary operator num++; // num = num + 1; - postfix increment ++num; // num = num + 1; - prefix increment count--; // count = count – 1; - postfix decrement --count; // count = count – 1; - prefix decrement 29

UNIVERSITY of South Asia 30 Md Rezaul Huda Reza UNIVERSITY of South Asia 30  NOT a value type  Reference type Strings contain a reference to a memory location - (much) more in a future lecture  Do not contain the actual data 30 Hello world string helloString; reference 1 Memory location helloString = “Hello World”; helloString = “Good bye!”; Good bye! helloString Memory location reference 2

UNIVERSITY of South Asia 31 Md Rezaul Huda Reza UNIVERSITY of South Asia 31  Immutable  You can never actually change the contents of a string.  in order to change what a string variable references, a new string object must be created  It can contain nulls: string nullString = null; 31 string s1 = "hello"; string s2 = s1; // s2 does at this point reference the same string //object as s1. Hello s1 reference 1 s2 reference 2 s2 = "goodbye"; a new string object is created for s1 to point to. Hence, following this piece of code, s1 equals "goodbye", whereas s2 still equals "hello" s2 = "goodbye"; a new string object is created for s1 to point to. Hence, following this piece of code, s1 equals "goodbye", whereas s2 still equals "hello" goodbye

UNIVERSITY of South Asia 32 Md Rezaul Huda Reza UNIVERSITY of South Asia 32 Some characters that cannot be easily included. E.g. To allow a quotation mark to be included in a string literal, a special code or escape character sequence is inserted. Example: string valid = "This is a \"string\".";

UNIVERSITY of South Asia 33 Md Rezaul Huda Reza UNIVERSITY of South Asia 33  Implicit conversions:  No special syntax is required because the conversion is type safe and no data will be lost.  Examples: conversions from smaller to larger integral types,  Explicit conversions (casts):  require a cast operator.  The source and destination variables are compatible (i e numbers), but there is a risk of data loss because the type of the destination variable is a smaller size than the source variable. 33

UNIVERSITY of South Asia 34 Md Rezaul Huda Reza UNIVERSITY of South Asia 34 FromTo byte short, int, long, float, double, or decimal shortint, long, float, double, or decimal intlong, float, double, or decimal longfloat, double, or decimal charInt, long, float, double, or decimal floatdouble 34 int num = ; long bigNum = num; // num long can hold any value an int can hold Example

UNIVERSITY of South Asia 35 Md Rezaul Huda Reza UNIVERSITY of South Asia 35  Write the type you are trying to cast into within “()” before the variable name 35 // Cast double to int double x = ; int a; a = (int)x; // a is 1234 Q: What is the value of a? int i = 20; short j = i; Q: Is this correct ? Cannot implicitly convert type 'short' to 'int‘’ !

UNIVERSITY of South Asia 36 Md Rezaul Huda Reza UNIVERSITY of South Asia 36  All numeric types have a Parse( ) method that can take a string as a parameter  double.Parse(“string number”)  int.Parse(“string number”)  char.Parse(“string number”)  bool.Parse(“string number”)  Ex 36 // Convert string to number. string text = "500"; int num = int.Parse(text); int i; i = “20"; Q: Is this correct ? Cannot implicitly convert type 'string' to 'int‘’ !

UNIVERSITY of South Asia 37 Md Rezaul Huda Reza UNIVERSITY of South Asia 37  All numeric types have a ToString( ) method  Convert anything into string representation  Ex  Note : Changing from and to a string is very important  The iext property of a text box contains only strings (same with labels, etc ) 37 // Convert a number to a string. double value = ; string str = value.ToString()

UNIVERSITY of South Asia 38 Md Rezaul Huda Reza UNIVERSITY of South Asia 38  Plus (+) with string Identifiers  concatenates operand2 onto end of operand1 string result; string fullName; string firstName = “Rochelle”; string lastName = “Howard”; fullName = firstName + “ “ + lastName;  Automatic casting to a string:  if you “add” a string to any other value type, the result is a string” int age = 20; string ageString = “ 21” Q: what will (age + ageString) be? 38

UNIVERSITY of South Asia 39 Md Rezaul Huda Reza UNIVERSITY of South Asia 39  The Convert class (System.Convert) can be used to convert from a type to another  Convert.ToDouble( )  Convert.ToDecimal( )  Convert.ToInt32( )  Convert.ToBoolean( )  Convert.ToChar( )  EX 39 // Convert a type to another. string stringValue = “123434”; int newValue = Convert.ToInt32(stringValue);

UNIVERSITY of South Asia 40 Md Rezaul Huda Reza UNIVERSITY of South Asia 40  Add the keyword const to a declaration  Value cannot to be changed  Standard naming convention  Syntax  const type identifier = expression;  const double TAX_RATE = ;  const int SPEED = 70;  const char HIGHEST_GRADE = ‘A’; 40

UNIVERSITY of South Asia 41 Md Rezaul Huda Reza UNIVERSITY of South Asia 41

UNIVERSITY of South Asia 42 Md Rezaul Huda Reza UNIVERSITY of South Asia 42