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
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
using System;class Fund{public static void Main(){int x1 = 50;int x2 = 100;if(x1>x2){Console.WriteLine(“X1 is greater”);elseConsole.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
switch(expression){case 10: Number is 10;breakcase 20: Number is 20;breakcase 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
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:
Usage
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
using System;class Forexample{public static void Main(){for(int I = 1; I<=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:
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
using System;class Whileexample{public static void Main(){int I = 1;while(I <=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
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:
Listing 8
using System;class Whileexample{public static void Main(){do{int I = 1;}while(I<=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
using System;using System.Collections;class Foreachdemo {public ArrayList numbers;Foreachdemo() {numbers = new ArrayList();numbers.Add("One");numbers.Add("Two");numbers.Add("Three");numbers.Add("Four");numbers.Add("Five");}public static void Main() {Foreachdemo fed = new Foreachdemo();foreach(string num in fed.numbers) {Console.WriteLine("{0}",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
using System;class Whileexample{public static void Main(){for(int I = 1; I<=10; I++)
{Console.WriteLine(I);if(I==5){break;}}
Thanks alot … it seems to be pretty clear and i would need some more example if possible.