Monday, January 27, 2014

Control Structures - Loops (while)

If you guys haven't read the previous posts linked with this post, please read them first and come back here :)
#1 Control Structures - If, If else, If else If
#2 Control Structures - Loops (for)

Ok, now you have a basic idea about what a loop is, and the for loop too. Now I will describe what is a while loop. While loop is used when we don't know the number of iterations in the loop. And the minimum number of iterations is zero.

This loop also works a bit similar to the for loop. Below I have an example code and the syntax.

while(<condition>)
{
   <statements>
}

Finite loop example:

int i = 0;
while(i<5)
{
   System.out.println("This is i: "+i"); //prints i when it is less than 5
}

Infinite Loop example

while(true)
{
   System.out.println("This will run forever!!");
}
While loop has the same arguments as in the for loop. But is has arranged in a different way. You will see that the starting index is not specified in the syntax, you can use that if you need a starting index for your code.

This works a bit simpler than the for loop. This only checks the condition, if the condition returns a true value, the code written between the curly brackets will run.
Resources: stepbystep.com

Tuesday, January 21, 2014

Control Structures - Loops

This post is linked with this, which we have talked about the IF, IF ELSE, IF ELSE IF and SWITCH CASE. So please make sure you read that and then come to this.

So you already know there are 2 types of control structures. The first one we have already discussed, the Conditional Statements. What we have left with is the Iterative structures. Simply the looping structures or the repeating structures. We can divide up to 2 types in this category.

  1. Infinite loops
  2. Finite loops
Let us first discuss about the 3 looping structures.

For loop

The syntax of the for loop is as follows:
for(startingIndex; condition; increment) //you can either use increment or decrement
{
    //codes to loop
}
Example for a finite loop:
for(int i=0; i<5;i++)
{
    System.out.println("This will print 5 times");
}
Example for a infinite loop:

for(int i=0;true;i++)
{
    System.out.println("Thisis i: "+i);
}

The code I have illustrated in the example executes like this. First when the interpreter comes into the for loop, it checks what the starting index variable and keeps them in a memory location. Then it checks the condition, if the condition returns a TRUE value, interpreter moves on to the code block surrounding with the curly brackets. After it has been executed, the interpreter goes to the increment or decrement part of the loop and executes it. Then the next step for the interpreter is to check the condition, if it returns a TRUE value, it goes on to the code block to loop...



There are two more looping structures, which I will explain in the next article. Enjoy and practice the for loop before you read the next :)

Resources: 
Images: 

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)

Saturday, January 18, 2014

Measurements

We all know that we can measure things with certain units that we all have agreed upon. There are two
Vernier caliper
systems at work at the current time; Metric System and the Imperial System. Both of these measure with


  1. Length
  2. Area
  3. Volume
  4. Mass and Weight
The standard in scientific measurement is mostly the metric system. But still there are countries that use imperial system instead. While the Metric system measures using a decimal system imperial system uses otherwise. It doesn't have a pattern to my point of view. But the Metric system also known as the SI (International System of units) has a well organised pattern and a prefix for most of the points. Since the majority of usage, we will discuss only about the Metric system.



There are 3 sub systems in Metric System.
  1. Centimeter-Gram-Second System (CGS)
  2. Meter-Kilogram-Second System (MKS)
  3. Meter-Tonne-Second System (MTS)
We can use either of these systems as per the scope of our measurement. Normally we use MKS system for our usages. Hence we measure
  • Length by Meters: suffix 'm'
  • Area by square meters: suffix 'm^2'
  • Volume by meter cube: suffix 'm^3'
  • Mass and weight by Kilograms: suffix 'kg'
There are a lot yet to be explained. To keep the post simple, I'd stop here. Will meet you with another Post!

Resources:
Images
#2 www.craftsmanspace.com
Information from: http://en.wikipedia.org/wiki/Metric_system and http://en.wikipedia.org/wiki/Imperial_units

Friday, January 17, 2014

The Speed of Light



The speed of light is the amount of distance the light travels for a unit of time. More over it is 299,792,458 m/s to be exact. Some times we take this value to be 3x10^8 as it is easy for mathematical calculations and this is commonly denoted as c. As in the popular equation E = mc^2 (c here is the speed of light and, m is a mass). This is the maximum speed in which any matter, energy or any other thing can travel.


For short distances this is a speed that the human eye would not be able to pick up. Which means it is almost instantaneous. But for long distances such as sun to earth, this would be very noticeable. Did you know that the time to travel the distance between the earth and the sun is about 8 mins and 17 seconds? This means we would not notice if something happens to sun immediately but we would know that after 8 mins! Amazing right?


This speed can be different to which the light is travelling. For transparent materials such as air, glass or water, the speed of light is less than c. The ratio between the speed of light and the speed of light through another material is called the reflective index of the material. This is the reason to refraction or reflection of light off materials.




Light Years is a unit to measure distance. It is the amount of distance the light travels in an year. This ads up to about 9.4605284 x 10^15m, That is a long distance!


Picture courtesy: http://scienceblogs.com/

Resourses:

Wiki: http://en.wikipedia.org/wiki/Speed_of_light

And my own physics teacher at school! :)