## Jun 25, 2014

### C Programming #24: Loop - break

This section discusses the usage of break within a loop.

Till now we saw only condition becoming false will make the loop to exit. But C provides keyword "break", when used inside the body of the loop, it exits the loop. Break can be used with any type of loop (while, do while and for) and even multiple times inside same loop too.

Typical use of it as follows:

for(initialize_counter; condition; change_condition) {
statement1;
if(condition_x) {
break;
}
statement2;
}

while(condition) {
statement1;
if(condition_x) {
break;
}
statement2;
}

do{
statement1;
if(condition_x) {
break;
}
statement2;
}while(condition);

In above usages, if condition_x becomes true, loop is just exited.

Program to find GCD (Greatest common divisor) of two number
It is also called as GCF (Greatest common Factor), HCF (Highest common Factor).  Find more information regarding GCD here.

Strategy:
Say we have two number a and b. Lets first find the smallest among both a and b. (say we assign it to m). Check if m divides both and a and b, if it divides then we have our GCD otherwise keep on decrementing m and repeating.

#include <stdio.h>
int main()
{
int a = 24, b = 36;
int m;
int counter;

/* Assign minimum value of
a and b to m */
m = (a > b) ? b : a;

for(counter = m; counter >=1; counter--) {
if((a % counter == 0) && (b % counter == 0)) {
break;
}
}
printf("GCD of %d and %d is %d\n", a, b, counter);
return 0;
}


Output of the above code is

GCD of 24 and 36 is 12

Explanation-
m = (a > b) ? b : a;  is finding the minimum of a and b which is quite obvious, if not read here. for loop is count down loop that is counting backwards from m to 1. Each time m is checked if it is dividing both a and b which is done by (a % counter == 0) && (b % counter == 0). Once this condition is met we get out of loop via break. Count down loop is used because we find the greatest divisor.

I always think usage of break causes confusion. The reasons are:
1. There might be multiple break in loop.
2. Multiple exit from loop is confusing as it goes against normal loop flow.

Same could be re-written using while loop without using break statement as follows.

#include <stdio.h>

int main()
{
int a = 24, b = 36;
int m;

/* Assign minimum value of
a and b to m */
m = (a > b) ? b : a;

while( (a%m != 0) || (b%m != 0)) {
m--;
}

printf("GCD of %d and %d is %d\n", a, b, m);
return 0;
}


Hence if you use break in future it means you have not understood your looping construct properly. It can be always re-written without using break which might be less elegant .

Food for thought
Q: Find LCM of two number ?
A: LCM stands for Least common multiple. You can find more information here.
Strategy:
Say we have two number a and b. Lets first find the minimum of both say it is min.
Let's keep on finding multiples of min and check if it is divisible by max number.

#include <stdio.h>

int main()
{
int a = 12, b = 15;
int min, max;
int counter;

min = (a < b) ? a : b;
max = (a > b) ? a : b;

counter = 1;
while((min * counter) % max != 0) {
counter ++;
}
printf("LCM of %d and %d is %d \n", a, b, min * counter);
return 0;

}


Output of above program is

LCM of 12 and 15 is 60


Always have clarity of problem, and map the problem properly to loop counter and condition. Thought process before coding is most important to write precise and elegant code.

## Links

Quiz - Not Yet written !!
Next Article - C Programming #25: Loop - continue
Previous Article - C Programming #23: Loop - do while
All Articles -
C Programming