Lesson 3: Variables and Expressions

Post on 02-Jul-2015

1019 Views

Category:

Education

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

Matthayom 3 Computer Science Supplementary Class on Programming with C#, Lesson 3

Transcript

VARIABLES & EXPRESSIONSM3 COMPUTER SCIENCE CLASS – TERM 2

THE PRINCE ROYAL'S COLLEGE

WHAT IS THE BASIC C# SYNTAX?

BASIC C# SYNTAX:

•C# code is made of a series of statements where

each statement ends with a semicolon “;”

•C# code is organized in “blocks”, which may

contain any number of statements, and are

bounded by braces “{” and “}”

{

<code line 1, statement 1>;

<code line 2, statement 2>

<code line 3, statement

2>;

}

LAYOUT OF A BLOCK OF CODE IN C#

WHAT IS A COMMENT?

•A comment is a descriptive text added to a

code. It is not executed/run because it is not

really part of the program code.

•Examples:

/* This is a comment */

// This is a different sort of comment.

/// A special comment

WHAT ARE VARIABLES?

•Variables store data which can be used and

changed within a program; in C# they have to

be assigned a name and a type first before they

can be used in C#

•C# syntax for declaring variables merely

specifies the type and variable name:

<type> <name>;

EXAMPLES OF VARIABLES:

• Integer TypesType Alias For Allowed Values

sbyte System.SByte Integer between −128 and 127

byte System.Byte Integer between 0 and 255

int System.Int32 Integer between −2147483648 and 2147483647

uint System.UInt32 Integer between 0 and 4294967295

long System.Int64 Integer between −9223372036854775808 and 9223372036854775807

ulong System.UInt64 Integer between 0 and 18446744073709551615

EXAMPLES OF VARIABLES:• Floating Types

• Text and Boolean Types

Type Alias For MIN/MAX Values

float System.Single 1.5 × 10−45 / 3.4 × 1038

double System.Double 5.0 × 10−324 / 1.7 × 10308

decimal System.Decimal 1.0 × 10−28 / 7.9 × 1028

Type Alias For Allowed Values

char System.Char One Unicode character, stored as an integer between 0 and 65535

bool System.Boolean Boolean value, true or false

string System.String A sequence of characters

HOW DO YOU NAME VARIABLES?

• The first character of a variable name must be either a

letter, an underscore character(_), or the at symbol

(@).

•Next characters may be letters, underscore characters,

or numbers.

•C# is case sensitive, so be careful and remember the

exact case used when you declare your variables.

WHAT ARE EXPRESSIONS?

•Basic building blocks of computation made by

combining variables and operators.

WHAT ARE OPERATORS?•Symbols that tells the compiler to perform

specific mathematical or logical manipulations.

• TYPES OF OPERATORS:

• Arithmetic Operators

• Relational Operators

• Logical Operators

• Bitwise Operators

• Assignment Operators

• Miscellaneous Operators

WHAT ARE OPERATORS?•Symbols that tells the compiler to perform

specific mathematical or logical manipulations.

• TYPES OF OPERATORS:

• Arithmetic Operators

• Relational Operators

• Logical Operators

• Bitwise Operators

• Assignment Operators

• Miscellaneous Operators

MATH OPERATORS:Operator

Sample Result

+ var1 = var2 + var3;

var1 is assigned the value that is the sum of var2 and var3

- var1 = var2 -var3;

var1 is assigned the value that is the value of var3 subtracted from the value of var2

* var1 = var2 * var3;

var1 is assigned the value that is the product of var2 and var3.

/ var1 = var2 / var3;

var1 is assigned the value that is the result of dividing var2 by var3

% var1 = var2 % var3;

var1 is assigned the value that is the remainder when var2 is divided by var3

STRING CONCATENATION OPERATOR:Operator

Sample Result

+ var1 = var2 + var3;

var1 is assigned the value that is the concatenationof the two strings stored in var2 and var3

INCREMENT AND DECREMENT OPERATORS:Operator

Sample Result

++ var1 = ++var2; var1 is assigned the value of var2 + 1. var2 is increased by 1var1 = var2++;

-- var1 = --var2; var1 is assigned the value of var2 - 1. var2 is decreased by 1var1 = var2--;

ASSIGNMENT OPERATORS:Operator

Sample Result

= var1 = var2; var1 is assigned the value of var2

+= var1 += var2; var1 is assigned the value that is the sum ofvar1 and var2

-= var1 -= var2; var1 is assigned the value that is the value of var2 subtracted from the value of var1.

*= var1 *= var2; var1 is assigned the value that is the product of var1 and var2.

/= var1 /= var2; var1 is assigned the value that is the concatenationof the two strings stored in var2 and var3.

%= var1 %= var2; var1 is assigned the value that is the remainder when var1 is divided by var2

Priority Operators

Highest ++, -- (used as prefixes); +, - (unary)

*, /, %

+, -

=, *=, /=, %=, +=, -=

Lowest ++, -- (used as suffixes)

ORDER OF OPERATORS:

SOURCES

• "Tutorials Point." C# Tutorial. N.p., n.d. Web. 20 Nov. 2014.

• "Variables." The Complete C# Tutorial. N.p., n.d. Web. 20 Nov.

2014.

• "C# Programming." Wikibooks. N.p., n.d. Web. 20 Nov. 2014.

• Watson, Karli, Jacob Vibe Hammer, John D. Reid, Morgan

Skinner, Daniel Kemper, and Christian Nagel. Beginning Visual

C#® 2012 Programming. Indianapolis: John Wiley and Sons,

2013. Print.

THE END

top related