Sunday, 2 November 2014

Control Structures (Statements)

The control structures are used to give to give conditional decisional statements to the computer. That is, it tells the computer what to do at a particular time.
TYPES OF CONTROL STATEMENTS
       1.   If statement
       2.   If else statement
       3.   Nested If statement
       4.   Switch/case statement
       5.   For loop statement
       6.   While loop statement
       7.   Do while statement
       8.   Go to statement
       9.   Break statement
10.    Continue statement
11.    Return statement

           1.   If statement
The If statement is a conditional statement in which a condition have to be meant. If the condition is not meant, there is no result.
The syntax of the If statement is:
If (condition)
{
Statement;
}
Let us consider the program below;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace myfirstprogram
{
    class Program
    {
        static void Main(string[] args)
        {
            double y, result;
            Console.WriteLine("enter value of y");
            y = Convert.ToDouble(Console.ReadLine());
            if (y != 5)
            {
                result = 10 * y;
                Console.WriteLine("result=" + result);
            }
         }
    }
}

From the program we can see that whenever a user enters a number that is not 5, result will be display but if a user enters 5 nothing will happen.
           2.   If … else statement
The If … else statement is a conditional statement in which a condition is meant and if not another alternative is provided.
The syntax of the If … else statement is:
If (condition)
{
Statement;
}
Else
{
Statement;
}
In a case where you have more than one condition you can also apply logical AND (&&) and OR (||).
Consider the program below on the use of If … else statement;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace myfirstprogram
{
    class Program
    {
        static void Main(string[] args)
        {
            double y, result;
            Console.WriteLine("enter value of y");
            y = Convert.ToDouble(Console.ReadLine());
            if (y != 5)
            {
                result = 10 * y;
                Console.WriteLine("result=" + result);
            }
            else
            {
                result = 0 * y;
                Console.WriteLine("result=" + result);
            }
        }
    }
}

From the program we can see that whenever a user enters a number that is not 5, the statement after the if statement will react but the user enters 5 the output result will be 0.

           3.   Nested If statement
A situation where we have more than one If … else statement in a program in a program is called the nested if statement.
The syntax of the nested if statement is:
If (condition1)
{
Statement1;
}
Else if (condition2)
{
Statement2;
}
.
.
.
Else
{
Statement  N;
}
Let’s take a quadractic equation solver (program to solve the root of a quadractic equation) to demonstrate this. Many students in programming class always find this difficult to solve the root of a quadractic equation. I will take my time to explain how to go about it before writing the code.
Recall that quadractic equation is of the form:
                                 ax2+bx+c=0
and the formular is: x1x2 =  (-b + sqrt(b2-4ac))/2a
                                                     
Let d=b2-4ac
Remember that quadractic equation has 3 conditions
     1.   When d>0 (we have two different roots) x1 =  (-b + sqrt(b2-4ac))/2a
                                                                              
                                                             X2= (-b - sqrt(b2-4ac))/ 2a
                                                                                     
     2.   When d=0 (we have two equal roots) x1=x2=-b/2a
     3.   When d<0 (complex root). For now we won’t solve this, but it will be later done.
Now let us write the program;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QE
{
    class Program
    {
        static void Main(string[] args)
        {
            double a, b, c, d, x1, x2;
            Console.WriteLine("Enter the value of a");
            a = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Enter the value of b");
            b = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Enter the value of c");
            c = Convert.ToDouble(Console.ReadLine());
            d = b * b - 4 * a * c;
            if (d == 0)
            {
                x1 = -b / (2 * a);
                x2 = x1;
                Console.WriteLine("X1=" + x1);
                Console.WriteLine("X2=" + x2);
            }
            else if (d > 0)
            {

                x1 = (-b + Math.Sqrt(d)) / (2 * a);
                x2 = (-b - Math.Sqrt(d)) / (2 * a);
                Console.WriteLine("X1=" + x1);
                Console.WriteLine("X2=" + x2);
            }
            else
            {
                Console.WriteLine("complex root");
            }
        }
    }
}

           4.   Switch/case statement
