0% found this document useful (0 votes)
48 views

Conditional Statements - Selection Statements: Depending On Certain Conditions

The document discusses different types of conditional and selection statements in C# including if/else statements, switch statements, and looping statements like while, do-while, for, and foreach. It provides examples of how each statement works and its basic syntax. Key conditional operators and branching statements like break, continue, goto, and return are also covered.

Uploaded by

Amar Sequeira
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Conditional Statements - Selection Statements: Depending On Certain Conditions

The document discusses different types of conditional and selection statements in C# including if/else statements, switch statements, and looping statements like while, do-while, for, and foreach. It provides examples of how each statement works and its basic syntax. Key conditional operators and branching statements like break, continue, goto, and return are also covered.

Uploaded by

Amar Sequeira
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Conditional Statements - Selection statements

If and switch are considered selection statements


These statements assist in determining the logical path code would take
depending on certain conditions.
The If Satetment
An if statement allows you to take different paths of logic, depending on a
given condition.
If the Boolean expression evaluates to true, the statement embedded in the
if clause will be executed.
If the Boolean expression evaluates to false, the statement embedded in the
else clause will be executed.
To build complex expressions, C# offers an expected set of conditional
operators, as shown in Table

// Single Decision and Action with braces


        if (myInt > 0)
        {
            Console.WriteLine("Your number {0} is greater than zero.", myInt);
        }
// Either/Or Decision
        if (myInt != 0)
        {
            Console.WriteLine("Your number {0} is not equal to zero.", myInt);
        }
        else
       {
            Console.WriteLine("Your number {0} is equal to zero.", myInt);
        }
// Multiple Case Decision
        if (myInt < 0 || myInt == 0)
        {
            Console.WriteLine("Your number {0} is less than or equal to zero.", myInt);
        }
        else if (myInt > 0 && myInt <= 10)
        {
            Console.WriteLine("Your number {0} is in the range from 1 to 10.", myInt);
        }
        else if (myInt > 10 && myInt <= 20)
        {
            Console.WriteLine("Your number {0} is in the range from 11 to 20.", myInt);
        }
        else if (myInt > 20 && myInt <= 30)
        {
            Console.WriteLine("Your number {0} is in the range from 21 to 30.", myInt);
        }
        else
       {
            Console.WriteLine("Your number {0} is greater than 30.", myInt);
        }
The switch statement
This statement evaluates an expression and compares the expression's value
to a number of cases.
Each case is associated with a statement list that is called a switch section.
The expression used as the driver for the switch statement is displayed in
parentheses that follow the switch keyword.
In general, the expression used in the switch statement must evaluate to one
of the following types: sbyte, byte, short, ushort, int, uint, long, ulong, char,
string.
Switch sections start with the keyword case, which is followed by a constant
expression.
A colon follows the constant expression, and the statement list follows the
colon.
The break specifies the end of the statement block.
The default keyword in the switch blocks is utilized to execute a particular
section of the code whenever none of the cases listed match the value switch
block's constant value.
// switch with integer type
        switch (myInt)
        {
            case 1:
                Console.WriteLine("Your number is {0}.", myInt);
                break;
            case 2:
                Console.WriteLine("Your number is {0}.", myInt);
                break;
            case 3:
                Console.WriteLine("Your number is {0}.", myInt);
                break;
            default:
                Console.WriteLine("Your number {0} is not between 1 and 3.", myInt);
                break;
        }
// switch with string type
        switch (myInput)
        {
            case "continue":
                goto begin;
            case "quit":
                Console.WriteLine("Bye.");
                break;
            default:
                Console.WriteLine("Your input {0} is incorrect.", myInput);
                goto decide;
        }
Branching Statements
Break Statement
Leaves the switch block.
The break statement will cause the program to exit the switch statement and
begin execution with the next statement after the switch block .

//switch (myInt)
        {
            case 1:
                Console.WriteLine("Your number is {0}.", myInt);
                break;
            case 2:
                Console.WriteLine("Your number is {0}.", myInt);
                break;
            case 3:
                Console.WriteLine("Your number is {0}.", myInt);
                break;
            default:
                Console.WriteLine("Your number {0} is not between 1 and 3.", myInt);
                break;
        }
Goto Statement
Leaves the switch block and jumps directly to a label of the form
"<labelname>:“
The goto statement causes program execution to jump to the
label following the goto keyword
//switch (myInput)
        {
            case "continue":
                goto begin;
            case "quit":
                Console.WriteLine("Bye.");
                break;
            default:
                Console.WriteLine("Your input {0} is incorrect.", myInput);
                goto decide;
        }
Continue Statements
Leaves the switch block, skips remaining logic in enclosing loop,
and goes back to loop condition to determine if loop should be
executed again from the beginning.
//for loop
for (int i=0; i < 20; i++)
        {
            if (i == 10)
                break;

            if (i % 2 == 0)
                continue;

            Console.WriteLine("{0} ", i);


        }
Return
Throw
Iteration statements
While, do, for, and foreach are considered iteration statements.
These statements assist in executing embedded statements multiple times.
A while loop will check a condition and then continues to execute a block of code
as long as the condition evaluates to a Boolean value of true.
When the Boolean expression evaluates to true, the statements will execute.
Once the statements have executed, control returns to the beginning of the while
loop to check the Boolean expression again.
When the Boolean expression evaluates to false, the while loop statements are
skipped and execution begins after the closing brace of that block of code.

//While Loop

        while (myInt < 10)


        {
            Console.Write("{0} ", myInt);
            myInt++;
        }
        Console.WriteLine();
    }
The do Loop
A do loop is similar to the while loop, except that it checks its
condition at the end of the loop.
This means that the do loop is guaranteed to execute at least
one time.

//do/while loop
static void Main(string[] args)
{
string userIsDone = "";
do
{
Console.WriteLine("In do/while loop");
Console.Write("Are you done? [yes] [no]: ");
userIsDone = Console.ReadLine();
}while(userIsDone.ToLower() != "yes"); // Note the semicolon!
}
The for Loop
A for loop works like a while loop, except that the syntax of the for loop
includes initialization and condition modification.
for loops are appropriate when you know exactly how many times you want to
perform the statements within the loop.
The contents within the for loop parentheses hold three sections separated by
semicolons.
Syntax:
(<initializer list>; <boolean expression>; <iterator list>) { <statements> }
//For loop
        for (int i=0; i < 20; i++)
        {
            if (i == 10)
                break;

            if (i % 2 == 0)
                continue;

            Console.Write("{0} ", i);


        }
        Console.WriteLine();
    }
The foreach Loop
A foreach loop is used to iterate through the items in a list.
It operates on arrays or collections such as Array List, which can be found in the
System.Collections namespace.
The syntax of a foreach loop is
foreach (<type> <iteration variable> in <list>)
{ <statements> }
The type is the type of item contained in the list. For example, if the type of the list was
int[] then the type would be int.
The iteration variable is an identifier that you choose, which could be anything but should
be meaningful. For example, if the list contained an array of people's ages, then a
meaningful name for item name would be age.
//foreach loop
public static void Main()
    {
        string[] names = {"Cheryl", "Joe", "Matt", "Robert"};

        foreach (string person in names)


        {
            Console.WriteLine("{0} ", person);
        }

You might also like