LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Help with simple loop program in C (https://www.linuxquestions.org/questions/programming-9/help-with-simple-loop-program-in-c-590414/)

Exsiss 10-08-2007 10:53 PM

Help with simple loop program in C
 
New problem:Okay I now need to do the same program as below but without using continue. I have no idea how I am supposed to do this, so can someone lead me in the right direction? Thanks!











Old post: I'm having a bit of trouble with the following program I am supposed to make:

Quote:

Write a program to print out a simple counter for odd numbers between 0 and 9, inclusive, using a for loop and a continue statement. The for loop must use a postfix increment of the loop variable (counter++). Add a brief comment at the beginning of the file describing what the program does.

The program output should look like:

counter = 1

counter = 3

counter = 5

counter = 7

counter = 9
The script I have for it is:

Quote:

int main ( )
{

int counter;

for (counter=1; counter < 10; counter++)

{
if (counter == 2 || counter == 4 || counter == 6 || counter == 8);
continue;
printf(" counter = %d\n", counter);
}


return(0);
}
When I compile it in Shell it does not come up with any warnings, but when I try to run it, nothing happens. I've tried changing things around, but no matter what I do, nothing happens when I run the program. Any suggestions? Thanks!

P.S. I'm a beginner at C so if you can keep the programming lingo to a way I can understand it, that would be great!

sid18 10-08-2007 11:00 PM

Quote:

Originally Posted by Exsiss (Post 2917938)
I'm having a bit of trouble with the following program I am supposed to make:



The script I have for it is:



When I compile it in Shell it does not come up with any warnings, but when I try to run it, nothing happens. I've tried changing things around, but no matter what I do, nothing happens when I run the program. Any suggestions? Thanks!

P.S. I'm a beginner at C so if you can keep the programming lingo to a way I can understand it, that would be great!

hi
i think you have given semicolon after if statement... and u wanted to put continue under if condition if i am right... so what is happening, even if the "if " condition is coming it has no effect on continue and continue will execute every time in loop giving no chance for printf 2 print.. so no answer is printed.... remove that semicolon after if statement and run it again...

Exsiss 10-08-2007 11:04 PM

Ahh, that was it. Thanks a bunch! Hopefully this is the last time I post a problem that is caused by misplaced semi-colons.

sid18 10-08-2007 11:10 PM

hi

i think u have given semicolon after "if" statement.. beacuse of which if has no effect on "continue" .. so even if "if" condition happens or not continue executes giving no chance 4 printf to print in whole loop..so nothing is printed remove semicolon after if statement and then execute..

Exsiss 10-09-2007 01:25 AM

Okay, I'm a little embarrassed to keep asking questions, but I have another question. I edited my original post so that my new problem is there. If someone can help me out with it, that would be great

graemef 10-09-2007 01:26 AM

In C you are not restricted to having a loop increment by just one each time. The i++ can be replaced by i+=3 which will increment the count by three or i*=2 which will double the cunt each time. With that in mind you can simplify your code.

Exsiss 10-09-2007 02:35 AM

Thanks for the help!

chrism01 10-10-2007 06:48 AM

Also, I HIGHLY recommend always using parentheses to delimit if(){...} blocks. Never rely on the implied-next-line-only syntax.
As you have seen, it frequently leads to obscure problems.


All times are GMT -5. The time now is 12:06 AM.