Control Statement: Selection Statement in C#

text zoom

Control statements are used to process some conditional code which only runs if the condition is true otherwise some alternate code may be processed. The following are two control statements used in C#.



  1. if-else statement

  2. switch statement


IF-ELSE Statement:


If Statement allows you to take different path depending on the decision condition. The condition must return boolean. When the condition evaluates to a boolean true, a block for that true condition will be executed.


 


Syntax:



  • If (condition) {

    // Single if statement

    //execute block if condition, returns true.}

  • If (condition) {

    // execute block if condition, return false

    } else {

    // execute block if condition, return false

    }

  • If (condition1) {

    // execute block if condition1 is true

    } else if (condition2) {

    // execute block if condition2 is true

    } else if (condition 3) {

    // execute block if condition3 is true

    } else {

    // otherwise if none condition satisfied then, this block will execute.

    }


 


Switch Statement:


Another selection statement is the switch statement, which executes a set of logic depending on the value of a given parameter that is evaluated by switch statement. The type of values a switch statement operates on can be Boolean, Enums, Integral Type and Strings.


 


Syntax:


Switch (Evaluated Value can be Boolean or Enums or integral type or strings) {


//switch with integer type


Case 1: // Block of statement


Break;


Case 2: // Block of statement


Break;


Case 3: // Block of statement


Break;


Case 4: // Block of statement


Break;


Default: // Block of statement


Break;


}


 


Note:- The switch block follows the switch expression, where one or more choices are evaluated for a possible match with the switch expression. Each choice is labeled with the case keyword, followed by an example that is of the same type as the switch expression and followed by a colon (:). When the result evaluated in the switch expression matches one of these choices, the statement immediately following the matching choice is executed. The block ends with either break, continue, goto, return or throw statement.


 


Example: Demonstrate Selection Statement





 


using System;


using System.Collections.Generic;


using System.ComponentModel;


using System.Data;


using System.Drawing;


using System.Text;


using System.Windows.Forms;


namespace cSHARPEXAMPLES


{


public partial class Form2 : Form


{


  public Form2()


    {


      InitializeComponent();


    }


  private void Form2_Load( object sender, EventArgs e)


    {


      Console.WriteLine( "FORM 2" );


    }


  private void button1_Click( object sender, EventArgs e)


    { // Here we can go through the example on IF


      int _value = Int32 .Parse(txtEnter.Text);


  if (_value > 100)


    {


    MessageBox .Show( "Value is greater than 100" );


        txtEnter.Text = "" ;


    } else if (_value < 100 && _value > 200){


    MessageBox .Show( "Value is greater than 100 but less than 200" );


        txtEnter.Text = "" ;


    } else {


    MessageBox .Show( "Value is less than 100" );


        txtEnter.Text = "" ;


        }


  }


  private void button2_Click( object sender, EventArgs e)


    { // Here we can go through the example on Switch


      int _value = Int32 .Parse(txtEnter.Text);


   switch (_value) // switch example with integer type


    {


    case 1:


    case 4:


    case 5: //Here the case 1 and 4 has no break, this will falls to this statement


      MessageBox .Show( "Value is 1 or 4 or 5" );


            txtEnter.Text = "" ;


      break ;


    case 2:


      MessageBox .Show( "Value is 2" ); txtEnter.Text = "" ;


      break ;


    case 3:


      MessageBox .Show( "Value is 3" ); txtEnter.Text = "" ;


      break ;


    default :


      MessageBox .Show( "Value is greater than 3" );


      break ;


            }


        }


    }


}


Output:



Click on: IF Example Button


This print value is less than 100


Click on: switch Example Button


This print value is greater than 3

                    

Copyright © 2013 VisualBuilder. All rights reserved