English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I want to write a c program to print 1-100 numbers using only "IF" condition and "using no loops". My teacher gave me a hint that we can use continue, break

please help me

2006-10-06 03:21:56 · 7 answers · asked by Anonymous in Computers & Internet Programming & Design

7 answers

I agree with Rawlyn - any teacher teaching GOTO and LABELS, as some of the other answerers suggest, has no business teaching the stuff.

But I don't think that is what your teacher is suggesting.

Notice how the other posters aren't mentioning break and continue?

Break and continue are loop-based statements (break can also be used in switch statements, but that is what is going on here).

If your teacher is suggesting the use of break and continue, your teacher is implicitly suggesting the use of a loop - just not an if loop.

Use a while (1) loop - one of the most powerful loops in C. "While (1)" is always true, unless you break out of the loop.

n = 1
while (1)
{
if (n > 100)
break;
printf ("%d\n", n);
n++;
continue; //note - "continue" here is redundant, but it can be used anywhere to send the code back to the start of the loop.
}

If your teacher means something else than this, do come back and let us know.

But break and continue are both loop-based statements.

2006-10-06 04:48:07 · answer #1 · answered by TJ 6 · 0 0

Your teacher gave you wrong hint: "continue" may be used only within a loop but your requirement/restriction is "using no loops".
All examples with "if... goto" sent by others are quite right: they do not use loops.

2006-10-06 04:23:34 · answer #2 · answered by alakit013 5 · 0 0

#include
int main()
{
static int number =0;
printf("%d\n",number);
if(number != 100)
{
number ++;
main();
}
return 0;
}

The program should be easy to understand.

The program uses recursion and does not use goto, for or while statements (which can be considered as programming construct for loops)

2006-10-06 03:59:58 · answer #3 · answered by Achint Mehta 3 · 0 0

Achint Mehta's solution with main() calling itself is very flaky, because it's almost as bad style as labels and goto, which Rawlyn is quite right about.

static void print_num (int i)
{
printf ("%d\n", i);
if (i < 100)
print_num (++i);
}

int main (void)
{
print_num (1);
return 0;
}

Excuse the lack of indentation. I put it there, but Yahoo! Answers took it away again.

2006-10-06 04:20:03 · answer #4 · answered by Anonymous · 2 0

Any teacher teaching you to use goto and label should be shot. If you're coding in BASIC on a ZX81 then goto is possibly acceptable - but in no other circumstance.

Rawlyn.

2006-10-06 04:00:38 · answer #5 · answered by Anonymous · 2 0

i=1;
label1: printf("%d",i);
if (i++ <100) goto label1;

one means,but check the format of goto and label. I forgot it already. Goto and label is discouraged.

Do you mean your also not allowed to use goto and label?

2006-10-06 03:30:37 · answer #6 · answered by eternalvoid 3 · 0 0

int c=0;
repeat:
if(++c<=100)
{
printf("%d ",c);
goto repeat;
}

2006-10-06 03:30:32 · answer #7 · answered by Woimy 2 · 0 0

fedest.com, questions and answers