Why is this program giving no output?

361 views Asked by At

I have a program which is not working due to a for loop in it. I'm pasting a working snippet of code here:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int numLoop = 19;
    int counter;
    int maxloops = 25;
    int takenNum1 = 9, takenNum2 = 14, takenNum3 = 17, takenNum4 = 21, takenNum5 = 24;

    for (counter=1; counter==maxloops; counter++)
    {
        printf("%d \n", counter);

        if (counter == numLoop)
        {
            break;
        }

        if (counter == takenNum1 || counter == takenNum2 || counter == takenNum3 || counter == takenNum4 || counter == takenNum5)
        {
            counter++;
            continue;
        }
    }

    return 0;
}

The expected output is: 1 2 3 4 5 6 7 8 10 11 12 13 15 16 18 19

Nothing is being printed.

4

There are 4 answers

3
Sourav Ghosh On BEST ANSWER

The for loop condition appears buggy. You want to write counter != maxloops instead of counter==maxloops.

Otherwise, the loop condition is not met and the loop body is not at all executed.

That said, as per your requirement,

  1. you need to move the checking block

    if (counter == takenNum1 || counter == takenNum2 || counter == takenNum3 || counter == takenNum4 || counter == takenNum5)
    {
        counter++;
        continue;
    }
    

    before the printf() statement to avoid unconditional printing.

  2. The for loop condition should really look like

    for (counter=1; counter < numloop; counter++)
                             ^^^^^^^^^^
    

    as you want the output to be limited to 19.

0
Vimal Bhaskar On

Buddy ur code looks all messed up. understand the concept of looping. In for loop Listen to me closely.

U have kept the condition logically wrong. The loop will check for condition. And you have written as the counter should equal to maxloop which will never satisfy in ur code as the code would exit wheb the counter reaches the value 19(break is given when the counter reaches 19). Hope that solves the first part.

Secondly please note that breaking of the loop should happen post displaying the values. For clear understanding i have given the code below. Hope this helps out.

#include <stdio.h>
#include <stdlib.h>
int main()
{
int numLoop = 19;
int counter;
int maxloops = 25;
int takenNum1 = 9, takenNum2 = 14, takenNum3 = 17, takenNum4 = 21, takenNum5 = 24;
for (counter = 1; counter <= maxloops; counter++)
{
if (counter == takenNum1 || counter == takenNum2 || counter == takenNum3 || counter == takenNum4 || counter == takenNum5)
{
continue;
}
printf("%d \n", counter);
if (counter == numLoop)
break;
}
getchar();
return 0;
}  
0
srivelayutha raja On

you may need to verify the logic. If the output needs to be maximum of 19 iterations. Also, the inner-if condition should be checked before printing the counter.

sample pseudo code as below.

for (counter=1; counter<=numLoop; counter++)
{
    if (counter == takenNum1 || counter == takenNum2 || counter == takenNum3 || counter == takenNum4 || counter == takenNum5)
    {
        continue;
    }
 printf("%d \n", counter);

}
1
Bob Jarvis - Слава Україні On

I'd like to take advantage of the teachable moment here. This appears to be a simple matter of misunderstanding the clauses in a for statement. A for statement is just a shortcut way to write a while loop, with the initialization and "after" code built in. The for statement has three clauses in its parenthesized list - (init ; while ; after). So let's use as and example a for statement which is written as

for(i = 0 ; i < 10 ; i++)
  {
  do_something();
  }

The above for loop could be rewritten using a while loop as

i = 0;

while(i < 10)
  {
  do_something();

  i++;
  }

As you can see, the for is a lot more compact.