Module 3:
Variables & Expressions
By
SRIRAM . B
Overview
Variables
DataTypes
Operators
Arithmetic
Relational
Logical
Conditional / Ternary Operators
Unary Operators
Variables
Variables are fundamental to any programming
language.
Values can be assigned to variables and changed during
program execution.
The value assigned to a variable is stored in the
memory location represented by the variable name.
Syntax : <type> <name>;
A variable can be declared as given below.
int var1;
where int represents the data type, and var1 is the
name given to the variable.
DataTypes
Data types are used for the actual representation
of data in the memory.
C-Sharp supports two kinds of data types:
Value types Reference
types
• bool
• Class types
• char, string
• Interface
• byte, short ,int, long
types
• Float, double, decimal
• Array types
• enum types
• struct types
Example 1 :- Using Variable and
Data types..
using System;
class Hello
{
public static void Main(){
//Variable declaration
int ivar;
float fvar;
char cvar;
//Variable initialization
ivar=10;
fvar=100.05f;
cvar=‘A’;
Example 1 :- Using Variable and
Data types..
//Display the contents of variables
Console.WriteLine("Value stored in int variable
is:{0}",ivar);
Console.WriteLine("Value stored in float variable
is:{0}",fvar);
Console.WriteLine("Value stored in character
variable is:{0}",cvar);
}
}
Output
Output of the code is:
Value stored in int variable is :10
Value stored in float variable is:100.05
Value stored in character variable is:A
Example 2 :- Using String Data type
The string data type is used for storing a stream of
characters.
using System;
class use_string
{
public static void Main()
{
string name="Jolly";
Console.WriteLine("Name is {0}",name);
}
}
Output of the code is: Name is Jolly
Operators
Operators indicate the type of operation to be performed on
the operands in a given expression.
They can be classified based on their functionality as follows:
Arithmetic Operators
In addition to the four operators +, -, * and /, C-Sharp
also provides the remainder or modulo operator
represented by the symbol %.
The modulo operator returns the remainder when an
integer is divided by another.
Using % Operator
//Using the modulo operator
using System;
class use_modulo
{
public static void Main()
{
int var1=11,var2=5,var3;
var3=var1%var2;
Console.WriteLine("Remainder is{0}",var3);
}
}
Output of the above code is:
Remainder is1
Example - Arithmetic Assignment
Operators
Arithmetic assignment operators combine an
arithmetic operator (+,-,*,/,%) and the
assignment operator (=).
//Using the arithmetic assignment operator
using System;
class arithmetic_assignment
public static void Main()
int var1=10,var2=5;
Example - Arithmetic Assignment
Operators..
var1+=var2;
Console.WriteLine("Value in var1 is
{0}", var1);
}
}
Output of the code is:
Value in var1 is 15
Relational Operators
Relational operators are used to compare two values.
The result of the comparison is either true or false.
Some of the relational operators are:
Operator Meaning
> Greater than
< Less than
== Equal to
!= Not equal to
>= Greater than or equal to
<= Less than or equal to
Logical Operators
Logical operators are used to combine two or more
conditions in expressions.
The conditional logical operators are also known as
short-circuit logical operators.
Examples of logical operators are:
Operator Meaning
&& Evaluates to true only if both conditions are
satisfied
|| Evaluates to true if any one of the condition is
satisfied
! Evaluates to true if condition is false and vice
versa
Operator Precedence and
Associativity
Operator precedence represents the order of execution
of the different operations in a given expression.
The associativity of the operators represents the
direction from where the execution begins.
Operators can have:
Right associativity or
Left associativity
Conditional / Ternary Operator
It operates on three operands
using System;
class Hello
{ public static void Main()
{
int var1=20,var2;
var2=(var1>10)?30:40;
}
}
Unary Operators
The unary operators operate on a single operand.
Examples of unary operators are:
Increment operator (++) – increments the value of a
variable by one.
Decrement operator (--) – decrements the value of a
variable by one.
Prefix and Postfix Notations
The increment and decrement
operators can be used in two ways:
Prefix – where the operator precedes the
variable:
++Var;
Postfix – where the operator follows the
variable
Var++;
Example 1 : Prefix and Postfix
Notations
using System;
class prefix
{
public static void Main()
{
int var1,var2;
var1=10;
var2=++var1;
Console.WriteLine("Var1:{0}",var1);
Console.WriteLine("Var2:{0}",var2);
}
}
Output of the code is:
Var1:11
Var2:11
Example 2 : Prefix and Postfix
Notations
using System;
class postfix
{
public static void Main()
{
int var1,var2;
var1=10;
var2=var1++;
Console.WriteLine("Var1:{0}",var1);
Console.WriteLine("Var2:{0}",var2);
}
}
Output of the code is:
Var1:11
Var2:10
Variables & Expressions - Flashback
Variables
DataTypes
Operators
Arithmetic
Relational
Logical
Conditional / Ternary Operators
Unary Operators
Session Ends
Exercise
Relax