Statements in C#

The real nuts and bolts of a programming languages is that which controls the flow of a program called statements.

Types of Statements in C#

C# borrows most of its statements directly from C and C++, though there are some noteworthy additions and modifications.

  1. Expression & Control statements
  2. Labeled & goto statements

Control statements allow to branch the code depending on the certain condition against the value of an expression. There are many types of conditional statements.

  1. the if statement
  2. switch statement
  3. the for loop
  4. the while loop
  5. the do…while loop
  6. the foreach loop

C# provides number of statements which we can use to jump to another line in the program. i.e.

  1. the goto statement
  2. the break statement
  3. the continue statement
  4. the return statement

Statement lists and blocks

A statement list consists of one or more statements written in sequence, and a block permits multiple statements to be written in contexts where a single statement is expected. For instance, the example

using System;
class Test
{
 static void Main()
 { // begin block 1
  Console.WriteLine("Test.Main");
  { // begin block 2
   Console.WriteLine("Nested block");
  }
 }
}

shows two blocks. First is the Main function’s block and the other is the block
with the line Console.WriteLine(“Nested block”);

Labeled statements and goto statements

A labeled statement permits a statement to be prefixed by a label, and goto statements can be used to transfer control to a labeled statement. The example

using System;
class Test
{
static void Main() {
goto H;
W: Console.WriteLine("world");
return;
H: Console.Write("Hello, ");
goto W;
}
}

is a convoluted version of the “Hello, world” program. The first statement transfers control to the statement labeled H. The first part of the message is written and then the next statement transfers control to the statement labeled W. The rest of the message is written, and the method returns.

Expression & Control statements

An expression statement evaluates a given expression. The value computed by the expression, if any, is discarded. Not all expressions are permitted as statements. In particular, expressions such as x + and x == 1 that have no side effects, but merely compute a value (which will be discarded), are not permitted as statements.

The if statement

An if statement selects a statement for execution based on the value of a Boolean expression. An if statement may optionally include an else clause that executes if the Boolean expression is false.
The example

using System;
12 Copyright ? Microsoft Corporation 1999-2000. All Rights Reserved.
class Test
{
static void Main(string[] args) {
if (args.Length == 0)
Console.WriteLine("No arguments were provided");
else
Console.WriteLine("Arguments were provided");
}
}

shows a program that uses an if statement to write out two different messages depending on whether command line arguments were provided or not.

The switch statement

A switch statement executes the statements that are associated with the value of a given expression, or a default of statements if no match exists.
The example

using System;
class Test
{
static void Main(string[] args) {
switch (args.Length) {
case 0:
Console.WriteLine("No arguments were provided");
break;
case 1:
Console.WriteLine("One arguments was provided");
break;
default:
Console.WriteLine("{0} arguments were provided");
break;
}
}
}

switches on the number of arguments provided.

The while statement

A while statement conditionally executes a statement zero or more times ? as long as a Boolean test is true.

using System;
class Test
{
static int Find(int value, int[] arr) {
int i = 0;
while (arr[i] != value) {
if (++i > arr.Length)
throw new ArgumentException();
}
return i;
}
static void Main() {
Console.WriteLine(Find(3, new int[] {5, 4, 3, 2, 1}));
}
}

uses a while statement to find the first occurrence of a value in an array.

The do statement

A do statement conditionally executes a statement one or more times.
The example

using System;
class Test
{
static void Main() {
string s;
do {
s = Console.ReadLine();
}
while (s != "Exit");
}
}

reads from the console until the user types ?Exit? and presses the enter key.

The for statement

A for statement evaluates a sequence of initialization expressions and then, while a condition is true, repeatedly executes a statement and evaluates a sequence of iteration expressions.
The example

using System;
class Test
{
static void Main() {
for (int i = 0; i < 10; i++)
Console.WriteLine(i);
}
}

uses a for statement to write out the integer values 1 through 10.

The foreach statement

A foreach statement enumerates the elements of a collection, executing a statement for each element of the collection.
The example

using System;
using System.Collections;
class Test
{
static void WriteList(ArrayList list) {
foreach (object o in list)
Console.WriteLine(o);
}
static void Main() {
ArrayList list = new ArrayList();
for (int i = 0; i < 10; i++)
list.Add(i);
WriteList(list);
}
}
M. Saqib: Saqib is Master-level Senior Software Engineer with over 14 years of experience in designing and developing large-scale software and web applications. He has more than eight years experience of leading software development teams. Saqib provides consultancy to develop software systems and web services for Fortune 500 companies. He has hands-on experience in C/C++ Java, JavaScript, PHP and .NET Technologies. Saqib owns and write contents on mycplus.com since 2004.
Related Post