While loop, do-while loop and for

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2009
Posts: 54
Reputation: chriscross86 is an unknown quantity at this point 
Solved Threads: 0
chriscross86 chriscross86 is offline Offline
Junior Poster in Training

While loop, do-while loop and for

 
0
  #1
Feb 20th, 2009
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;


}
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,442
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 118
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: While loop, do-while loop and for

 
1
  #2
Feb 20th, 2009
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.
  1. #include <stdio.h>
  2.  
  3. int main() {
  4. int x;
  5.  
  6. printf("Enter an integer:\n");
  7. scanf("%d", &x);
  8.  
  9. if ( !((x <= 20) && (x > 0)) ) {
  10. printf("\nYou have entered an invalid integer.\nPlease enter a number smaller or equal to 20:\n");
  11. }
  12.  
  13. while ( (x > 20) || (x <= 0) ){
  14. printf("\nEnter a valid integer:\n");
  15. scanf("%d",&x);
  16. }
  17.  
  18. printf("\nvalid");
  19.  
  20. return 0;
  21. }

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:
    1. while ( x-- != 0 ) {
    2. printf("*");
    3. }
    This will keep decrementing x and displaying one asterisks at a time, until x == 0.

  • do-while:
    1. do {
    2. printf("*");
    3. } while ( --x != 0 );
    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--).

  • for:
    1. for (; x != 0; --x) {
    2. printf("*");
    3. }
    Once again, the same thing is happening, except the code looks a little different...

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 96
Reputation: Dewey1040 is an unknown quantity at this point 
Solved Threads: 3
Dewey1040 Dewey1040 is offline Offline
Junior Poster in Training

Re: While loop, do-while loop and for

 
0
  #3
Feb 20th, 2009
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
  1. do{
  2. printf("\nEnter valid integer less than 20: ");
  3. scanf("%d", &x);
  4. }while(x > 0 && x <= 20);
  5.  
  6. for(i = 0; i < x; i++){
  7. printf("*");
  8. }
etc...
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,889
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 302
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: While loop, do-while loop and for

 
0
  #4
Feb 20th, 2009
Originally Posted by Dewey1040 View Post
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...
Last edited by niek_e; Feb 20th, 2009 at 9:55 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,442
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 118
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: While loop, do-while loop and for

 
0
  #5
Feb 20th, 2009
True, what about if you're making a graph... and need to plot some x & y coordinates, what would be more readable?
  1. for (int i = 0; i < 10; ++i) {
  2. for (int j = 0; j < 10; ++j) {
  3. graph.plot(i, j);
  4. }
  5. }
or...
  1. for (int x = 0; x < 10; ++x) {
  2. for (int y = 0; y < 10; ++y) {
  3. graph.plot(x, y);
  4. }
  5. }
Just use what seems appropriate, and don't stick with i, j, k just because they are more commonly used.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: While loop, do-while loop and for

 
0
  #6
Feb 20th, 2009
>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.
Last edited by Aia; Feb 20th, 2009 at 10:50 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,442
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 118
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: While loop, do-while loop and for

 
0
  #7
Feb 20th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 476
Reputation: nucleon has a spectacular aura about nucleon has a spectacular aura about 
Solved Threads: 91
nucleon's Avatar
nucleon nucleon is offline Offline
Posting Pro in Training

Re: While loop, do-while loop and for

 
0
  #8
Feb 20th, 2009
Splitting hairs somewhat further, a better formatting for the printf with the long string is:
  1. printf("\nYou have entered an invalid integer.\n"
  2. "Please enter a number smaller or equal to 20:\n");
String literals separated only by whitespace are concatenated by the compiler. A useful fact.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 96
Reputation: Dewey1040 is an unknown quantity at this point 
Solved Threads: 3
Dewey1040 Dewey1040 is offline Offline
Junior Poster in Training

Re: While loop, do-while loop and for

 
0
  #9
Feb 20th, 2009
Originally Posted by niek_e View Post
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...
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: While loop, do-while loop and for

 
0
  #10
Feb 21st, 2009
I hope that you at least recognize the importance of the programming concepts rather than memorizing the text of the book, like your neurotic teacher seems to think.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC