Tuesday, 25 March 2014

3: Statement, Expression and Operators in C#

Statements in C#

Statements are used for input, process and output task of a program.

Examples:

Console.WriteLine(“Statements in C Sharp”);
Int stateRange=5;
Mystate obj=new  Mystate();
String Name=Console.ReadLine();

Types of Statement:

There are four basic statements in C#.
  1. Selection Statement
  2. Iteration Statement
  3. Jump Statement
  4. Error/Exception Handling statement

1. Selection Statement

Selection statements uses if, else, else if, switch and case. These type of statements are use for taking decision.

2. Iteration Statement

Iteration statements uses do, for, foreach, in and while. These types of statement are used for repetition of block (block of code).

3. Jump Statement

Jump statement uses break, continue, default, goto, return and yield. These types of statement are used for transferring the flow from one block to the another.

4. Error/Exception
Error/Exception uses throw, try-catch, try-finally and try-catch-finally. These types of statement are used for creating custom exception, and catching run time error/exception.


Expression in C#

Expression in C# is use for manipulation of data.

Example:

(10*10)+ (12-2);
Expression always returns value.
Expression is a part of Statement.
int myStateExpression=(10*10)+(12-2);

Operators in C#

There are six types of operators in C#.
  1. Arithmetic operators
  2. Relational operators
  3. Logical operators
  4. Conditional operators
  5. Increment and decrement operators
  6. Assignment operators

Arithmetic operators

There are four operators use for performing arithmetic operations. +, -,*,/,%.
Example:

Relational operators

There are six operators use for comparison between two operands. And return true or false based on the condition.
== equal to, != not equal to, > greater than, < less than, >= greater than and equal to, <= less than and equal.


Example:

Logical operators

Logical operators are binary operator use to perform logical operations and return true or false (Boolean value).
Operators are & (AND operator), | (OR operator) and (Exclusive OR).

Example:

Conditional operators

There are two types of Conditional operators

Conditional AND (&&)
In conditional AND, if first expression true then it check next expression otherwise return false.


Conditional OR (||)
In Conditional OR, if first expression false then it check next expression otherwise if it finds true at first expression it does not check second expression and return true.
Conditional operators can be explain by following example.

Example:
string sName1 = "Ahmad";
string sName2 = "Faraz";

Console.WriteLine("Ahmad and Faraz are there = {0}",(sName1=="Ahmad" && sName2=="Faraz"));// return true because both expression true

Console.WriteLine("Ahmad and Faraz are there = {0}", (sName1 == "Waqas" && sName2 == "Faraz"));// return false because first expression false

Console.WriteLine("Ahmad and Faraz are there = {0}", (sName1 == "Ahmad" && sName2 == "Wasim"));// return false because second expression false

Console.WriteLine("Ahmad and Faraz are there = {0}", (sName1 == "Ahsan" && sName2 == "Wasim"));// return false because both expression false

Console.WriteLine("Ahmad or Faraz are there = {0}", (sName1 == "Ahmad" || sName2 == "Faraz"));// return true because first expression true, not see second

Console.WriteLine("Ahmad or Faraz are there = {0}", (sName1 == "Waqas" || sName2 == "Faraz"));// return true because second expression true

Console.WriteLine("Ahmad or Faraz are there = {0}", (sName1 == "Ahmad" || sName2 == "Wasim"));// return true because first expression true

Console.WriteLine("Ahmad or Faraz are there = {0}", (sName1 == "Ahsan" || sName2 == "Wasim"));// return true because first expression true


Increment/Decrement operators

Increment/Decrement operators are use for increment by one or decrement by one.
Denoted by ++, --. If ++/-- found before variable so it called pre-increment/pre decrement respectively.
If ++/-- found after variable so it called post-increment/post decrement respectively.

Example:
int age = 22;
Console.WriteLine("After one year my age is {0}",++age);//example of pre-increament
Console.WriteLine("last year my age was {0}", --age);//example of pre-decrement


Assignment Operators

Assignment operators are use to assign value from right side operand to the left side operand.
There are two types:
Simple assignment operator is denoted by “=”.
Name=”my Name”;
Compound assignment operators use “=” and arithmetic operator.
Like +=, -=, /=, *= etc.

For example:
int a = 10;
a += 10;
Console.WriteLine(a);
a -= 10;
Console.WriteLine(a);

Use all like above example for learning.



No comments:

Post a Comment