if-else constructor
if-else constructor is used to check conditions using. if-else will be always having two conditions True or False.
Let's see a simple if-else constructor.
--------------------------------------
if(Condition)
{
// true part;
}
else
{
// false part;
}
---------------------------------------
In the above set of code we are using an if-else constructor to check a condition. Now if the Condition we have typed inside the if is TRUE it will get inside the True part of the constructor and if the condition is false it will enter inside the false part. The CONDITION may Contain operators like Arithmetic, Relational and Logical for checking different types of conditions.
Now, Lets check a real problem with if-else....
I have two integer variables nameley iNum1 and iNum2. I am going to check which number is bigger.
----------------------------------------
//Assigning values for the two variables
iNum1=10;
iNum2=20;
//Starting the condition
if(iNum1>iNum2)
{
Console.WriteLine("The condition is TRUE");
}
else
{
Console.WriteLine("The condition is FALSE");
}
----------------------------------------
Here iNum1 is assigned a value of 10 and iNum2 is assigned a value of 20. Now when the program gets executed the condition will be returning false because iNum1 variable is smaller than iNum2.
So, it will print "The condition is FALSE" on the screen.
Like this if the condition gets true it will enter into the true part ie the "if" body of the program and if condition gets false it enters into the else part of the Program.
We can also have multiple nested if conditions on a single if-else constructor. Let's check another example of checking which number is bigger out of three numbers.
I have three variables namely iNum1,iNum2,iNum3.
------------------------------------------
if(iNum1>iNum2 && iNum1>iNum3)
{
Console.WriteLine("iNum1 is the biggest number");
}
else if(iNum2>iNum1 && iNum2>iNum3)
{
Console.WriteLine("iNum2 is the biggest number");
}
else
{
Console.WriteLine("iNum3 is the biggest number");
}
------------------------------------------
Here in the above code we are using Relational and Logical operators. When we have multiple conditions like this we can use operators as shown above.
We have actually three conditions here so we can use Nested if's like this. When the if condition gets false it will enter the else if condition and when and only else if condition gets false it will enter the else condition of the program.
This is how we use if-else constructor in programs.
-------------------------------------------------------------
switch-case conditions.
switch case conditional statements are used to check conditions according to the incoming values to the variables. Here we will switch a variable and inside the cases we will check for the conditions.......
Let's see and example for this...
--------------------------------------------------------------
switch(iMonth)
{
case 1:
{
Console.WriteLine("January");
break;
}
case 2:
{
Console.WriteLine("February");
break;
}
case 3:
{
Console.WriteLine("March");
break;
}
default:
{
Console.WriteLine("Please enter a Valid number");
break;
}
}
------------------------------------------------------------
Here in the above code we are switching a variable inside the switch body. Now the switch will check for cases with the value inside the switch variable. If it matches any of the value inside any case it will get into the Case body.
If we pass a value 2 into the variable iMonth the switch will get into the case 2: and print "February".
Since we are using integer Datatype we can straight away write the numbers in the cases with a single space seperated.
The "default" statement gets printed in the case if a number(value) which is not the case conditions is assigned to the switch variable.
Now if you are passing a Character Datatype you must write the cases like this...
--------
case 'A':
{
//statements
break;
}
case 'C':
{
//statements
break;
}
---------
and in the case of strings you can write the cases like this....
---------
case "C#.net"
{
Console.WriteLine("Windows programming");
break;
}
case "ASP.net"
{
Console.WriteLine("Web Programming");
}
---------
you must have checked "break" statements after each case and default statements.
We must use break statements inside this just because it may jump into next Case. In C# break statements are compulsory and if we dont use break statements it will return an error.
We can have multiple cases in the switch statement but only a single default statement in the switch statement....
That's all about switch case and If else conditions.........
Tuesday, May 20, 2008
Tuesday, March 18, 2008
What are operators?
Operators are used to do perform some operations on variables.
We have four types of operators in C#.
Arithmetic Operators :-
Arithmetic Operators are used to do mathematical operations
The basic arithmetic Operators are
+ for addition
- for subtraction
* multiplication
/ division
% modulo operator(This operator is used to return the Remainder of two numbers).
Relational Operators:-
Relational operators are mainly used for checking conditions between two or more variables.
The relational operators we use in C# are..
> Greater than(Checks which number is greater)
<>= Greater than or equal to(Checks whether the given number is greater than or equal to the second number)
<= Less than or equal to(Checks whether the given number is less than or equal to the second number) == Equal to(Checks whether the second number is equal to the first number) != not equal to(Checks whether the second number is not equal to the first number) Logical Operators :
Logical operators are used to check logical conditions with a block of two or more relations
Logical operators we use in C# are AND, OR and NOT.
eg:-
((iNum1>iNum2) && (iNum1!=0))
In the above example we are checking whether the first variable is greater than the second variable AND iNum1 is not equal to ZERO. Here if both the conditions are true only the condition will execute.
How Logical operators work.
AND(&&)
&&(AND) operator becomes true only if all the conditions are true. If any of the conditions are false && operator returns false.
OR(||)
||(OR) operator becomes false only if all conditions are false. If any of the condition becomes true || operator becomes true.
NOT(!)
!(NOT) operator becomes true if the condition is false and false if the condition is true.
Unary Operators
Unary operators are used to add or subtract a variable by 1.
We have mainly two types of unary operators
Increment and
Decrement.
Increment operator is represented by ++ and decrement operator is represeted by --.
Increment operator adds a variable by one and decrement operator subracts a variable value by
one.
i++, i--.
Unary operators can be used in two ways POSTFIX and PREFIX.
In a postfix notation the unary operator will be placed after the variable(i++)
In a prefix notation the unary operator will be placed before the variable(++i)
Assignment Operator
An assignment operator is used to assign a value to a variable. An = (equal to) symbol is known as an assignment operator.
int i=10;
means assigning the integer variable i a vlaue of 10.
We have four types of operators in C#.
- Arithmetic Operators,
- Relational operators ,
- Logical operators and
- Unary operators.
Arithmetic Operators :-
Arithmetic Operators are used to do mathematical operations
The basic arithmetic Operators are
+ for addition
- for subtraction
* multiplication
/ division
% modulo operator(This operator is used to return the Remainder of two numbers).
Relational Operators:-
Relational operators are mainly used for checking conditions between two or more variables.
The relational operators we use in C# are..
> Greater than(Checks which number is greater)
<>= Greater than or equal to(Checks whether the given number is greater than or equal to the second number)
<= Less than or equal to(Checks whether the given number is less than or equal to the second number) == Equal to(Checks whether the second number is equal to the first number) != not equal to(Checks whether the second number is not equal to the first number) Logical Operators :
Logical operators are used to check logical conditions with a block of two or more relations
Logical operators we use in C# are AND, OR and NOT.
- AND operator is represented with && (two ampersand)
- OR operator is represented with || (two pipes) and
- NOT operator is represented with !(Exclamation).
eg:-
((iNum1>iNum2) && (iNum1!=0))
In the above example we are checking whether the first variable is greater than the second variable AND iNum1 is not equal to ZERO. Here if both the conditions are true only the condition will execute.
How Logical operators work.
AND(&&)
&&(AND) operator becomes true only if all the conditions are true. If any of the conditions are false && operator returns false.
OR(||)
||(OR) operator becomes false only if all conditions are false. If any of the condition becomes true || operator becomes true.
NOT(!)
!(NOT) operator becomes true if the condition is false and false if the condition is true.
Unary Operators
Unary operators are used to add or subtract a variable by 1.
We have mainly two types of unary operators
Increment and
Decrement.
Increment operator is represented by ++ and decrement operator is represeted by --.
Increment operator adds a variable by one and decrement operator subracts a variable value by
one.
i++, i--.
Unary operators can be used in two ways POSTFIX and PREFIX.
In a postfix notation the unary operator will be placed after the variable(i++)
In a prefix notation the unary operator will be placed before the variable(++i)
Assignment Operator
An assignment operator is used to assign a value to a variable. An = (equal to) symbol is known as an assignment operator.
int i=10;
means assigning the integer variable i a vlaue of 10.
What is a Variable?
Variable is a name given to a memory location.
Variable will be having a memory address value
For eg:-
int iNum1
char cName
float fNum etc..
Here, int,char,float represents the Data type to be used and iNum1,cName,fNum are the variable names given for those DataTypes.
Rules For Declaring a Variables :-
Variable will be having a memory address value
For eg:-
int iNum1
char cName
float fNum etc..
Here, int,char,float represents the Data type to be used and iNum1,cName,fNum are the variable names given for those DataTypes.
Rules For Declaring a Variables :-
- A Variable name Should Start with an Alphabet
- Key Words representing the program should not be used as variable name.
- for eg: numeric, int, console etc.. should not be used.
- Special characters except an underscore( _ ) are not allowed
- Variable names should not start with a number.
Friday, March 7, 2008
Data Types in C#
As of now we have studied how to start program using C#.
Now we will check some other programs in C#.
What do you mean by a Data Type?
In simple words Data Type means the type of data. The data may be mainly of three types. They are
Integer, Character and Float. These are also called base data types.
Integer are used to store integers or numbers without decimals.
eg:- 1,123,1452,9999 etc...
Characters are used to accept letters. Basically a character Data type can accept only one byte. So, it will store only one Letter.
eg:- a,A,b,c,d etc....
If we write -AB- it will return an error...
Float is used to accept Floating numbers or numbers with decimals..
eg:- 1.8F,21.3F etc
Some data types are :-
Name. . . . . Description
byte . . . . .. . 8-bits '
sbyte . . . .. . 8-bits
int. . . . . . . .. 32-bit
uint. . . . . . .. 32 bit
short. . . . . ..16-bit
ushort . . . .. 16-bit
long. . . . . . .. 64-bit
ulong. . . . . .. 64-bit
float . . . . . .. 32-bit
double. . . . .. 64-bit
decimal . . . . 96-bit
bool . . . . . . . true and false
char. . . . . . . 16-bit
Now we will check some other programs in C#.
What do you mean by a Data Type?
In simple words Data Type means the type of data. The data may be mainly of three types. They are
Integer, Character and Float. These are also called base data types.
Integer are used to store integers or numbers without decimals.
eg:- 1,123,1452,9999 etc...
Characters are used to accept letters. Basically a character Data type can accept only one byte. So, it will store only one Letter.
eg:- a,A,b,c,d etc....
If we write -AB- it will return an error...
Float is used to accept Floating numbers or numbers with decimals..
eg:- 1.8F,21.3F etc
Some data types are :-
Name. . . . . Description
byte . . . . .. . 8-bits '
sbyte . . . .. . 8-bits
int. . . . . . . .. 32-bit
uint. . . . . . .. 32 bit
short. . . . . ..16-bit
ushort . . . .. 16-bit
long. . . . . . .. 64-bit
ulong. . . . . .. 64-bit
float . . . . . .. 32-bit
double. . . . .. 64-bit
decimal . . . . 96-bit
bool . . . . . . . true and false
char. . . . . . . 16-bit
Tuesday, March 4, 2008
First Program in C#
So, we are going to start with C#. C# is an Object Oriented Language developed by Microsoft Corporation for working with .NET platform. For working with C# you must have any version of Visual Studio installed on your Computer. For the latest case you will get Light Components like Microsoft C# Express Edition to Download from Microsoft which is absolutely free.
Now we can see a simple program in C#.
-------------------------------------------------
using System;
namespace smilu
{
class cHello
{
public static void Main(string[] args)
{
Console.Write("Hello World");
}
}
}
--------------------------------------------------------
I know that if you are a fresher to C#; you may be wondering seeing the Code. There is nothing in much in the above code....
Let's be now familiar with what is written in the above code.
In the above code go from the begining...
The first line is
using System;
The using statement is used to add a NAMESPACE into this program.
Namespaces are collection of Classes. These Namespaces are used to avoid Class Name Conflicts. ie, there may be situations where we must have two or more CLASSes with the same Name. So, inside a program, it will not allow to create two classes with the same name. So there exists a problem. So to avoid these types of conflicts we use Namespaces. So we create a namespace and add the classes inside the namespace and we create the other class with the same name in another namespace.
In the above program we have added System namespace into this program. The System namespace contains CLASSes for implementing base datatypes.
Now the second step
namespace smilu
This step is used to create a User Defined namespace which contain classes. Here we have created a Namespace in the name of "smilu".
Then you must have noticed curly brackets throughout this program. These are the Bodies of Namespace, Classes and Functions.
The Third step
class cHello
As I have said in the earlier topics that C# is an Object Oriented Language. So an Object Oriented Language(OOL) must have a class. Without class we cannot say a program is purely Object Oriented(refer). So, in this step we are creating a CLASS with name "cHello".
We will be writing rest of the Codes inside the CLASS
The Fourth Step
public static void Main(string[] args)
or
public static void Main()
This is the most important Function of a Program. Without the Main() function a program will not Execute. We write the Main() function in C# like this. All the things written there are not necessary. We will check each keyword in the Declaration.
public:
public written in the function refers it Access to others.It is known as "Access Specifier" ie we have Given a public access to the Main function,so anywhere within the program we can access this function.
Static:
C# is an OOL. So member variables and member functions can only be accessed through objects. Here in the case of Main it is the heart of a program. When we execute a program the first thing which gets executed is the Main() function. This is the first class so Object for this class can only be created inside the Main() function. So we must have to get the program started by executing the Main() function. That is why we declare a Static keyword to Main() function. So it is not necessary to create object for the class.
void:
void means NOTHING.... void represents the return type of the Main() function. ie, what the main should return to the program. Here in this case we doesn't need anything to be returned.. so, we have given the return type as void.
Main(string[] args) :
string[] args is used to store Command Line Arguments(if any). Command Line Arguments are arguments which we pass to a program while its execution with its Name.
The Fifth Step
Console.Write("Hello World");
Here we are creating Console application ie, we create a DOS based program. System.Console is a class which contains definitions for Write(), Read() etc... We mainly use
Write(); For writing in the same line(output)
WriteLine() This is used to Write in a string and set a break to the Line after that.
Read() Read a single line
ReadLine() Reading the strings.
Here in the above code inside the write function I have written "Hello World". The text inside the double quotes("") represenets the text to be printed as the output. What all we are write inside the "" of the Write() or WriteLine() functions will be printed as that itself exept some Escape charactors like...
Commonly used escape charactors are...
\n - For new Line
\t - For a Tab Space
\b - For a BackSpace etc...
NOTE:- C# is a case sensitive Language. So it is necessary that you write codes in appropriate cases itself as written above.
After you type the program save the file with a Filename with extension ".cs"(eg:- Hello.cs).
For executing the program take the Microsoft Visual Studio 2005 Command Prompt from
START->PROGRAMS->MICROSOFT VISUAL STUDIO 2005->MICROSOFT VISUAL STUDIO TOOLS->MICROSOFT VISUAL STUDIO 2005 COMMAND PROMPT.
Now navigate to the Folder you have saved the File.
Compile the Code.
For Comiling you have to do like this .. if the file name is Hello.cs
In the COmmand prompt write " csc Hello.cs " and Press enter
If the compilation returns Successfully write the File name and press Enter
like
" Hello " and Enter.
You will get an output as
Hello World
Now we can see a simple program in C#.
-------------------------------------------------
using System;
namespace smilu
{
class cHello
{
public static void Main(string[] args)
{
Console.Write("Hello World");
}
}
}
--------------------------------------------------------
I know that if you are a fresher to C#; you may be wondering seeing the Code. There is nothing in much in the above code....
Let's be now familiar with what is written in the above code.
In the above code go from the begining...
The first line is
using System;
The using statement is used to add a NAMESPACE into this program.
Namespaces are collection of Classes. These Namespaces are used to avoid Class Name Conflicts. ie, there may be situations where we must have two or more CLASSes with the same Name. So, inside a program, it will not allow to create two classes with the same name. So there exists a problem. So to avoid these types of conflicts we use Namespaces. So we create a namespace and add the classes inside the namespace and we create the other class with the same name in another namespace.
In the above program we have added System namespace into this program. The System namespace contains CLASSes for implementing base datatypes.
Now the second step
namespace smilu
This step is used to create a User Defined namespace which contain classes. Here we have created a Namespace in the name of "smilu".
Then you must have noticed curly brackets throughout this program. These are the Bodies of Namespace, Classes and Functions.
The Third step
class cHello
As I have said in the earlier topics that C# is an Object Oriented Language. So an Object Oriented Language(OOL) must have a class. Without class we cannot say a program is purely Object Oriented(refer). So, in this step we are creating a CLASS with name "cHello".
We will be writing rest of the Codes inside the CLASS
The Fourth Step
public static void Main(string[] args)
or
public static void Main()
This is the most important Function of a Program. Without the Main() function a program will not Execute. We write the Main() function in C# like this. All the things written there are not necessary. We will check each keyword in the Declaration.
public:
public written in the function refers it Access to others.It is known as "Access Specifier" ie we have Given a public access to the Main function,so anywhere within the program we can access this function.
Static:
C# is an OOL. So member variables and member functions can only be accessed through objects. Here in the case of Main it is the heart of a program. When we execute a program the first thing which gets executed is the Main() function. This is the first class so Object for this class can only be created inside the Main() function. So we must have to get the program started by executing the Main() function. That is why we declare a Static keyword to Main() function. So it is not necessary to create object for the class.
void:
void means NOTHING.... void represents the return type of the Main() function. ie, what the main should return to the program. Here in this case we doesn't need anything to be returned.. so, we have given the return type as void.
Main(string[] args) :
string[] args is used to store Command Line Arguments(if any). Command Line Arguments are arguments which we pass to a program while its execution with its Name.
The Fifth Step
Console.Write("Hello World");
Here we are creating Console application ie, we create a DOS based program. System.Console is a class which contains definitions for Write(), Read() etc... We mainly use
Write(); For writing in the same line(output)
WriteLine() This is used to Write in a string and set a break to the Line after that.
Read() Read a single line
ReadLine() Reading the strings.
Here in the above code inside the write function I have written "Hello World". The text inside the double quotes("") represenets the text to be printed as the output. What all we are write inside the "" of the Write() or WriteLine() functions will be printed as that itself exept some Escape charactors like...
Commonly used escape charactors are...
\n - For new Line
\t - For a Tab Space
\b - For a BackSpace etc...
NOTE:- C# is a case sensitive Language. So it is necessary that you write codes in appropriate cases itself as written above.
After you type the program save the file with a Filename with extension ".cs"(eg:- Hello.cs).
For executing the program take the Microsoft Visual Studio 2005 Command Prompt from
START->PROGRAMS->MICROSOFT VISUAL STUDIO 2005->MICROSOFT VISUAL STUDIO TOOLS->MICROSOFT VISUAL STUDIO 2005 COMMAND PROMPT.
Now navigate to the Folder you have saved the File.
Compile the Code.
For Comiling you have to do like this .. if the file name is Hello.cs
In the COmmand prompt write " csc Hello.cs " and Press enter
If the compilation returns Successfully write the File name and press Enter
like
" Hello " and Enter.
You will get an output as
Hello World
Subscribe to:
Posts (Atom)