Understanding Programming Constructs using C#

In this session, we will discuss about the commonly used programming constructs like if-else, Switch-case, for, while, do-while loops.

Let’s analyze each one of them in detail

Conditional Statements

If – else

This is one of the popular decision making statements, which is similar to that of its C+= and Java counterparts. Its usage is given below

Usage

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">if(Condition) { 
Statements 
} 
else  
{ 
Statements 
}

If the condition satisfies, the statements inside the if block will be executed, otherwise statements inside the else part will execute as shown in Listing 4:

Listing 4

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">using System; 
class Fund 
{ 
public static void Main() 
{ 
int x1 = 50; 
int x2 = 100; 
if(x1&gt;x2) 
{ 
Console.WriteLine(“X1 is greater”); 
else 
Console.WriteLine(“X2 is greater”)  
} 
} 
}
Switch – Case

This is also one of the decision making statement which is regarded as an alternative to if – else. Its usage is given below

Usage

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">switch(expression)   
 { 
case 10: Number is 10;break 
case 20: Number is 20;break 
case else: Illegal Entry;break 
} 

When the expression matches with a case value, the corresponding statements would be printed. Listing 5 explains the usage of Switch – Case statement.

Listing 5

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">using System; 

class Switchexample 
{ 
public static void Main() 
{ 
int wday = 2; 
switch(wday) 
{ 
case 1: Console.WriteLine(“Monday”);break; 
case 2: Console.WriteLine(“Tuesday”);break; 
} 
} 
} 

Looping Statements

For loop

This loop is used to iterate a value fixed number of times. Its usage is given below:

READ  Understanding Sealed and Abstract classes using C#

Usage

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">for(Initial Value, Condition, Incrementation / Decrementation) 
{ 
Statements 
} 

You have to specify Incrementation and Decrementation using ++ and – operators. Listing 6 examines the working of for loop. Here, numbers from 1 to 10 will be printed as output. 10 will also be printed because we have used <= operator. Try using only < operator and observe the result.

Listing 6

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">using System; 
class Forexample 
{ 
public static void Main() 
{ 
for(int I = 1; I<span style="color: #0000ff">&lt;</span>=10;I++) 
{ 
Console.WriteLine(I); 
} 
} 

While loop

This loop is similar to for loop, except that condition is specified first and is used to repeat a statement as long as a particular condition is true. The usage of while loop is given below:

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">while(Condition) 

{ 

Statements 

} 

If the condition is false, statements within the loop will not be executed. Listing 7 examines the working of while loop:

Listing 7

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">using System; 
class Whileexample 
{
public static void Main() 
{ 
int I = 1; 
while(I <span style="color: #0000ff">&lt;</span>=10) 
{ 
console.WriteLine(I); 
} 
} 

Do- While

This looping statement is also similar to that of the previous one but the condition is specified last. Its usage is given below:

Usage

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">do 
{ 
Statements 
}
while(Condition)

In this case, the condition is tested at the end of the loop. Hence the statements within the loop are executed at least once even if the condition is false. Let’s verify an example:

READ  Visual Studio 2015 Update 1 CTP Gets Better with New C# Interactive Window and Edit/Continue Support

Listing 8

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">using System; 
class Whileexample 
{ 
public static void Main() 
{ 
do 
{ 
int I = 1; 
} 
while(I<span style="color: #0000ff">&lt;</span>=10); 
Console.WriteLine(I); 
} 
} 

Foreach

If you had used Visual Basic then you would be familiar with foreach statement. C# also supports this statement. Listing 9 illustrates the usage of this statement:

Listing 9

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">using System; 
using System.Collections; 
class Foreachdemo   { 
public ArrayList numbers; 
Foreachdemo() { 
numbers = new ArrayList(); 
numbers.Add(&quot;One&quot;); 
numbers.Add(&quot;Two&quot;); 
numbers.Add(&quot;Three&quot;); 
numbers.Add(&quot;Four&quot;); 
numbers.Add(&quot;Five&quot;); 
} 
public static void Main() { 
Foreachdemo fed = new Foreachdemo(); 
foreach(string num in fed.numbers) { 
Console.WriteLine(&quot;{0}&quot;,num); 
} 
} 
} 

In the above example, an object of type ArrayList is created and values are added to it using the Add method of the ArrayList class. Try removing the second using directive and observe the result

Break Statement

This statement is used to terminate a loop abruptly. We have seen the usage of this statement while discussing decision-making statements. Listing 10 examines the working of this statement.

Listing 10 

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">using System; 
class Whileexample 
{ 
public static void Main() 
{ 
for(int I = 1; I<span style="color: #0000ff">&lt;</span>=10; I++) 
{ 
Console.WriteLine(I); 
if(I==5) 
{ 
break; 
} 
} 

One Response to "Understanding Programming Constructs using C#"

  1. tom   October 26, 2010 at 7:55 pm

    Thanks alot … it seems to be pretty clear and i would need some more example if possible.

    Reply

Leave a Reply

Your email address will not be published.