Credits

Wednesday, February 11, 2009

More For Statement Examples and Break From Loops

Sometimes you have to repeat loops more than the usual and break from them whenever we need it.

We can use a double loops and a break to do these tasks respectively.

Check the programs below:



[+/-] show/hide




/* a program to display a multiplication table of 15 by 10 */
#include stdio.h /*write stdio.h in between <>*/
main()
{
int row,col;
for (row=1;row<=15;row++) {for(col=1;col<=10;col++) printf("%d\t",row*col); printf("\n");} getche(); } /*A program to exit from loops */ #include stdio.h /*write stdio.h in between <>*/
main()
{
int row,col;
for (row=1;row<=15;row++)
{for(col=1;col<=10;col++)
printf("%d\t",row*col);
printf("\n");
if (row=10) /* this means that if the outer loop has reached 10, it will exit from this loop*/
break;}
getche();
}

What are the outputs of these two programs?
Translate this into Bloodshed C++. Simply replace the printf command with cout.

AddThis