954,479 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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. so you could do

do{
printf("\nEnter valid integer less than 20: ");
scanf("%d", &x);
}while(x > 0 && x <= 20);

for(i = 0; i < x; i++){
printf("*");
}

etc...

Dewey1040
Junior Poster
133 posts since Dec 2008
Reputation Points: 17
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.

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
Moderator
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
 

Splitting hairs somewhat further, a better formatting for the printf with the long string is:

printf("\nYou have entered an invalid integer.\n"
           "Please enter a number smaller or equal to 20:\n");

String literals separated only by whitespace are concatenated by the compiler. A useful fact.

nucleon
Posting Pro in Training
478 posts since Oct 2008
Reputation Points: 163
Solved Threads: 91
 
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.

Dewey1040
Junior Poster
133 posts since Dec 2008
Reputation Points: 17
Solved Threads: 3
 

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.

death_oclock
Posting Whiz
393 posts since Apr 2006
Reputation Points: 129
Solved Threads: 45
 

haha, yeah that gets annoying but he makes sure we learn EVERYTHING, definitely the coding, and knowing how to 'doodle' what is going on in the code, and memory templates, and all that good shtuff.

Dewey1040
Junior Poster
133 posts since Dec 2008
Reputation Points: 17
Solved Threads: 3
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You