Module 3 - Variables & Expressions
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
{
//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);
using System;
class arithmetic_assignment
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
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
Console.WriteLine("Var1:{0}",var1);
Console.WriteLine("Var2:{0}",var2);
}
}
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);
}
}
Var1:11
Var2:10
Variables & Expressions - Flashback
Variables
DataTypes
Operators
Arithmetic
Relational
Logical
Conditional / Ternary Operators
Unary Operators
Session Ends
Exercise
Relax