4. Control Structure: Objectives

This chapter introduces Control Structures (also known as Loops) to meet the following objectives:

(i) Condition Statements
(ii) Iteration Statements
(iii) Jump Statements
(iv) Conversions
(v) Expressions and Statements

C# supports three kinds of Contol Structures. They are (i)Condiitional Statements, (ii)Iteration Statements, and
(iii)Jump Statements for developing and controlling the execution of the program.

1. Conditional Statements

C# .NET supports the following Conditional Statements:
  • if statement
  • if… else statement
  • ternary operator
  • break statement
  • continue statement

if else example code
        //Example Listing 4.1
        using System;
        namespace QSSTraining
        {
            /// 
            /// if else statement
            /// 
            public class Program
            {
        
                public static void Main(string[] args)
                {
                    int a = 10;
                    int b = 20;
                    int max = 0;
                    if (a > b)
                    {
                        max = a;
                    }
                    else
                    {
                        max = b;
                    }
                    Console.WriteLine("Maximum value = {0}", max);
                }
           }
        }
        

Ternary Operator: C# provides a ternary operator that is an alternative to the if… else statement.


ternary operator example code
        //Example Listing 4.2
        using System;
        namespace QSSTraining
        {
            /// 
            /// ternary operator statement
            /// 
            public class Program
            {
                public static void Main(string[] args)
                {
                    int a = 10;
                    int b = 20;
                    int max = 0;
                    max = a > b ? a : b;
                    Console.WriteLine("Maximum value = {0}", max);
                }
            }
        }
        

Switch Case statement:
A switch case statement acts much like multiple if... else statements.


switch case statement example code
        //Example Listing 4.3
        using System;
        namespace QSSTraining
        {
            /// 
            /// switch case statement
            /// 
            public class Program
            {
                //SwitchCase 
                public static void Main(string[] args)
                {
                    Console.Write("Enter State Code: ");
                    string stateCode = Console.ReadLine();
                    switch (stateCode)
                    {
                        case "AL":
                            Console.WriteLine("Alabama");
                            break;
                        case "GA":
                            Console.WriteLine("Georgia");
                            break;
                        case "FL":
                            Console.WriteLine("Florida");
                            break;
                        case "TX":
                            Console.WriteLine("Texas");
                            break;
                        default:
                            Console.WriteLine("No valid state");
                            break;
                    }
                }
            }
        }
        

2. Iteration Statements

C# .NET supports the following Iteration statements also known as Loop statements:
  • while lloop
  • do while looop
  • for loop
  • forEach loop

while loop example code
        //Example Listing 4.4
        using System;
        namespace QSSTraining
        {
            /// 
            /// while Loop
            /// 
            public class Program
            {
                public static void Main(string[] args)
                {
                    int i = 0;
                    while (i < 10)
                    {
                        Console.WriteLine("i = " + i);
                        i++;
                    }
                }
           }
        }
        

do while loop example code
        //Example Listing 4.5
        using System;
        namespace QSSTraining
        {
             /// 
            /// Iteration Statements
            /// 
            public class Program
            {
               // do while Loop
                public static void Main(string[] args)
                {
                    int i = 0;
                    do
                    {
                        Console.WriteLine("i = " + i);
                        i++;
                    }
                    while (i < 10);
                }
              }
        }
        

for loop example code
        //Example Listing 4.6
        using System;
        namespace QSSTraining
        {
             /// 
            /// Iteration Statements
            /// 
            public class Program
            {
                // for Loop
                public static void  Main(string[] args)
                {
                    int iCount = 10;
                    for (int i = 0; i < iCount; i++)
                    {
                        Console.WriteLine("The Numbers are: {0}", i);
                    }
                }
             }
        }
        

foreach loop example code
        //Example Listing 4.7
        using System;
        namespace QSSTraining
        {
             /// 
            /// Iteration Statements
            /// 
            public class Program
            {
                 // foreach Loop
                public static void Main(string[] args)
                {
                    string[] arrNames = { "Kirby", "Robert", "Neil", "Victoria" };
                    Console.WriteLine("The List of Names: ");
                    foreach (string names in arrNames)
                    {
                        Console.WriteLine("{0}", names);
                    }
                }
            }
        }
        

3. Jump Statements

C# .NET supports the following Jump statements. The jump statements unconditionally transfer control. :
  • break statement
  • continue statement
  • goto statement
  • return statement

break statement example code
        //Example Listing 4.8
        using System;
        namespace QSSTraining
        {
            /// 
            /// Jump Statements
            /// 
            public class Program
            {
                // break
	            public static void Main(string[] args)
                {
                    for (int i = 0; i < 10; i++)
                    {
                        if (i == 5)
                            break;
                        Console.WriteLine("i = " + i);
                    }
                }
            }
        }
        

continue statement example code
        //Example Listing 4.9
        using System;
        namespace QSSTraining
        {
            /// 
            /// Jump Statements
            /// 
            public class Program
            {
                // continue
                public static void Main(string[] args)
                {
                    for (int i = 0; i < 10; i++)
                    {
                        if (i == 5)
                            continue;
                        Console.WriteLine("i = " + i);
                    }
                }
            }
        }
        

goto statement example code
        //Example Listing 4.10
        using System;
        namespace QSSTraining
        {
            /// 
            /// Jump Statements
            /// 
            public class Program
            {
                // goto
                public static void Main(string[] args)
                {
                    Console.WriteLine("Usage of goto");
                    goto MyLabel;
                    Console.WriteLine("This statement doesn't execute");
                MyLabel:
                    Console.WriteLine("Jumped into MyLabel statement");
                }
            }
        }
        

return statement example code
        //Example Listing 4.11
        using System;
        namespace QSSTraining
        {
            /// 
            /// Jump Statements
            /// 
            public class Program
            {
               // return
               public static int Main(string[] args)
                {
                    Console.WriteLine("Exiting from the Application");
                    return 0;
                }
           }
        }