Elements of a
C# Program
Variables
▪ A variable is a named data storage location in
your computer’s memory. By using a variable’s
name in your program, in effect, referring to the
data stored there.
▪ A variable declaration has the following format:
datatype varname;
Variables
▪ Variables are initialized (assigned a value) with an
equal sign followed by a constant expression.
variable_name = value;
<data_type> <variable_name> = value;
Variables
▪ Some examples are:
int d = 3, f = 5; /* initializing d and f. */
byte z = 22; /* initializes z. */
double pi = 3.14159; /* declares an approximation
of pi. */
char x = 'x'; /* the variable x has the value 'x'. */
Variables
▪ It must be unique.
▪ It can contain letters, digits, and the underscore _
only.
▪ It must start with a letter.
▪ Case-sensitive, num and Num are considered
different names.
▪ Cannot contain reserved keywords.
▪ Spaces are not allowed.
Datatypes
▪ In software programming, datatype refers to the
type of value a variable has and what type of
mathematical, relational or logical operations can
be applied without causing an error.
Data types Range Keyword
Byte 0 to 255 byte
Sbyte -128 to 127 sbyte
Integer -2,147,483,648 to 2,147,483,647 int
Unsigned Integer 0 to 4,294,967,295 uint
Long -9,223,372,036,854,775,808 to long
9,223,372,036,854,775,807
Unsigned Long 0 to 18,446,744,073,709,551,615 ulong
Short -32,768 to 32,767 short
Unsigned Short 0 to 65,535 ushort
Floating point Real numbers - 7 digits float
Double floating point Long real numbers - 15-16 digits double
Decimal 28-29 decimal places decimal
Character Single character char
String More than one character string
Valueless No value void
Boolean True or False bool
Statements
▪ A statement is a complete direction instructing
the computer to carry out some task. C#
statements always end with a semicolon.
▪ A compound statement, also a block, is a group of
two or more statements enclosed in curly braces.
Comments
▪ Comments are used in a program to help us
understand a piece of code. They are human
readable words intended to make the code
readable. Comments are completely ignored by
the compiler.
Comments
▪ Single Line Comments ( // )
Single line comments start with a double slash
//. The compiler ignores everything after // to
the end of the line.
Example:
Comments
▪ Multi -Line Comments ( /* */ )
Multi line comments start with /* and ends with
*/. Multi line comments can span over multiple
lines.
Example:
Expressions
▪ In C#, an expression is anything that evaluates to a
numeric value. C# expressions come in all levels of
complexity.
▪ There are two kinds of expressions in C#: Lvalues
and Rvalues.
Expressions
▪ lvalue: An expression that is an lvalue may appear
as either the left-hand or right-hand side of an
assignment.
▪ rvalue: An expression that is an rvalue may appear
on the right- but not left-hand side of an
assignment.
Expressions
▪ Variables are lvalues and hence they may appear
on the left-hand side of an assignment. Numeric
literals are rvalues and hence they may not be
assigned and cannot appear on the left-hand side.
int g = 20;
Constants
▪ Constants are defined using the const keyword.
Syntax for defining a constant is:
const <data_type> <constant_name> = value;
▪ Example: const int Max_Students = 25;
Operators
▪ An operator is a symbol that instructs C# to
perform some operation, or action, on one or more
operands.
▪ An operand is something that an operator acts on.
In C#, all operands are expressions. C# operators
fall into several categories.
Assignment Operators
▪ Assignment operators are used to assigning
value to a variable. The left side (lvalues) operand
of the assignment operator is a variable and right
side (rvalues) operand of the assignment operator
is a value.
▪ The value on the right side must be of the same
data-type of the variable on the left side otherwise
the compiler will raise an error.
Operator Description Example
= Simple assignment operator, Assigns values from right side C = A + B will assign
operands to left side operand value of A + B into C
+= Add AND assignment operator, It adds right operand to the left C += A is equivalent to
operand and assign the result to left operand C=C+A
-= Subtract AND assignment operator, It subtracts right operand from C -= A is equivalent to
the left operand and assign the result to left operand C=C–A
*= Multiply AND assignment operator, It multiplies right operand with C *= A is equivalent to
the left operand and assign the result to left operand C=C*A
/= Divide AND assignment operator, It divides left operand with the C /= A is equivalent to
right operand and assign the result to left operand C=C/A
%= Modulus AND assignment operator, It takes modulus using two C %= A is equivalent
operands and assign the result to left operand to C = C % A
Arithmetic Operators
▪ C#’s Mathematical operators (Arithmetic
)perform mathematical operations such as
addition, subtraction, multiplication, division,
modulus. The different operators are listed in the
table below.
Operator Symbol Action Example
Increment ++ Increments operand by one ++x, x++
Decrement -- Decrements operand by one --x, x--
Addition + Adds its two operands x+y
Subtraction - Subtracts the second operand from x – y
the first
Multiplication * Multiplies its two operands x*y
Division / Divides the first operand by the x / y
second operand
Modulus % Used with integers only. Gives the x % y
remainder when the first operand is
divided by the second operand
Power Math.Pow Computes exponential values. Math.Pow(x, y)
Arithmetic Operators
▪ Operator Precedence
Operators Relative Precedence
++, -- 1
*, /, % 2
+, - 3
Relational Operators
▪ C#’s relational operators are used to compare
expressions, “asking” questions such as “Is x
greater than 100?” or “Is y equal to 0?”. An
expression containing a relational operator
evaluates as either true( 1 ) or false( 0 ).
Operator Symbol Example
Equal == x==y
Greater than > x>y
Less than < x<y
Greater than or equal >= x>=y
Less than or equal <= x<=y
Not equal != x!=y
Logical Operators
▪ C#’s logical operators enable you to combine two
or more relational expressions into a single
expression that evaluates as either true or false.
C#’s logical operators are listed in the table below.
Operator Symbol Example
AND && expr1 && expr2
OR || expr1 || expr2
NOT ! !expr
Relational & Logical Operators
▪ Operator Precedence
Operator Relative Precedence
! 1
>, >=, <, <= 2
==, != 3
&& 4
|| 5
Operator Precedence
▪ Operator precedence is a set of rules which
defines how an expression is evaluated. In C#,
each C# operator has an assigned priority and
based on these priorities, the expression is
evaluated.
▪ The precedence of multiplication (*) operator is
higher than the precedence of addition (+)
operator.
Category Operators
Grouping ()
Postfix Increment and Decrement ++, --
Prefix Increment, Decrement and ++, --, +, -, !, ~
Unary
Multiplicative *, /, %
Additive +, -
Relational <, <=, >, >=
Equality ==, !=
Logical AND &&
Logical OR ||
Ternary ?:
=, +=, -=, *=, /=, %=, &=, |=, ^=, <<=,
Assignment
>>=
Example #1:
8 / 2 * (10 % 8) / 2
Solution #1:
8 / 2 * (10 % 8) / 2
8 / 2 * (10 % 8) / 2
8/2*2/2
4*2/2
8/2
4
Example #2:
5 < 6 || 5 < 5 && 5 < 1
Solution #2:
5 < 6 || 5 < 5 && 5 < 1
5 < 6 || 5 < 5 && 5 < 1
T || F && F
T || F
T
Example #3:
int a = 5, b = 6, c = 4;
--a * b - ++c
Solution #3:
int a = 5, b = 6, c = 4;
--a * b - ++c
--5 * 6 - ++4
4*6–5
24 – 5
19
Thank You!
Assignment:
1. 10 % 9 – 8 / 7 + 6
2. (20 * 3) / (13 % 2) + ((15 – 10) * 2)
3. 10 % 3 * 3 – (1 + 2)
4. (5 < 6 || 5 < 5) && 5 < 1
5. !(F || (T && F || !(T || F)))