| | |
While loop, do-while loop and for
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2009
Posts: 54
Reputation:
Solved Threads: 0
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 <stdio.h>
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;
}
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 <stdio.h>
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;
}
•
•
Join Date: Mar 2008
Posts: 1,442
Reputation:
Solved Threads: 118
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.
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.
Hope this helps.
Here is the code how I would have formatted it, after cleaning it up a little for you.
C Syntax (Toggle Plain Text)
#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
So, if I understood correctly, you want to make a loop which will print as many asterisks' as the user inputs.
- while:
This will keep decrementing x and displaying one asterisks at a time, until x == 0.C Syntax (Toggle Plain Text)- while ( x-- != 0 ) {
- printf("*");
- }
- do-while:
Same applies for this one, but as the order in which the code executes has been changed, you have to decrement x before comparing it with 0 (by changing it to --x instead of x--).C Syntax (Toggle Plain Text)- do {
- printf("*");
- } while ( --x != 0 );
- for: Once again, the same thing is happening, except the code looks a little different...C Syntax (Toggle Plain Text)
- for (; x != 0; --x) {
- printf("*");
- }
Hope this helps.
•
•
Join Date: Dec 2008
Posts: 96
Reputation:
Solved Threads: 3
idk if its the same for everybody, but my teacher takes points off unless we use i, j, or k for loops. so you could do
etc...
C Syntax (Toggle Plain Text)
do{ printf("\nEnter valid integer less than 20: "); scanf("%d", &x); }while(x > 0 && x <= 20); for(i = 0; i < x; i++){ printf("*"); }
•
•
•
•
idk if its the same for everybody, but my teacher takes points off unless we use i, j, or k for loops.
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...
Last edited by niek_e; Feb 20th, 2009 at 9:55 am.
•
•
Join Date: Mar 2008
Posts: 1,442
Reputation:
Solved Threads: 118
True, what about if you're making a graph... and need to plot some x & y coordinates, what would be more readable? or...
Just use what seems appropriate, and don't stick with
C Syntax (Toggle Plain Text)
for (int i = 0; i < 10; ++i) { for (int j = 0; j < 10; ++j) { graph.plot(i, j); } }
C Syntax (Toggle Plain Text)
for (int x = 0; x < 10; ++x) { for (int y = 0; y < 10; ++y) { graph.plot(x, y); } }
i, j, k just because they are more commonly used. >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.
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.
Last edited by Aia; Feb 20th, 2009 at 10:50 am.
Splitting hairs somewhat further, a better formatting for the printf with the long string is:
String literals separated only by whitespace are concatenated by the compiler. A useful fact.
C++ Syntax (Toggle Plain Text)
printf("\nYou have entered an invalid integer.\n" "Please enter a number smaller or equal to 20:\n");
•
•
Join Date: Dec 2008
Posts: 96
Reputation:
Solved Threads: 3
lol i think its the exact opposite of that actually. i think hees such a perfectionist that he just has to give us rules like that to make us learn more things... same reason he makes us learn the exact titles of every chapter, section of the chapter, and every sub-section of the chapter.
![]() |
Similar Threads
- Do/While Loop help (C++)
- problem creating a for loop for an array (C++)
- loop problem (C++)
- Clear getchar in a loop (C)
- Loop...without the loop (Java)
Other Threads in the C Forum
- Previous Thread: bitxor
- Next Thread: how to write IVR code in C
| Thread Tools | Search this Thread |
#include adobe ansi api append array arrays asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc csyntax database directory dynamic execv feet fgets file fork framework frequency function getlasterror givemetehcodez global grade gtkgcurlcompiling hacking hardware highest histogram include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue number odf opensource overwrite owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scripting segmentationfault sequential socket socketprograming standard string systemcall testing threads turboc unix user voidmain() wab windows.h windowsapi






