LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-08-2007, 10:53 PM   #1
Exsiss
LQ Newbie
 
Registered: Oct 2007
Posts: 12

Rep: Reputation: 0
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!

Last edited by Exsiss; 10-09-2007 at 01:25 AM. Reason: New Problem
 
Old 10-08-2007, 11:00 PM   #2
sid18
LQ Newbie
 
Registered: Aug 2007
Posts: 19

Rep: Reputation: 0
Quote:
Originally Posted by Exsiss View Post
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...
 
Old 10-08-2007, 11:04 PM   #3
Exsiss
LQ Newbie
 
Registered: Oct 2007
Posts: 12

Original Poster
Rep: Reputation: 0
Ahh, that was it. Thanks a bunch! Hopefully this is the last time I post a problem that is caused by misplaced semi-colons.
 
Old 10-08-2007, 11:10 PM   #4
sid18
LQ Newbie
 
Registered: Aug 2007
Posts: 19

Rep: Reputation: 0
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..

Last edited by sid18; 10-08-2007 at 11:11 PM.
 
Old 10-09-2007, 01:25 AM   #5
Exsiss
LQ Newbie
 
Registered: Oct 2007
Posts: 12

Original Poster
Rep: Reputation: 0
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
 
Old 10-09-2007, 01:26 AM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 10-09-2007, 02:35 AM   #7
Exsiss
LQ Newbie
 
Registered: Oct 2007
Posts: 12

Original Poster
Rep: Reputation: 0
Thanks for the help!

Last edited by Exsiss; 10-09-2007 at 02:39 AM.
 
Old 10-10-2007, 06:48 AM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with simple shell script for loop abefroman Programming 2 10-25-2005 08:26 PM
[c shell] simple for loop saiz66 Programming 1 09-28-2004 07:02 PM
Simple for loop lappen Programming 10 09-26-2004 06:36 AM
Simple while loop problem (newbie question) Seventh Programming 3 09-07-2004 12:00 PM
awk - simple 'for' loop doing my nut... davee Programming 2 06-30-2004 08:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration