17
VARIABLES & EXPRESSIONS M3 COMPUTER SCIENCE CLASS – TERM 2 THE PRINCE ROYAL'S COLLEGE

Lesson 3: Variables and Expressions

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Lesson 3: Variables and Expressions

VARIABLES & EXPRESSIONSM3 COMPUTER SCIENCE CLASS – TERM 2

THE PRINCE ROYAL'S COLLEGE

Page 2: Lesson 3: Variables and Expressions

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 “}”

Page 3: Lesson 3: Variables and Expressions

{

<code line 1, statement 1>;

<code line 2, statement 2>

<code line 3, statement

2>;

}

LAYOUT OF A BLOCK OF CODE IN C#

Page 4: Lesson 3: Variables and Expressions

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

Page 5: Lesson 3: Variables and Expressions

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>;

Page 6: Lesson 3: Variables and Expressions

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

Page 7: Lesson 3: Variables and Expressions

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

Page 8: Lesson 3: Variables and Expressions

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.

Page 9: Lesson 3: Variables and Expressions

WHAT ARE EXPRESSIONS?

•Basic building blocks of computation made by

combining variables and operators.

Page 10: Lesson 3: Variables and Expressions

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

Page 11: Lesson 3: Variables and Expressions

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

Page 12: Lesson 3: Variables and Expressions

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

Page 13: Lesson 3: Variables and Expressions

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--;

Page 14: Lesson 3: Variables and Expressions

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

Page 15: Lesson 3: Variables and Expressions

Priority Operators

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

*, /, %

+, -

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

Lowest ++, -- (used as suffixes)

ORDER OF OPERATORS:

Page 16: Lesson 3: Variables and Expressions

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.

Page 17: Lesson 3: Variables and Expressions

THE END