Visual Programming Lab Manual

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

VISUAL PROGRAMMING LAB MANUAL

LAB#2
Objectives:
Demonstrate the usage of Data types, Operators and Expression, Methods, Class

Theory:
In this lab students will be introduced with variable, data types, operator, class, method
and expression.

C# Variable

A variable is a name of memory location. It is used to store data. Its value can be
changed and it can be reused many times. It is a way to represent memory location
through symbol so that it can be easily identified. The basic variable type available in C#
can be categorized as:

Variable Type Example

Decimal types decimal

Boolean types True or false value, as assigned

Integral types int, char, byte, short, long

Floating point types float and double

Nullable types Nullable data types

Rules for defining Variables

 A variable can have alphabets, digits and underscore.

 A variable name can start with alphabet and underscore only. It can't start with
digit.
 No white space is allowed within variable name.
 A variable name must not be any reserved word or keyword e.g. char, float etc.

C# Data Types
VISUAL PROGRAMMING LAB MANUAL

A data type specifies the type of data that a variable can store such as integer, floating,
character etc. There are 3 types of data types in C# language.

Types Data Types

Value Data Types short, int, char, float, double etc

Reference Data Types String, Class, Object and Interface

Pointer Data Type Pointers

Value Data Type: Value type variables can be assigned a value directly. They are derived
from the class System.ValueType. The value types directly contain data. Some
examples are int, char, and float, which stores numbers, alphabets, and floating point
numbers, respectively. When you declare an int type, the system allocates memory to
store the value.

The following table lists the available value types in C#


Type Represents Range Default
Value

bool Boolean value True or False False

byte 8-bit unsigned integer 0 to 255 0

char 16-bit Unicode character U +0000 to U +ffff '\0'

decimal 128-bit precise decimal 0.0M


values with 28-29 (-7.9 x 1028 to 7.9 x 1028) / 100to 28
significant digits

Double 64-bit double-precision (+/-)5.0 x 10-324 to (+/-)1.7 x 10308 0.0D


floating point type

Float 32-bit single-precision -3.4 x 1038 to + 3.4 x 1038 0.0F


floating point type
VISUAL PROGRAMMING LAB MANUAL

Int 32-bit signed integer type -2,147,483,648 to 2,147,483,647 0

Long 64-bit signed integer type -9,223,372,036,854,775,808 to 0L


9,223,372,036,854,775,807

sbyte 8-bit signed integer type -128 to 127 0

Short 16-bit signed integer type -32,768 to 32,767 0

Uint 32-bit unsigned integer 0


0 to 4,294,967,295
type

Ulong 64-bit unsigned integer 0


0 to 18,446,744,073,709,551,615
type

Ushort 16-bit unsigned integer 0


0 to 65,535
type

using System;
using System.Collections.Generic;
using System.Linq;

using System.Text;

namespace VariableDeclarationExample

{
class Program
VISUAL PROGRAMMING LAB MANUAL

{
static void Main(string[] args)
{
//variable declaration
short a;
int b;
double c;
//variable initializatioin
a = 10;

b = 20;
c = 2015.65;
Console.WriteLine("The vale of a = {0}, b = {1}, c = {2}",a,b,c);
Console.ReadLine();

}
}
}

Output: The value of a = 10, b=20, c= 2015.65

To get the exact size of a type or a variable on a particular platform, you can use
the sizeof method. The expression sizeof(type) yields the storage size of the object or
type in bytes. Following is an example to get the size of int type on any machine –
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataTypeApplication
{
class Program
VISUAL PROGRAMMING LAB MANUAL

{
static void Main(string[] args)
{
Console.WriteLine("Size of int : {0} bytes",sizeof(int));
Console.WriteLine("Size of char: {0} bytes", sizeof(char));
Console.WriteLine("Size of double : {0} bytes", sizeof(double));
Console.WriteLine("Size of bool : {0} bytes", sizeof(bool));
Console.ReadLine();

}
}
}

Output:
Size of int : 4 bytes
Size of double : 8 bytes
Size of char : 2 bytes
Size of bool : 1 bytes

