Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick The Programming Process rUse an editor to create a program file (source file). l contains the text of.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
6/4/01 2. Programming with Objects 2.1 Names 2.2 Objects and Classes 2.3 using Objects 2.4 Arithmetic Expressions 2.5 Parameters and Input 2.6 Getting.
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2 Data Types, Declarations, and Displays
ASP.NET Programming with C# and SQL Server First Edition
JavaScript, Third Edition
Basic Elements of C++ Chapter 2.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
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.
COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
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.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
C++ Programming: Basic Elements of C++.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.
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.
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,
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
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: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
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.
C++ Lesson 1.
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Data types Data types Basic types
BASIC ELEMENTS OF A COMPUTER PROGRAM
Lecture 2: Data Types, Variables, Operators, and Expressions
ITEC113 Algorithms and Programming Techniques
Basic Elements of C++.
Data Types, Identifiers, and Expressions
Chapter 3 Assignment Statement
C Short Overview Lembit Jürimägi.
Computing with C# and the .NET Framework
Java Programming: From Problem Analysis to Program Design, 4e
Basic Elements of C++ Chapter 2.
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,
Basics of ‘C’.
Chapter 2: Basic Elements of Java
elementary programming
Module 2 Variables, Assignment, and Data Types
Module 2 Variables, Data Types and Arithmetic
Presentation transcript:

Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman

Variables A variable represents a storage location. Every variable has a name and a type. int age = 19; Name is “age” Type is “int” Value is 19. age 19

Identifiers An identifier names program elements. C# is case sensitive. Digits may occur, but cannot be the first character. Identifiers may use the underscore character. Keywords are identifiers reserved for special uses.

abstract as base bool break byte case catch char checked class const continue decimal default delegate do double else enum event explicit extern false finally fixed float for foreach goto if implicit in int interface internal is lock long namespace new null object operator out override params private protected public readonly ref return sbyte sealed short sizeof stackalloc static string struct switch this throw true try typeof uint ulong unchecked unsafe ushort using virtual void volatile while

Some valid C# identifiers are: savings textLabel rest_stop_12 B3 _test My____my Some invalid identifiers are: 4you // Starts with a number x<y // Includes an illegal character, < top-gun // Includes an illegal character, - int // Reserved keyword

The character set The ASCII character set contain 128 printing and nonprinting characters including upper and lower case letters, digits, and punctuation. C# uses Unicode with contains thousands of characters including ASCII. For English ASCII is sufficient

Type int A type defines the values a variable can have. Type int represents a range of integer values from int.MaxValue -2,147,483,648 to int.MinValue 2,147,483,647.

Assignment Initialize a variable once int age = 19; Assign values during runtime The assignment operator is the equals sign, = age = 10; The value is stored in the memory location allocated for that variable.

a. int age =19; b. age = 10; c. age = 20;

Constructing a program Define a class (used later to define object types) Include a method that contains executable code The Main method starts execution. It must have the static modifier to be accessible before any objects are created.

public class AssignIt { public static void Main( ) { int number1 = 25; int number2 = 12; number2 = number1 + 15; System.Console.WriteLine ("Number2 is now {0}", number2); } // Output -- Number2 is now 40 Example 2.3

UML (Unified Modeling Language) Standard notation for object-oriented design Class diagram has three parts Upper section – class name Middle section – data fields Lower section – methods Underline static methods AssignIt Main

Constants const int FIXED = 25; Cannot assign to a constant. We use upper case names for constants to remind us.

Input from the keyboard System.Console.Write("Enter your name: "); // displays a prompt to the user String name = System.Console.ReadLine(); // reads the line the user enters // saves it as a string of characters int number = int.Parse (System.Console.ReadLine()); // converts input to an integer

Using The statement using System; Allows the code to refer to classes in the System namespace with the explicit prefix. We can replace System.Console.ReadLine(); with Console.ReadLine();

Formatting output {0:C} formats output as currency, dollars and cents in the US. {0, 10} right-aligns the output in a 10 character field {0, -10} left-aligns the output in a 10 character field

Arithmetic Operators Integer data produces integer results. Binary operators, +, -, *, /, % two operands Unary operators +, - one operand

Operation Math notation C# (constants) C# (variables) Addition a + b score1 + score2 Subtraction a - b bats - gloves Multiplication ab 12 * 17 twelve * dozens Division a/b 7 / 3 total / quantity Remainder r in a=qb+r 43 % 5 cookies % people Negation -a -6 -amount Figure 2.9 C# arithmetic operations

Operator Precedence Evaluate * 5 Which operator gets its operands first? has higher precedence than + so * 5 = = 23 Avoid parentheses With parentheses 3 + (4 * 5) = 23 but (3 + 4) * 5 = 7 * 5 = 35

3 + 4 * 5 Figure 2.10 Multiplication gets its operands first

* 5 (3 + 4) Figure 2.11 Compute within parentheses first

Precedence Table Higher precedence -, + Unary Negation and Plus *, /, %Multiplication, Division, Remainder +, -Binary Addition and Subtraction =Assignment Lower precedence See Appendix for full table

Combining Assign and Increment Postincrement – evaluate then add 1 x=5 x++ evaluates to 5 and x becomes 6 Preincrement – add 1 then evaluate x=5 ++x, x becomes 6 then ++x value is 6 Postdecrement – evaluate then subtract 1 x=5 x-- evaluates to 5 and x becomes 4 Predecrement – subtract 1 then evaluate x=5 --x, x becomes 4 and --x value is 4

Figure 2.12 Expression a) and equivalent expression b) then 3 + x x++ a.b. 3 + x++

Figure 2.13 Expression a) and equivalent expression b) then ++x 3 + x a.b x

Methods A method can contain the code for an operation. public static int MultiplyBy4(int aNumber) { return 4*aNumber; } Parameters pass data to the method return statement specifies the return value, if any

Argument 5 multiplyBy4 Return value 20

Pass By Value C#passes arguments by value by default The called method receives the value of the argument not the location The value from the caller gets copied to the parameter which is like a local variable of the method PassByValue.cs illustrates value of argument x copied to aNumber

Figure 2.17 Memory usage cube aNumber 12 x value 0 After the call then 17 During the call main result Before the call to cube(x)