Tuesday, January 17, 2012

Iteration Statements

You can create loops by using the iteration statements. Iteration statements cause embedded statements to be executed a number of times, subject to the loop-termination criteria. These statements are executed in order, except when a jump statement is encountered.

The following keywords are used in iteration statements:
-do
-for
-foreach
-in
-while

For Iterations
The for loop executes a statement or a block of statements repeatedly until a specified expression evaluates to false.

eg:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace forloop
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);

}
Console.ReadLine();
}
}
}

Output :
0
1
2
3
4
5
6
7
8
9

You can break out of the loop by using the break keyword, or step to the next iteration in the loop by using the continue keyword.
You also can exit the loop by using a goto, return, or throw statement.

Foreach,in statements
The foreach statement repeats a group of embedded statements for each element in an array or an object collection that implements the System.Collections.IEnumerable or System.Collections.Generic.IEnumerable(Of T) interface.

eg:
class ForEachTest
{
static void Main(string[] args)
{
int[] fibarray = new int[] { 0, 1, 1, 2, 3, 5, 8, 13 };
foreach (int i in fibarray)
{
System.Console.WriteLine(i);
}
}
}
/*
Output:
0
1
1
2
3
5
8
13
*/

While statement
The while statement executes a statement or a block of statements until a specified expression evaluates to false.

eg:
class WhileTest
{
static void Main()
{
int n = 1;
while (n < 6)
{
Console.WriteLine("Current value of n is {0}", n);
n++;
}
console.ReadLine();
}
}

Output:
Current value of n is 1
Current value of n is 2
Current value of n is 3
Current value of n is 4
Current value of n is 5


do statement
The do statement executes a statement or a block of statements repeatedly until a specified expression evaluates to false. The body of the loop must be enclosed in braces, {}, unless it consists of a single statement. In that case, the braces are optional.

eg:
namespace dowhile
{
class Program
{
static void Main(string[] args)
{
int x = 0;
do
{

Console.WriteLine(x);
x++;
}
while (x < 5);
Console.ReadLine();
}
}
}
Output
0
1
2
3
4

Saturday, January 14, 2012

Operators ,Expressions and Statements

Operators ,Expressions and Statements

==> Statements - are complete sentences in C#
==> Statements are made up of one or more expressions .
==>Expressions contains operators and operands.

eg 1. Console.writeline("Type some numbers");

In the above example,
1. Console.writeline("Type some "numbers"); -whole thing is called expression.
2. Console and "type some numbers" are operands .

eg 2. a= b+5;
==> here "b+5" is the expression.
==> b is the operand and "+" is the operator.

eg 3. if(a==3)
==> if(a==3)- the whole thing is statement.
==> (a=3)is the expression

eg 4. myString = "john" + myLastName
==> "john" + myLastName is the expression .
==> john and myLastName are the operands and + is the operator.


Operands - can be objects , variables, or literal strings.
Operator - can be addition (+),concatenation (+), assignment (=), equality(==).

==>The operator for method invocation is (). This is what we use to pass our arguments to the parameters of the given method .
eg: Console.WriteLine(" hai "); and Console.ReadLine();

==> Dot operator(.) - which is for member access,that is , to access a method which is a member of a class .
eg: Console.WriteLine("hai"); -here writeline is the method and console is a class.

==>Declaration Statement
eg: int x;

==> Declaration Statement with initialization
eg: int x=3 ;

==> Expression statement
eg: myString = "john" + myLastName

==>Selection(Decision) Statement
eg: if(a>b)c = "myName"; - that if "a>b" then c is equal to "myName"

==>post-increment (x++),post-decrement (x--), pre-increment (++x) and pre-decrement (--x) operators
eg: suppose that int x= 0; then apply ++x,--x, x-- and x++

++x ==> 1 , --x ==> 0 , x-- ==> 0 and x++ ==> 1.

==>AND(&&) and OR(||) operators - These two are known as Logical Operators.
eg:1. if ( isTrue == false && isFalse == false )
{
// code block
}
You use the AND operator when you want to check more than one value at once. So in the example above, you're checking if both values are false. If and ONLY if both of your conditions are met ,then the code block get executed.

eg;2. if ( isTrue == false || isFalse == false )
{
// code block
}
If just one of our condition is false, then the code block will get executed.



Operators with their precedence and Associativity





Naming Conventions
A consistent naming pattern is one of the most important elements of predictability and discoverability in a managed class library. Widespread use and understanding of these naming guidelines should eliminate unclear code and make it easier for developers to understand shared code.

There are three types of capitalization styles defined:

1. Camel Case - The first letter of an identifier is lowercase and the first letter of each subsequent concatenated word is capitalized.
eg: myFirstName, numberOfDays etc.
Most programmers use this . Its a way to easily read something.

2. Pascal Case - The first letter in the identifier and the first letter of each subsequent concatenated word are capitalized.
eg; DataType , DataSet etc.

3. Uppercase - all letters are capitalized.
eg; ADO, ID etc.

Friday, January 13, 2012

selection(conditional) statements

A selection statement causes the program control to be transferred to a specific flow based upon whether a certain condition is true or not.

The following keywords are used in selection statements:

if
else
switch
case
default

The if Statement
When the condition evaluates to a boolean true, a block of code for that true condition will execute.

It takes the following form:
if (expression)
statement1
[else
statement2]

where:

expression
An expression that can be implicitly converted to bool or a type that contains overloading of the true and false operators.
statement1
The embedded statement(s) to be executed if expression is true.
statement2
The embedded statement(s) to be executed if expression is false.

eg:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ifdecisionstmt
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please type your lucky number");
string number = Console.ReadLine();
if(number=="1")
{
Console.WriteLine("you have won a car");

}
else if(number=="2")
{
Console.WriteLine("you have won a boat");

}
else if(number=="3")
{
Console.WriteLine("you have won a van");

}
else
{
Console.WriteLine("sorry, try next time");

}

Console.ReadLine();
}
}
}

