Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

CSharp.NET Tutorial – Day 8


In previous article, we have discussed what are the Data Types available in C# like Integer types, Decimal Types, Boolean Types, Character Types, and Generic Types and also will discuss constant and readonly variables, Value Types, Reference Types, Implicitly typed Variables, what are Nullable Value Types, what is Boxing and UnBoxing, what are Operators available in C#, etc
Please find below the link for accessing the article
CSharp.NET Tutorial - Day 7 

Now, in this article we will discuss what are conditional Statements and types, what is Conditional Branching, what is Conditional Looping, what is ‘if’ statement, what is switch case, what is for loop, what is while loop, what is do while loop, what is foreach loop along with syntaxes and example programs, what is difference between array and collection, what are Jump Statements available in C# and syntaxes of goto, break, continue, and return statements along with some examples.

Conditional Statements
A Block of code, which gets executed based on a condition called conditional statement.  There are 2 types of conditional statements, which are as follows:
1)      Conditional Branching
2)      Conditional Looping

1) Conditional Branching
Conditional branching statements allow us to branch our code depending on whether certain conditions were met or not.  C# has two constructs for branching the code i.e. the ‘if’ statement, which allows us to test whether a specific condition is met and the ‘switch’ statement, which allows us to compare an expression with a number of different values.



Syntax for ‘if’ statement

if(<condition>)
<statements>;
else if(<condition>)
< statements >;
___________
else
< statements >;


Eg:
using System;
class IfEx
{
static void Main()
{
int x, y;
Console.Write ("Enter x value :");
x = int.Parse (Console.ReadLine ());
Console.Write ("Enter y value :");
y = int.Parse (Console.ReadLine ());
if (x > y)
Console.WriteLine ("x is greater");
else if (x < y)
Console.WriteLine ("y is greater");
else
Console.WriteLine ("both are equal");
}
}
‘ReadLine’ method of the ‘Console’ class reads the input given by the user in Runtime and Return the value in the form of string because return type of the method ReadLine is a string and it returns the value as string making use of the Parse method.  In this case, we should explicitly convert the value into the required type, where Parse can be called on any type to convert the value into the type on which the method was called.

Eg:
int x=int.Parse("100");
bool b=bool. Parse ("True");
double d=double. Parse ("324.344");

Note:  If we call the Parse method on a value that is not convertible into a specific type, we will obviously get an error.

Eg:
int y=int.Parse("10A2"); //Invalid

Syntax for ‘switch’ case
switch(<expression>)
{
case<value>
<statements>;
break;
_______
default
<statements>;
break;
}

Note:  It is mandatory to use ‘break’ statement for each and every case as well as default block also.


Eg:
using System;
class SwitchEx
{
static void Main( )
{
Console.Write ("Enter student no (1-3) :");
int sno=int.Parse("Console.ReadLine());
switch (sno)
{
case1:
Console.WriteLine ("student1");
break;
case2;
Console.WriteLine ("student2");
break;
case3;
Console.WriteLine ("student3");
break;
default:
Console.WriteLine ("Wrong student no");
break;
}
}
}

2) Conditional Loops
C# provides four different kind of loops which allows us to execute a block of code repeatedly until a certain condition is met.  Those are as below.

1)      for loop
2)      while loop
3)      do while loop
4)      foreach loop

Whatever loop we use that should follow the below requirements in common.

1)      Initialization --> it will set starting point of a loop
2)      Condition-> it will set the ending point of a loop
3)      Iteration-> it will take us to the next level of a loop either in forward or backward directions.


Syntax for ‘for’ loop

for(intializer;condition;iteration)
<statements>;


Eg:

for(int x=1;x<=100;x++)
Console.WriteLine(x);

Syntax for ‘while’ loop

while (condition)
<statements>;


Eg:
int x=1;
while(x<=100)
{
Console.WriteLine(x);
x++;
}

Syntax for ‘do while’ loop

int x=1;
do{
Console.WriteLine(x);
x++;
}while(x<=100);





Note:  A ‘do while’ loop can execute the loop for the first time without performing any conditional check so that the minimum number of times it gets executed will always be one.

Syntax for ‘foreach’ loop
It is specifically designed for processing the values of an array or a collection.

foreach (type var in coll||array)
{
<statements>;
}


Note:  The difference in between array and collection is an Array is a set of similar type values and it is static and/or fixed length, whereas a collection is a set of decimal type values and it is dynamic in nature.

Jump Statements
C# provides a number of statements which will allow us to jump immediately to another line in a program which are listed below.
1) goto
2) break
3) continue
4) return

1) goto: ‘goto’ allows us to jump directly to another specified line in a program which is indicated by a label.  Here label is an identifier followed by a colon.


Eg:
goto xxx:
Console.WriteLine ("Hello");
xxx;
Console.WriteLine ("Goto Called");

2) break: ‘break’ is used to exit from a case in a switch statement and also it is used to exit from for, foreach, while and do while loops, which will switch control to the statement immediately after end of the loop.



Eg:
for(int i=1;i<=100;i++)
{
Console.WriteLine (i);
if(i==50)
break;
}
Console.WriteLine ("End of the Loop");

3) continue: - ‘continue’ can be used only in a loop statement and we can use it skip the execution of statements present after it.


Eg:
for(int i=1;i<=100;i++)
{
if(i==7)
continue;
Console.WriteLine (i);
}
Console.WriteLine ("End of the Loop");

4) return:  ‘return’ is used to jump out of a method, which is currently in execution.  Please note that it is also capable to carry a value out of the method at that time.

Eg:
using System;
class RetEx
{
static void Main()
{
Console.Write ("Enter a Numeric value :");
int x=int.Parse(Console.ReadLine());
if(x==0)
return;
for(int i=1;i<=10;i++)
Console.WriteLine({0}*{1}={2};x,i,x*i);
}
}

Happy Learning .....

CSharp.NET Tutorial - Day 9


This post first appeared on Dot Net Programming (C#, Asp.Net, ADO.Net, WCF, WPF, Ajax, LINQ), please read the originial post: here

Share the post

CSharp.NET Tutorial – Day 8

×

Subscribe to Dot Net Programming (c#, Asp.net, Ado.net, Wcf, Wpf, Ajax, Linq)

Get updates delivered right to your inbox!

Thank you for your subscription

×