Saturday, November 7, 2009

Control statement sample Programs


Switch statement
First we are going to see a small porgram for switch case:

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

namespace conditional_statement
{
class Program
{
static void Main(string[] args)
{
int a = 10;
int b = 5;


Console.Write("Please enter your option: + or - ");
string str = Console.ReadLine();
switch (str)
{
case "+":
int c = a + b;
Console.WriteLine("the sum is " + c);
break;
case "-":
int d = a - b;
Console.WriteLine("the sum is " + d);
break;
default:
string stri = "not recognized";
Console.WriteLine(stri);
break;
}
Console.ReadLine();


}
}
}


Now we can see about

Console.ReadLine(); and Console.WriteLine();



Console.ReadLine(), is used to read the data from the console i.e, our peogram will get the input from the keyboard through console.


Here console is the predefined class and ReadLine is the predefined method in the Console class.
If you are a C programmer you can compare it with scanf and if you are a C++ programmer you can assume like cin>>
Console.WriteLine(), is used to print the out put in console WriteLine is the method inside Console class.
We can compare this with printf in c programming or cout<< in c++.

No comments:

Post a Comment