Analyzing control structures is essential from the perspective of understanding the running time of an algorithm. Sequential structures, looping structures, control structures, recursive calls, and so on are all part of programming languages. We shall examine the complexities of each of them here.

The following is the relationship between the order of growth rate:

O(1) < O(log n) < O(n) < O(n log n) < O(n^{2}) < O(n^{3}) < O(2^{n}) < n! < n^{n}

## Sequential execution

Statements or blocks of statements appear one after the other in sequential structures. Assume B1 and B2 are two blocks of instructions, each of which might contain a single instruction, several sequential instructions, or a collection of complicated instructions.

The execution of B1 and B2 are indicated in the figure to be consecutive. Assume that the time taken by code of B1 is t_{1} and the time taken by code of B2 is t_{2}.

If B1 and B2 are executed in sequential order than the complexity of the entire program will be,

T(n) = t_{1} + t_{2} = max (t_{1}, t_{2})

In a complexity study, the order of code blocks is irrelevant. In general, if the program has m modules (or m functions), then the total complexity of the program is determined by first determining the complexity of each module, i.e. t_{1}, t_{2},â€¦, t_{m}. Find the maximum time for all of them; this is the overall program’s complexity.

T(n) = max (t_{1}, t_{2}, t_{3}, â€¦, t_{m})

T(n) = max (t_{1}, t_{2}, t_{3},â€¦,t_{m}) = max (t_{1}, t_{2}, t_{3}, t_{4})

= max (O(n^{3}), O(n), O(n^{4}), O(n^{2}))

= O(n^{4})

## If else

The execution of B1 and B2 is now conditional on a number of factors. If the condition is met, the B1 block is executed; if the condition is not met, the B2 block is executed.

T(n) = max (t_{1}, t_{2})

T(n) = max (O(n), O(n^{2})) = O(n^{2})

## For loop

For loop executes inner statements n times if loop is set to iterate n times. Following code blocks explain the running time of for loop

```
for (i = 1; i â‰¤ n; i++)
{
Count = Count + 1;
}
```

Cost of the inner statement is O(1) and for loop iterates n times.

```
for (i = 1; i â‰¤ n; i++)
{
for(j = 1; j â‰¤ n; j++)
{
Count = Count + 1;
}
}
```

## While loop

Running time of while loop and for loop is computed in similar way. Complexity of code block is given by total cost of inner most statement in loop. Letâ€™s analyze few code segments containing while loop:

```
while(n > 0)
{
n = n â€“ 1;
}
```

Inner statement executes n times, so T(n) = O(n)

```
i = 1;
while(i â‰¤ n)
{
j = 1;
while(j â‰¤ n)
{
Count = Count + 1;
j = j + 1;
}
i = i + 1;
}
```

Inner most statements executes n times for each value of i, and loop of I itself iterates n times. So,

```
while(n > 0)
{
n = n / 2;
}
```

Here, decrement of n is not in linear order. Value of n is reduced by factor 2, maximum log_{2}n divisions are possible before n reduce to 0. So,

T(n) = O(log_{2}n)

```
while(n > 0)
{
n = n / m;
}
```

In the above code, n reduces by factor m in every iteration, so while loop can iterate maximum log_{m}n times.

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â T(n)Â Â Â =Â Â O(log_{m}Â n)

**Additional Reading**: Analyzing Control Structures [PDF]