Monday, August 07, 2006

A Program with and without the go-to statement.

The goto statement makes a C Programmer's life only worse. Because of goto, it becomes hard to debug programs and find out and correct errors. With goto, the sequence of program execution changes abruptly, and so it also becomes very difficult to understand the programs.

Of course, we can do without goto. But the only thing is that we need to combine the other instructions. One such example is provided here. The first program makes use of the goto statement, which appears to be fascinating.
But then, as said earlier, the effect of goto can be realized by using other statements as well. That is being demonstrated in the next program.

Program with goto:

#include<stdio.h>
main()
{

int i, j, k;
for (i=1; i<=3; i++)

{
for (j=1; j<=3; j++)

{
for (k=1; k<=3; k++)

{
if (i==3&& j==3&&k==3)

goto out;
else
printf("%d %d %d \n", i, j, k);

}
}
}

out:
printf("Out of the loop at last!");

}



Program without goto:


#include<stdio.h>
main()
{

int i, j, k;
for (i=1; i<=3; i++)

{
for (j=1; j<=3; j++)

{
for (k=1; k<=3; k++)

{
if (i==3&& j==3&&k==3)

continue;
else
printf("%d %d %d \n", i, j, k);

}
continue;
}
continue;
}

printf("Out of the loop at last!");

}





The File can be downloaded at:
Download File (with goto)

Download File (without goto)

1 comment:

surya said...

become a follower for free C,C++ And vb code of my blog:
http://www.a2zhacks.blogspot.com to learn C,C++,VB programming.