output
please type your lucky number
1
you have won a car

Switch Statement
The switch statement is a control statement that selects a switch section to execute from a list of candidates.

eg :
int caseSwitch = 1;
switch (caseSwitch)
{
case 1:
Console.WriteLine("Case 1");
break;
case 2:
Console.WriteLine("Case 2");
break;
default:
Console.WriteLine("Default case");
break;
}

Each case label specifies a constant value.Control is transferred to the switch section whose case label contains a constant value that matches the value of the switch expression, caseSwitch. If no case label contains a matching value, control is transferred to the default section.

==>no two case labels can contain the same constant value.

==>Execution of the statement list in the selected section begins with the first statement and proceeds through the statement list, typically until a jump statement
is reached, such as a break, goto case, return, or throw. At that point, control is transferred outside the switch statement or to another case label.

==>C# does not allow execution to continue from one switch section to the next.The following code causes an error.

switch (caseSwitch)
{
// The following switch section causes an error.
case 1:
Console.WriteLine("Case 1...");
// Add a break or other jump statement here.
case 2:
Console.WriteLine("... and/or Case 2");
break;
}

Inline conditional operator
Its a shortcut for writing a long code
eg;
string message = "";
if (x>y)
{
message = " car ;
]
else
{
message = "bus";
}

shortcut for this code is :
string message =(x>y)? "car" : "bus" ;

this is called in line conditional operators . Here we are trying to write code in few lines.

Variable Declaration

Variables are named "buckets" containing values in your computers memory .You can create buckets just the right size using dates and times,for strings,etc.

C# consist of the Boolean type and three numeric types - Integrals, Floating Point, Decimal, and Strings.
Integrals"- refers to the classification of types that include sbyte, byte, short, ushort, int, uint, long, ulong, and char.

Floating Point- refers to the float and double types.

String type - represents a string of characters.

Integral types

They are whole numbers, either signed or unsigned, and the char type(16 bits). The char type is a Unicode character

How to declare an integer

eg:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace declarevariable
{
class Program
{
static void Main(string[] args)
{
int x, y;
x = 6;
y = x + 4;
Console.WriteLine(y);
Console.ReadLine();
}
}
}
Output is 10.

Boolean Type
Boolean types are declared using the keyword, bool. They have two values: true or false.
eg: using System;

class Booleans
{
public static void Main()
{
bool white = true;
bool black = false;

Console.WriteLine("It is {0) that jasmine is white in color", white);
Console.WriteLine("The above statement is not {0}.", black);
}
}
output - It is true that jasmine is white in color
The above statement is not false.

Floating Point and Decimal Types

C# floating point type is either a float or double. They are used any time you need to represent a real number



The string Type - A string is a sequence of text characters.

C# Operators

C# provides a large set of operators, which are symbols that specify which operations to perform in an expression.



Left associativity means that operations are evaluated from left to right. Right associativity mean all operations occur from right to left, such as assignment operators where everything to the right is evaluated before the result is placed into the variable on the left.

Friday, January 6, 2012

Console application

Console applications means Word, Excel, notepad , etc. User typically interacts with a console application using only a keyboard and display screen.

First C# program

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

namespace helloworld
{
class Strong
{
static void Main(string[] args)
{
Console.WriteLine("hello world");
Console.ReadLine();

}
}
}
==>c# is case sensitive

==> whenever we create a project the IDE automatically add some common namespaces with the help of "using" statement .
that is, using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Namespace acts like a last name or surname to a class . that is to call this particular program we can use " hello world.strong" and thus it avoids confusion or ambiguity and organize classes.
Namespace will be whatever your project name is.
Namespace contains a group of code that can be called by c# programs.

==>static void main(string[] args)
In the case of a simple console application main() method is the first method to be executed. main function typically has access to the command arguments given to the program when it was executed.
main() is often called as "entry point". Every method must have a return type ,here it is void which means main() does not return a value.
Using static, main() can run on its own and it does not need an object for reference.

==> Every method also has a parameter list.

==> console.writeLine allows what we want to write in console window. console is a class in system namespace and writeLine is a method in console class. WriteLine also called as a method parameter because we are writing a string in ("") ,that is , (" hello world") for our console window.
ReadLine() accept no parameters.

==> classes and methods begin with "{" and ends with "}". Any statement within { } defines a block.