The switch statement is more readable because it eliminates the problem of writing plenty code compare to that of the If … else statement. The switch and the case statement go hand-in-hand. Just like the If … else statement. The advantage the switch/case statement have over the if … else statement is that it reduces data redundancy.
The syntax of the switch/case is:
Switch(condition)
{
Case1:
Statement;
Break;
Case2:
Statement;
.
.
.
Case  N:
Statement;
Break;
}
Let us find the GPA of two courses in a department. You can modify this code to find the CGPA of students in the whole school.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace myfirstprogram
{
    class Program
    {
        static void Main(string[] args)
        {
           //gpa computation
            string csc101,csc102;
            double csc101g,csc101u,csc102g,csc102u,gpa;
            csc101=Convert.ToString(Console.ReadLine());
            csc102=Convert.ToString(Console.ReadLine());
            csc101g = 5;
            csc101g = 4;
            csc101u = 4;
            csc102g = 5;
            csc102u = 3;
            switch (csc101)
            {
                case "A":
                    csc101g = 5;
                    csc101u = 4;
                   
                    break;
                case "B":
                    csc101g = 4;
                    csc101u = 4;
                   
                    break;
            }
            switch (csc102)
            {
                case "A":
                    csc102g = 5;
                    csc102u = 3;
                   
                    break;
                case "B":
                    csc102g = 4;
                    csc102u = 3;
                   
                    break;
            }
            gpa = (csc102g * csc102u + csc101g * csc101u) / (csc101u + csc102u);
            Console.WriteLine(gpa);
             
      }
    }
}

           5.   For loop statement
The for loop statement is used for looping a particular set of code either forward (++) or backward (--). It is one of the most widely used control statements for performing iterations in a loop. The For loop statement is a PRETEST loop which means if first tests if a condition is true and only executes if it is.
Syntax:
For (int expression; condition; increment expression)
{
Statement;
}
Program to solve Jacobi iteration of 3 x 3 matrix is shown below. The matrix is of the form;
ax+by+cz=d …………(1)
ex+fy+gz=h ………….(2)
ix+jy+kz=l ……………(3)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QE
{
    class Program
    {
        static void Main(string[] args)
        {
            double a, b, c, d, e, f, g, h, i, j, k, l, x, y, z;
            double tempx, tempy, tempz, iteration;
            tempx = tempy = tempz = 0;
            Console.WriteLine("enter value of a");
            a = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("enter value of b");
            b = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("enter value of c");
            c = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("enter value of d");
            d = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("enter value of e");
            e = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("enter value of f");
            f = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("enter value of g");
            g = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("enter value of h");
            h = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("enter value of i");
            i = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("enter value of i");
            j = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("enter value of k");
            k = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("enter value of l");
            l = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("enter value of iteration");
            iteration = Convert.ToDouble(Console.ReadLine());
           
            for (int m = 0; m < iteration; m++)
            {
                x = (d - b * tempy - c * tempz) / a;
                y = (h - e * tempx - g * tempz) / f;
                z = (l - i * tempx - j * tempy) / k;
                tempx = x;
                tempy = y;
                tempz = z;
               
                Console.WriteLine("x=" + x);
                Console.WriteLine("y=" + y);
                Console.WriteLine("z=" + z);
            }

        }
    }
}

Exercise: Try the above matrix form using Gaus Scidel iteration method.

           6.   While loop statement
The while loop statement is a conditional loop statement that behaves like the For loop statement. The while loop statement executes until the whole condition is true.
Syntax:
While (condition)
{
Statement
}

Consider the program below as an example.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace myfirstprogram
{
    class Program
    {
        static void Main(string[] args)
        {
            int man = 0;
            while (man < 10)
            {
                Console.WriteLine(man.ToString());
                man++;
            }

        }
    }
}

           7.   Do … while statement
The do … while loop statement is a conditional loop statement that executes a statement first and then checks if the condition is true. If the condition is true, the loop continues until it is false. Because of this condition, the do ….while loop is regarded as a post-test loop.
Syntax:
Do
{
Statement;
}
While (condition);

Consider the program below;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace myfirstprogram
{
    class Program
    {
        static void Main(string[] args)
        {
            int man = 0;
            do
            {
                Console.WriteLine(man.ToString());
                man++;
            }
            while (man < 10);
           

        }
    }
}

           8.   Goto statement
The goto statement is an unconditional jump statement. When the goto statement is encountered program flow jumps to the location specified by the goto.
           9.   Break statement
In a simple term, the break statement leaves the current loop. It exists from a loop or a switch immediately after a condition is meant. The break statement can be used in a switch statement (check the program under the switch statement, in a For statement, etc.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QE
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 50; i++)
            {
                if (i == 20)
                {
                    break;
                }
                Console.WriteLine(i);
            }
            Console.WriteLine("exists");
            Console.ReadKey();
        }
    }
}

Console.ReadLine() and Console.ReadKey() behaves the same way.
           10.               Continue statement
The continue statement behaves exactly like the break statement. In a simple word, the continue statement goes to the next iteration of the current loop.
Program: write the same program under break but replace break with continue.
The difference between the break statement and the continue statement is that the break statement makes control exists the entire loop while the continue statement only skips the current iteration.
           11.               Return statement
The return statement returns from a method before the end of that method is reached. It can be a value or not, depending on the method that calls it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QE
{
    class Program
    {
        static void Main(string[] args)
        {
            int result = 101 + 20;
            if (result > 90)
            {
               
                Console.WriteLine("the result is large");
                return;
            }
        }
    }
}


From the example above the return statement returns nothing

No comments :

Post a Comment