C# series a : Control statement - ponda

Breaking

7/07/2017

C# series a : Control statement

1. if statement
- An if statement identifies which statement to run based on the value of a Boolean expression.
- The if statement is a conditional statement that executes a block of code when condition is true.

Syntax :

if(condition)
{
     // Execute this code when condition is true
}

Example 1 : ( the condition can be any expression that returns true or false ).

string str1 = "ABC";
string str2 = "DEF";

if(str1 == str2)
{
     //this line will be hit only if the evaluation of 'if' is true
     Console.WriteLine("normal if");
}

Example 2 :

static void Main(string[]args)
{
     int x = 8;
     int y = 3;
   
     if(x > y)
     {
          Console.WriteLine("x is greater than y");
     }
}

the code above will evaluate the condition x > y. if it is true, the code inside the if block will execute.
when only one line of coe is in the if block, the curly braces can be omitted. 
for example :
if(x > y)
   Console.WriteLine("x is greater than y");


2. if-else statement
An optional else clause can be specified to execute a block of code when the condition in the if statement evaluates to false.

Syntax :

if(condition)
{
     // statement logic to execute if true
}
else
{
     //statement logic to execute if false
}

Example 1 :

int mark = 85;

if(mark < 50)
{
     Console.WriteLine("You failed");
}
else
{
     Console.WriteLine("You passed");
}

// Outputs "You passed".

Example 2 :

string str1 = "ABC";
string str2 = "DEF";

if(str1 == str2)
{
//this line will be hit only if the evaluation of if is true
Console.WriteLine("if block");
}
else
{
//this line will be hit only if the evaluation of if is false
Console.WriteLine("Else Block");
}

3. if-else-if statement

Syntax :

if(condition)
{
     //logic for execution
}

else if(condition)
{
     //logic for execution
}

else
{
     //logic for execution
}

Example 1 :

string str1 = "ABC";
string str2 = "DEF";

if(str1 == str2)
{
     //this line will be hit only if the evaluation of if is true
     Console.WriteLine("IF BLOCK");
}

else if(str1 == "ABC" && str2 != "ABC")
{
     //this line will be hit only if the evaluation of else-if is true
     Console.WriteLine("Else-If Block");
}

else
{
     //this line will be hit only if both the upper if returns false
     Console.WriteLine("Else Block");
}

Example 2 :
The if - else if Statement can be used to decide among three or more actions.

int x = 33;
if (x == 8)
{
      Console.WriteLine("Value of x is 8");
}
else if (x == 18)
{
      Console.WriteLine("Value of x is 18");
}
else if (x == 33)
{
     Console.WriteLine("Value of x is 33");
}
else
{
      Console.WriteLine("No match");
}

// output "Value of x is 33"
Remember, that an if can have zero or more else if's and they must come before the last else, which is optional. Once an else if succeeds, none of the remaining else if's or else clause will be tested.

4. Nested if statements
You can also include or nest if statements within another if statement.

Example :
int mark = 100;
if(mark >= 50)
{
     Console.WriteLine("You passed");
     if(mark == 100)
     {
          Console.WriteLine("perfect!");
     }
}
else
{
     Console.WriteLine("You failed");
}

/* Outputs
You passed
Perfect!
*/

You can nest an unlimited number of if - else statements.
Example :

int age = 18;
if (age > 14)
{
     if(age > 18)
     {
          Console.WriteLine("Adult");
     }
     else
     {
          Console.WriteLine("Teenager");
     }
}
else
{
     if (age > 0)
     {
          Console.WriteLine("Child");
     }
     else
     {
          Console.WriteLine("Something's wrong");
     }
}

// output "Teenager"
Remember that all else clauses must have corresponding if statements

5. Switch statement is like a series of if statements
The switch statement is a control statement that selects a switch section to execute from a list of candidates. A switch statement include one or more switch sections. Each switch section contains one or more case labels followed by one or more statements.

The switch statement provides a more elegant way to test a variable for equality against a list of values. Each value is called a case and the variable being switched on is checked for each switch case.

Syntax

switch(variable)
{
case <val1> :
     //logic execution
break;

case <val2> :
     //logic execution
break;

case <valn> :
     //logic execution
break;

default :
     //logic execution
break;
}

Example 1 :

Console.WriteLine("enter subject?(Science/Maths/English");
string subject = Console.ReadLine();

switch(subject.ToLower())

{
case "english":
Console.WriteLine("english selected");
//break will move the control out of the switch block(required)
break;

case "maths":
Console.WriteLine("maths selected");
break;

case "science":
Console.WriteLine("science selected");
break;

//executed when all the above case is failed
default:
Console.WriteLine("wrong subject selected");
break;
}

Example 2 :
int num = 3;
switch (num)
{
     case 1:
     Console.WriteLine("one");
     break;

     case 2:
     Console.WriteLine("two");
     break;

     case 3:
     Console.WriteLine("three");
     break;
}

// Outputs "there"

Example 3 :

Fill in the blanks to create a valid switch statement

int x = 33;
switch( x )
{
     case 8:
     Console.WriteLine("Value is 8");
     break;

     case 18:
     Console.WriteLine("Value is 18");
     break;

     case 33:
     Console.WriteLine("Value is 33");
     break;
}

Each case represents a value to be checked, followed by a colon and the statements to get executed if that case is matched.

A switch statement can include any number of cases. However, no two case labels may contain the same constant value. The break; statement that ends each case will be covered shortly.

Example 4 :
THE DEFAULT CASE
in a switch statement, the optional default case is executed when none of the previous cases match.
xample A :

int age = 88;
switch(age)
{
     case 16:
     Console.WriteLine("Too young");
     break;

     case 42:
     Console.WriteLine("Adult");
     break;

     case 70:
     Console.WriteLine("Senior");
     break;

     default:
     Console.WriteLine("The default case");
     break;
}

// Outputs "The default case"
The default code executes when none of the cases matches the switch expression.

xample B:
case(x)
{
     case 10:
     Console.WriteLine("Ten");
     break;

     case 20:
     Console.WriteLine("Twenty");
     break;

     default:
     Console.WriteLine("No match");
     break;
}


The break statement
the role of the break statement is to terminate the switch statement. Without it, execution continues past the matching case statements and falls through to the next case statements, even when the case labels dont match the switch variable. This behavior is called fallthrough and modern C# compilers will not compile such code. All case and default code must end with a break statement. 

No comments:

Post a Comment