[C# Tutorial 6] Statements

0

In earlier tutorials we learned about Classes and Objects and their insiders methods and constructor Now, it’s time for real coding, that is to use logic statements inside the methods.

In C#, a complete program instruction is called a statement. Programs consist of
sequences of C# statements. Directly or indirectly every statement ends with a semicolon (;). C# statements are evaluated in order. The compiler starts at the beginning of a statement list and makes its way to the end. However this order can be altered by using branching statements or iterative statements.

Branching Statement:

There are two types of branches in a C# program:

  • Unconditional branches
  • Conditional branches.

Unconditional Branching Statements: 

These kind of branching is done with the help of method calling.

The above diagram shows the execution flow of a program. The methods are called without any conditional checks.

Conditional Branches:

A conditional branch is created with special words also known as keywords like if, else or switch.A conditional branch occurs only if the condition expression evaluates true.

If..else statements

The syntax of if..else statement is as shown below:

if (expression)
   statement1
[else
   statement2]

The syntax consists of “if” keyword followed by condition in round brackets. The next line consists of the body of “if” which is enclosed in curly brackets.[If any(if for else) block contains only 1 statement then curly brackets can be avoided]. However this block is executed when the condition in round bracket returns true. If it returns false then the else block is executed if exists as else part is optional. If the condition is true, then if part is executed and else part is bypassed. and vice-verse.

Nested if..else statements:

It is also known as one of the variant of if..else statement. As the name suggest it contains if..else statement inside another if..else statements.This nesting is mainly needed to work with complex data which is needed to be checked against constraints. Its general format is as shown below:

if (expression)
     statement1
     if (expression)
          statement1
     [else
           statement2][else
      statement2]

The inner if..else statements can be written in any outer block that is either if or else.

Example: This code snippet checks the percentage and decides whether it is 1st class or distinction, 2nd class or failed.

            if(percentage<35)
Console.WriteLine(“Failed”);
else
if(percentage<60)
Console.WriteLine(“2nd class”);
else
if(percentage<75)
Console.WriteLine(“1st class”);
else
Console.WriteLine(“1st class with distinction”);

In the above code snippet the 1st if condition checks whether the percentage is less than 35  if it is then if block is executed and else block is bypassed. If it is not then else part is executed and if block is bypassed.

Switch statements:

Switch case is mainly used when there are many options from which a user selects one, and according to that set of lines are executed.

Syntax:

switch (expression)
{
case constant-expression:
statement
jump-statement
[default: statement]
}

Example: The following example displays a menu for calculator and on selecting the option it performs the particular operation.

 Console.WriteLine(“1. Add \n1. Subtract \n3. Multiply \n4. Divide”);
Console.WriteLine(“Enter your choice”);
int option;
int a = 50, b = 20, result =0;
option = Convert.ToInt16(Console.ReadLine());
switch (option)
{
case 1:
result = a + b;
break;
case 2:
result = a – b;
break;
case 3:
result = a * b;
break;
case 4:
result = a / b;
break;
default:
Console.WriteLine(“Enter correct choice”);
break;

}
Console.WriteLine(“Result:  ” + result);

Output:

 

In the above code we have also used a default case, if no option match is found then default case is executed, a precaution to be taken here is that default case is always written at last, if written first then all options will match with default case and thus no further cases will ever get executed despite of match found. We can also use strings for selecting option in switch case, It is written in double quotes as case “<value> ” :

There is one more type of statements known as Iterative statements. explained in next tutorial

Share.

Leave A Reply