Statements in C#

 

Here we are going to discuss various statements available in C#. We discuss about if statement, switch statements in C#.

 

Empty statement: 

                ;  

; is a terminator, not a separator

 

Assignment Statement: 

                x = 2 * y + 1;

 

Calling the Method: 

string s = "A, B, C"; 

string[] parts = s.Split(',');             // invocation of an object method (non-static) 

s = String.Join(" + ", parts);          // invocation of a class method (static)

 

if statement:

 Like any other language in C# also has if statement to check the condition.

if ('0' <= ch && ch <= '9') 

{ 

     val = ch - '0'; 

}

else if ('A' <= ch && ch <= 'Z') 

{ 

      val = 10 + ch - 'A'; 

} 

else 

{ 

      val = 0; 

      Console.WriteLine("invalid character " + ch); 

}

switch statement: 

switch statement is used to handle multiple selections by passing control to one of the case statements within its body. 

switch (country) 

{ 

       case "England": 

       case "Canada": 

       case "USA": 

                    language = "English"; 

                    break; 

       case "Germany":

       case "Austria": 

       case "Switzerland": 

                    language = "German"; 

                    break; 

        case null: 

                    Console.WriteLine("no country specified"); 

                    break; 

         default: 

                    Console.WriteLine("don't know the language of " + country); 

                    break; 

}

 

Type of the switch expression: 

integer type, char, enum or string (null ok as a case label).

No fall-through (unlike in C or in Java)!: 

Every statement sequence in a case must be terminated with break (or return, goto, throw).

 

If no case label matches è default 

If no default specified è continuation after the switch statement

switch with goto statement: 

int state = 0; 

int ch = Console.Read(); 

switch (state) 

{

                case 0:

                      if (ch == 'a')

                       {

                            ch = Console.Read();

                            goto case 1;

                      } 

                    else if (ch == 'c') goto case 2; 

                    else goto default; 

                case 1:

                     if (ch == 'b')

                    {

                        ch = Console.Read();

                        goto case 1;

                   }

                    else if (ch == 'c') goto case 2; 

                    else goto default; 

                case 2:

                     Console.WriteLine("input valid"); 

                     break; 

                default:

                     Console.WriteLine("illegal character " + ch); 

                     break;

} 

 

 

While statement: 

while (i < n)

{

    sum += i;

    i++;

}

do-while statement: 

do 

{

   sum += a[i]; 

                i--;

 } while (i > 0);

 

 for statement:

 

for (int i = 0; i < n; i++) 

{

    sum += i; 

 }

 

foreach statement:

For iterating over collections and arrays

 int[] a = {3, 17, 4, 8, 2, 29}; 

  foreach (int x in a)

         sum += x;

 string s = "Hello";

 

foreach (char ch in s)

          Console.WriteLine(ch);

 

             Queue q = new Queue(); // elements are of type object 

                q.Enqueue("John"); q.Enqueue("Alice"); 

          

 foreach (string s in q)

Console.WriteLine(s);

 

break statement:

 For exiting a loop or a switch statement.  There is no break with a label like in Java (use goto instead).

 

continue statement:

 Continues with the next loop iteration.

 

return statement:

 Returning from a void method

 

                void Foo (int x) { 

                                if (x == 0) return; 

                                ...

                }

Returning a value from a function method

 

                int Max (int a, int b) { 

                                if (a > b) return a; else return b; 

                }

                class C { 

                                static int Main() { 

                                                ...

 

                                                return errorCode;            // The Main method can be declared as a function;

 

                                }                                              // the returned error code can be checked with the

 

                                                                                // system variable errorlevel

 

                }

 

Getting input from Keyboard : 

int ch = Console.Read(); 

returns the next character.

 

waits until the user pressed the return key. 

 

                e.g. input: "abc" + return key. 

                Read returns: 'a', 'b', 'c', '\r', '\n'.

 

                after the last character (Ctrl-Z + return) Read returns -1

 

string line = Console.ReadLine(); 

                returns the next line (after Ctrl-Z+CR+LF it returns null). 

                waits until the user pressed the return key. 

                returns the line without CR, LF.

 

Input from a File: 

using System; 

using System.IO;

 

class FileInput 

{ 

 

    static void Main() { 

                                FileStream s = new FileStream("input.txt", FileMode.Open); 

                                StreamReader r = new StreamReader(s);

 

                                string line = r.ReadLine(); 

                                while (line != null) { 

                                                ... 

                                                line = r.ReadLine(); 

                                }

 

                                r.Close(); 

                } 

}

 

It is not possible to have multiple StreamReaders working on the same streamat the same time.

 

Reading Command-line Parameters:

 

using System;

class CmdInput 

{

    static void Main(string[] arg) 

    {          // e.g. invoked as: Test value = 3

 

        for (int i = 0; i < arg.Length; i++) 

            Console.WriteLine("{0}: {1}", i, arg[i]);        // output (tokens are separated by blanks): 

        //    0: value 

        //    1: = 

        //    2: 3

 

        foreach (string s in arg) 

            Console.WriteLine(s);        // output:  

    } 

}