Reference Type
The reference types do not contain the actual data stored in a variable, but they contain
a reference to the variables.
In other words, they refer to a memory location. Using multiple variables, the reference
types can refer to a memory location. If the data in the memory location is changed by
one of the variables, the other variable automatically reflects this change in value.
Example of built-in reference types are: object, dynamic, and string.
Object Type
The Object Type is the ultimate base class for all data types in C# Common Type
System (CTS). Object is an alias for System.Object class. The object types can be
assigned values of any other types, value types, reference types, predefined or user-
defined types. However, before assigning values, it needs type conversion.
Note: When a value type is converted to object type, it is called boxing and on the other
hand, when an object type is converted to a value type, it is called unboxing.
Example:

object obj;
VISUAL PROGRAMMING LAB MANUAL

obj = 100; // this is boxing

Dynamic Type
You can store any type of value in the dynamic data type variable. Type checking for
these types of variables takes place at run-time.
Syntax for declaring a dynamic type is −

dynamic <variable_name> = value;


For example,

dynamic d = 20;
Dynamic types are similar to object types except that type checking for object type
variables takes place at compile time, whereas that for the dynamic type variables takes
place at run time.
Note: The constants refer to fixed values that the program may not alter during its
execution. These fixed values are also called literals.
Defining Constants
Constants are defined using the const keyword. Syntax for defining a constant is −

const <data_type> <constant_name> = value;

The following program demonstrates defining and using a constant in your program −
using System;
using System.Collections.Generic;
using System.Linq;

using System.Text;

namespace ConstantExample
{
class Program

{
VISUAL PROGRAMMING LAB MANUAL

static void Main(string[] args)


{
const double pi = 3.14159;
double r;

Console.WriteLine("Enter Radius: ");


r = Convert.ToDouble(Console.ReadLine());

double areaCircle = pi * r * r;
Console.WriteLine("Radius: {0}, Area: {1}", r, areaCircle);
Console.ReadLine();

}
}

}
String Type:
The String Type allows you to assign any string values to a variable. The string type is
an alias for the System.String class. It is derived from object type. The value for a string
type can be assigned using string literals in two forms: quoted and @quoted.

For example,

String str = "Hello";


A @quoted string literal looks as follows −

@"Hello";
Pointer Type
Pointer type variables store the memory address of another type. Pointers in C# have
the same capabilities as the pointers in C or C++.
Syntax for declaring a pointer type is −

type* identifier;
For example,
VISUAL PROGRAMMING LAB MANUAL

char* cptr;

int* iptr;

Type Casting:
Type conversion is converting one type of data to another type. It is also known as Type
Casting. In C#, type casting has two forms:
Implicit type conversion: These conversions are performed by C# in a type-safe manner.
For example, are conversions from smaller to larger integral types and conversions
from derived classes to base classes.
Explicit type conversion: These conversions are done explicitly by users using the pre-
defined functions. Explicit conversions require a cast operator.

using System;
using System.Collections.Generic;
using System.Linq;

using System.Text;

namespace TypeCastingExample
{
class Program
{
static void Main(string[] args)
{
double d = 542.56;

int i;

//Casting double to integer


VISUAL PROGRAMMING LAB MANUAL

i = (int)d;
Console.WriteLine("The vale of i is = {0}", i);
Console.ReadLine();

}
}
}

Output: The value of is = 542

Operators and Expressions:


An operator is a symbol that tells the compiler to perform specific mathematical or

logical manipulations. C# has rich set of built-in operators and provides the following

type of operators

 Arithmetic Operators [ +, -, *, /, %, ++, -- ]

 Relational Operators [==, !=, >, <, >=, <= ]

 Logical Operators [ &&, ||, !]

 Bitwise Operators [&, |, ^, ~, <<, >>]

 Assignment Operators [=, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=]
VISUAL PROGRAMMING LAB MANUAL

Review Questions/ Exercise:

1. Write a program that contains two variables of type int and perform all arithmetic
operations.

2. Write a program that performs all the logical operations on two variables.
VISUAL PROGRAMMING LAB MANUAL

3. What is operator precedence? Explain with suitable example program.


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.
For example, the precedence of multiplication (*) operator is higher than the precedence of
addition (+) operator. Therefore, operation involving multiplication is carried out before addition.
Take a look at the statement below.
int x = 4 + 3 * 5;
What will be the value of x after executing this statement?
The operand 3 is associated with + and *. As stated earlier, multiplication has a higher precedence
than addition. So, the operation 3 * 5 is carried out instead of 4 + 3. The value of variable x will be
19.
If addition would have a higher precedence, 4 + 3 would be evaluated first and the value of x would
be 35.

Name:
Roll #:
Date:

(Subject Teacher)

Remarks:

You might also like