Sunday, January 19, 2014

Control Structures in programming

We all know that when we write programs we write them in an order in which they will be executed. So what if we need to deviate from this order? what if we need to execute a set  of codes again and again? To do that, most of the programming languages have introduced us with control structures. These will do what ever thing they are instructed until their conditions are met. That's how they manage the control flow in a program.

There are a few control structures we can see in a programming language. To illustrate them, I have used Java language.

  • Conditional Statements
  • Iterative Structures
Before learning these, you need to have a basic idea of boolean data type. This is the data type which stores "TRUE" or "FALSE" values. Check the following example.

6 > 5 //true
2.4 > 9 //false
number == 5 //depends on how number is defined
number != 4
You have a set of operators that goes along with boolean data type.

> greater than
< less than
>= greater than ore equal
<= less than or equal
== equal
!= not equal
! not

You can use these to build boolean values.

Conditional Statements

In this category, we can find 4 different types of conditional statements. First one is the "IF" structure.

IF Structure

I will illustrate the syntax of an if structure using Java(language) codes.

if( x > 5 )
   System.out.print("The condition  is met");

Here in, x > 5 is the condition. You can see that the condition is always a boolean value. You can use raw boolean value inside a condition too, but that would not make sense of having the control structure in the first place. This structure works like this. If the value of the condition is "TRUE" then and only then the statement after that will be executed. If you have more than one statement to execute, you can enclose them inside curly brackets ( { } ).

IF ELSE Structure

This works the same as the IF Structure but this has another addition. You might have noticed in the IF Structure, the statement will only occur if and only if the condition is met. We have not specified what to do when the condition is not met.

if(x > 5 )
   System.out.print("Condition is met");
else
   System.out.print("Condition is not met");

So if the condition is not met, the else part will be executed. Which means that the condition is FALSE the else part will be executed.

IF ELSE IF

We have learned somewhat similar conditional statements. This differs from other types as follows, the two we learned previously had only one condition. If the condition is met the if block will execute, otherwise the else part will be executed. Here we can have more than one condition, this is how you do it.

if(x>5)
  System.out.print("Condition 1 is met");
else if(x>3)
  System.out.print("Condition 2 is met");
else if(x>4)
  System.out.print("Condition 3 is met");
.......
.......
else
  System.out.print("all conditions are false");

Switch Case

This is also another method of selecting the appropriate decision. Using this you can't implement all that you can do with an IF or IF ELSE or IF ELSE IF. But when you can implement something using this method, the amount of code will be less than previous cases and it will be easier to implement.

switch(x)
{
   case 1: System.out.print("x=1");
                break;
   case 2: System.out.print("x=2");
                 break;
   case 3: System.out.print("x=3");
                 break;
  default:   case 1: System.out.print("x is not 1, 2 or 3");
}

If the value of the x is 1, this simple code will print x=1, else if x is 2 it will print x=2... and for when x is 3 it will print x=3. The default case is just like else part in the IF ELSE IF statements.

We will discuss other control structures in the next blog post.
Resources:
# http://en.wikipedia.org/wiki/Java_(programming_language)

2 comments:

Its all about friendly scientific conversation here at One Science Fact. I'd love to hear your thoughts about this blog.

Be sure to check back again because i always reply to every single comment