05 - C# Control Statements

5.1 If Statements

If Statement is used for developing conditional statements. With If statement the sequence of the code can be changed. It checks for the Boolean condition. A true statement is one that evaluates to a non - zero number. A false statement evaluates to zero.

The basic structure of If statement is as shown below:

if(statement is TRUE)
EXECUTE this line of code

If statement in if…else conditional statement is followed by the logical expression where data is compared and based on the results the decision is done. In an If else statement if condition evaluates to true, then the statement is executed. If the condition is false, else statement is executed. The syntax for the if else construct is as shown below:

 

if(expression)
{
    statements;
}
else
{
    statements;
}

 

Consider an example for if else construct as shown below:

class Program
{
    static void Main( string[ ] args)
    {
        int age = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine(“enter the age”);
        age=Convert.ToInt32(Console.ReadLine());
        
        if(age<18)
        {
            Console.WriteLine(“You are not eligible”);
        }
        else
        {
            Console.WriteLine(“You are eligible”);
        }
        Console.Read();
    }
}

 

Output is as shown below:

Nested If else statements: If statements can be nested in C#. User can test multiple conditions using if else if statement. The syntax for nested if else statements is as shown below:

if ( condition )
{
   code to execute;
   if (condition)
   {
    code to execute;
   }
   else if ( condition )
   {
     if ( condition )
    {
        code to execute;
    }
   }
}

 

A sample code for nested if else is as shown below:

class Program
{
    int age;
    string gender;
    Console.WriteLine(“enter the age”);
    age=Convert.ToInt32( Console.ReadLine());
    Console.WriteLine(“enter the gender”);
    gender = Console.ReadLine();
    
    if(age>12)
    {
        if(age<18)
        {
            if(gender ==”male”)
            {
                Console.WriteLine(“You are a male child”);
            }
            else
            {
                Console.WriteLine(“You are a girl child”);
            }
        }
    else
    {
        Console.WriteLine(“You are an adult”);
    }
    }
    else
    {
        Console.WriteLine(“You are too young”);
    }
    }
}

Output is as shown below:

5.2 switch statement

The switch statement is used when you have to evaluate a variable for multiple values. The switch statement construct is as shown below:

switch ( variablename )
{
    case constantExpression_1:
    statements;
    break;
    case constantExpression_2:
    statements;
    break;
    case constantExpression_3:
    statements;
    break;
    default:
    statements;
    break;
}

When the switch statement is executed, the variable name is given in the switch statement. It is compared with each case constant. If there is a matching case statement, the control is passed to the corresponding statement. A break statement is used to exit form the switch statement. If none of the case matches the variable, the default statement is executed.

Consider a code example for the switch statement as shown below:

class Program
{
    static void Main ( string[ ] args)
    {
        int grade;
        grade=4;
        switch ( grade )
        {
            case 1:
                Console.WriteLine(“Your grade is : Pass”);
                break;
            case 2: 
                Console.WriteLine(“Your grade is : OK”);
                break;
            case 3:
                Console.WriteLine(“Your grade is : Good”);
                break;
            case 4:
                Console.WriteLine(“Your grade is : Excellent”);
                break;
            default: 
                Console.WriteLine(“Your grade is not a valid grade”);
                break;
        }
        Console.Read();
    }
}

Output is as shown below:

5.3 break and continue statements

The break statement terminates the case in the switch statement where it is placed. Control is passes to the statement followed by the terminated statement if present in the loop.

The code snippet for break statement is as shown below:

class Program
{
    static void Main ( string [ ] args )
    {
        for ( int i=0; i<=10; i++ )
        {
            if ( i==4)
            {
                break;
            }
            Console.WriteLine(i);
        }
        Console.Read();
    }
}

Output is as shown below:

The continue statement is used to pass the control to the next iteration for the statement in which it appears.

The code snippet for the continue statement is as shown below:

class Program
{
    static void Main ( string [ ] args )
    {
        for ( int i=0; i<=10; i++)
        {
          if(i<6)
          {
            continue;
           }
           Console.WriteLine(i);
        }
        Console.Read();
    }
}

 

Output is as shown below:

5.4 The while loop

The while loop statement is used to execute a block depending on the condition specified. The condition is checked  first and the statements within the loop are executed if the condition returns true.

After the last statement in the while loop is executed, the control is sent back to the start of the loop. When the condition is true, the statements present in the loop are executed. The execution of the loop stops when the condition returns a false value.

The syntax for the while loop is as shown below:

while ( expression )
{
    statements;
}

The code snippet to demonstrate the while statement is as shown below:

class Program
{
    static void Main ( string[ ] args )
    {
        int i;
        i = 1;
        while ( i<10 )
        {
            Console.WriteLine(“Value of the variable is :{0}”, i );
            I = i+2;
        }
        Console.Read();
    }
} 

The output is as shown below:

5.5 The do..while loop

The do while loop and while loop are similar. Both the loops will execute until the loop becomes false. But the statements in the do while loop are executed at least once, as they are executed before the condition is checked. 

The difference between the while loop and the do while loop is as shown below:

The syntax for do while loop is as shown below:

do
{
    statements;
} while ( expression );

 

A sample code for do while loop is as shown below:

class Program
{
    static void Main ( string[ ] args )
    {
        int i=0;
        i=10;
        do
        {
            Console.WriteLine(“value of i is”+i);
            i = i+10;
        }while ( i<100 );
        Console.Read();
    }
}

 

The output is as shown below:

5.6 The for loop

A for loop structure is used to execute a set of statements for a specified number of times. The following statements show the syntax of for loop:

for ( initialization; termination; increment/decrement)
{
    statements
}

The initialization expression is used for initializing for loop. It is executed once at the starting of the loop. The termination expression contains the value to end the loop. It is checked for the condition. Before the execution of iteration, this expression is evaluated. If the expression is results a false value, the loop ends performing the iteration. The increment or decrement expression is called for each iteration.

The sequence of execution of for loop construct is as shown below:

An infinite loop is created by keeping all the three expressions blank, as shown in the code below:

for ( ; ; )
{
    statements
}

A sample code about for loop is as shown below:

class Program
{
    static void Main ( string [] args )
    {
        int i;
        for ( i=50; i<=60; i++ )
        {
            Console.WriteLine( “Value of variable I is “+i);
        }
        Console.Read();
    }
}

The output is as shown below:

Like us on Facebook