• 0

do...while printing numbers 1 though 9


Question

Hello everyone!

 

I'm writing code that prints the numbers 1 though 9 (Including 9).  This is the code I have:

#include "stdio.h"

void main()
{
	int count;
	count = 1;

	{
		do 
		(count = count + 1);

		while
		(count < 10);
			printf("%d", count);
	printf("\n");
	}
}

However when I run this it is not printing the numbers 1 through 9, its only printing out the number 10.  Can someone please help me?  I really don't know what I'm doing wrong and I am unfamiliar with how to use do ...while loops in c.

 

 

post-505691-0-36078000-1379792700.png

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

Two things I see you need to do.

 

First move the statement printf("%d", count); after count = count+1 and before the while since right now it's not in the loop.

 

Second you need to set count= 0, because if you leave it at 1 and start to run to program it's going to print 2 first since you have the statement count +1 which will set count to 2 the first time through the loop.

Link to comment
Share on other sites

  • 0

The problem you are trying to solve is trivial in virtually any programming language. It would probably behoove you to read a beginner's book that teaches basic programming logic intermixed with lessons in a specific programming language. Once you understand one language - and more importantly have a good grasp on how to program - learning other languages becomes much easier. Therefore I strongly recommend against starting with C as your first language. Although C is very powerful, it lacks the object-orientation that is of paramount importance in most other high-level languages and has far more caveats that make it very difficult to become a proficient C programmer. Personally I would recommend Python as a good first language in conjunction with the book Learn Python The Hard Way.

 

That said, zman982's analysis of your mistake is correct. You are printing the count after it has reached 10, not as you are iterating like you apparently intended. A do... while loop does not make much sense in this case anyway. It would be much more concise to use a for loop instead.

#include <stdio.h>

int main( int argc, char * argv[] )
{
    for( int i = 1; i < 10; i++ ) printf( "%d\n", i );
    return 0;
}
Link to comment
Share on other sites

  • 0

+xorangekiller, I would love to agree with you about not starting with C, however the institution of higher education that I am in believes that once you take SQL, algorithms and data structures, and VB.NET that you have to take C to get into java/flash.  I have already completed this successfully using a for loop, and a while loop, however this do loop is throwing me for a loop (LOL). 

Link to comment
Share on other sites

  • 0

There are several ways you can construct a loop in C, one of which is a for loop proposed by xorangekiller. However, if you really want to use a do .. while loop, the syntax is

do
{
    // loop body
}
while (condition)

While omitting the curly braces is possible in certain conditions, I'd advise against it, as at this point it will only confuse you, and it's generally considered to be bad style.

 

Remember that this loop, as opposed to while or for will always execute at least once - it performs the do part first, then checks if it should repeat.

 

Other problems that I've found with your code:

#include "stdio.h"

should be

#include <stdio.h>

Your compiler probably fixed it automatically for you, but most of the time you use <file> for built-in headers, and "file" for those included in your project.

void main()
{
    // code
}

should also be fixed to

int main()
{
    // code

    return 0;
}

Again, this is something that your compiler might have fixed, but in Unix environment, it's a standard behaviour that an application returns 0, when it completes without errors.

Link to comment
Share on other sites

  • 0

If you must learn C now, I recommend that you read the C Programming Language (Second Edition). It was written by the original authors of the C language and is by far one of the best programming books I have ever read. It very thoroughly explains the why and how of C from the author's point-of-view to help you gain better a understanding of the language. That is why it sits on my bookshelf as a prominent member of my reference collection.

Link to comment
Share on other sites

  • 0

Ok fellas thank you for all of your help and wonderful insight!  You have been a tremendous help.  I followed your suggestions zman982 and it is working flawlessly!  Thank you so much for everything! 

Link to comment
Share on other sites

  • 0


 

#include <stdio.h>

 

int main(int argc, char* argv[])

{

    int count = 0;

    

    do {

         count++;

         printf("%d\n", count); }

    

    while(count < 9);

    return 0;

}

 

 

 

Link to comment
Share on other sites

  • 0

I really don't know what I'm doing wrong and I am unfamiliar with how to use do ...while loops in c.

If you are working in Visual Studio (which you likely are since you're on Windows), start your program by pressing F10 (step over) and press F10 repeatedly to see program executing line by line. You can see the value of the different variables in your program simply by hovering the mouse cursor over them. This will allow you to visualize exactly what your program does. The earlier you learn to use the debugger, the faster you'll learn programming.

 

Here's a good tutorial on debugging: http://www.codeproject.com/Articles/79508/Mastering-Debugging-in-Visual-Studio-2010-A-Beginn

  • Like 2
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.