While loop, do-while loop and for
hi,
there...ive come up with these codes and they work.but i dont know how to modify the program so that it prints sequence of asterisks in a single line based on the input gained using :
1.while
2.do-while
3.for
please help...
#include
int main()
{
int x;
printf("Enter an integer\n");
scanf("%d", &x);
if((x<=20)&&(x>0))
{
}
else
{
printf("You have entered an invalid integer.Please enter a number smaller or equals to 20");
}
while((x>20)||(x<=0)){
printf("\nEnter a valid integer \n");
scanf("%d",&x);
}
printf("valid");
return 0;
}
chriscross86
Junior Poster in Training
54 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
In future, remember to use code tags .
Here is the code how I would have formatted it, after cleaning it up a little for you.
#include <stdio.h>
int main() {
int x;
printf("Enter an integer:\n");
scanf("%d", &x);
if ( !((x <= 20) && (x > 0)) ) {
printf("\nYou have entered an invalid integer.\nPlease enter a number smaller or equal to 20:\n");
}
while ( (x > 20) || (x <= 0) ){
printf("\nEnter a valid integer:\n");
scanf("%d",&x);
}
printf("\nvalid");
return 0;
}
prints sequence of asterisks in a single line based on the input gained using :
1.while
2.do-while
3.for
First, all three of these will do a very similar thing (loop until a condition is met), so the code for each will look quite similar.
So, if I understood correctly, you want to make a loop which will print as many asterisks' as the user inputs.while:
while ( x-- != 0 ) {
printf("*");
}
This will keep decrementingx and displaying one asterisks at a time, until x == 0.
do-while:
do {
printf("*");
} while ( --x != 0 );
Same applies for this one, but as the order in which the code executes has been changed, you have to decrementx before comparing it with 0 (by changing it to --x instead of x--).
for:
for (; x != 0; --x) {
printf("*");
}
Once again, the same thing is happening, except the code looks a little different...
Hope this helps.
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
idk if its the same for everybody, but my teacher takes points off unless we use i, j, or k for loops.
For real? I agree that 'i,j,k'are quite commonly used as vars for a loop, but I can think of countless examples where i,j or k would be a very bad choice.
I personally only use 1-character-variables (such as i,j,k) if the loop is very short (no more then 3-4 lines). And I try to never use the same name twice in a program. That's to keep the code more readable.
I think your teacher is probably just a lazy guy and wants his students to use these names so that he can easily identify the loops in a code or something...
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
True, what about if you're making a graph... and need to plot some x & y coordinates, what would be more readable?
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 10; ++j) {
graph.plot(i, j);
}
}
or...
for (int x = 0; x < 10; ++x) {
for (int y = 0; y < 10; ++y) {
graph.plot(x, y);
}
}
Just use what seems appropriate, and don't stick with i, j, k just because they are more commonly used.
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
>True, what about if you're making a graph... and need to plot some x & y coordinates, what would be more readable?
Since we are splitting hair, let's declare those int variables outside those for loops. Portability to the lowest common denominator should be a concern.
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
Since we are splitting hair, let's declare those int variables outside those for loops. Portability to the lowest common denominator should be a concern.
Switching between the C and C++ forum can be so confusing sometimes :)
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129