1. ## Using Loops

I'm in an intro computer science class so I'm very new to programming. Below is the assignment. I did get my program to compile, but it doesn't work properly when you first enter a number between 1 and 10. It only works when you enter it incorrectly first. What did I do wrong? Should I be using a different loop? Any insight would be greatly appreciated!

Goal: Write a program that asks the user for a number to count to (in the range of 1 to 10), once the user has entered in an appropriate number, count to that number

Ask the user to enter a number between 1 and 10.
If the user did so correctly, count from 0 to the number entered by the user.
If the user did not enter the number correctly, tell the user “I’m sorry, that is incorrect.” and then prompt the user to enter the number again. Repeat as many times as necessary.
You must use two different loop types.

Code:
```#include <stdio.h>

int main(void)
{
//DECLARE VARIABLES
int i, stored_number;

//PURPOSE OF PROGRAM
printf ("\nThis program will count from 0 to a number you pick.\n");

//PICK AND STORE NUMBER
printf ("\nPlease enter a number (between 1 and 10):");
scanf ("%d", &stored_number);

//DO LOOP
i=1;
do
{
printf ("\nI'm sorry, that is incorrect.\n");
printf ("Please enter a number (between 1 and 10):");
scanf ("%d", &stored_number);
}       while (stored_number>10);
printf ("\n");

//WHILE LOOP
printf ("\n");
i=0;
while (i<=stored_number)
{
printf ("%d...", i);
i++;
}
printf ("done!");

//END PROGRAM
printf ("\n\n");

return 0;
}```

2. A do ... while loop is always exectued at least once, so the error message is always output. You need a while ... loop.

You'll find that do ... while loops are quite rare in real programs. Almost always you have a null case which means that you don't want to run the loop at all.

3. Thank you for your feedback. I'm sorry, I'm still so new to this so I don't fully understand. I can get rid of the do .... while loop, but then I'm going to need another one since the assignment asks for two different loops. Should I add a for ... loop?

4. Your second ("while()") loop would be better suited as a "for()" loop, allowing you to follow the advice of Malcolm McLean.

5. Okay, I rewrote it to start with a for... loop statement. What would my other loop look like if a user entered a number over 10?

Code:
```#include <stdio.h>

int main(void)
{
//DECLARE VARIABLES
int i, stored_number;

//PURPOSE OF PROGRAM
printf ("\nThis program will count from 0 to a number you pick.\n");

//PICK AND STORE NUMBER
printf ("\nPlease enter a number (between 1 and 10):");
scanf ("%d", &stored_number);

//FOR LOOP
printf ("\n");
for (i=0; i<=stored_number; i++)
{
printf ("%d...", i);
}
printf (" done!");

//END PROGRAM
printf ("\n\n");```

6. You could try using a do while loop to get the number from the user and to display the error message if the number is incorrect
Then use a for loop to print the number.

Code:
```do
{
printf("Enter a number from one to 10: ");
scanf("%d",&stored_number);
if(stored_number > 10)
{
printf("I'm sorry, that is incorrect");
continue;
}
else
break;
}while(1==1);
}```
The 1==1 is to make sure the loop keeps running until stored_number is less than 10.

7. Hmm, the problem with that ^^^ is that it excepts 0 as a valid answer. I would do:

Code:
```    while(number < 1 || number > 10){
puts("Incorrect Input");
scanf("%d", &number);
}```

8. Thank you atac and Dakshin for your help. I just have one problem; the assignment says I'm not allowed to use a break. If I used a while ... loop for a number greater than 10, it would just go on forever, correct? How else could I create a 2nd loop for an incorrect response to the initial prompt?