[SOLVED] printing a through g using a while loop in c language
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I just initialized the character variable ch to the string value 'A' and I think that is what fixed my error. Before that initialization you were right, the value that I had initially stored was all nonsense. Thank you again.
Click here to see the post LQ members have rated as the most helpful post in this thread.
What you have there will work. It's a little overly complex for my taste (for example, why is the variable a running from 65 to 72 instead of 0 to 7? why are you using ++a instead of a++? just little nitpicky things like that), but it will work consistently.
I know you already solved this, but sometimes it helps to see other solutions to the same problem. Here is what I would probably do for this:
Code:
#include <stdio.h>
int main (void)
{
char a;
for (a='A';a<='G';a++) printf("%5c",a);
printf("\n");
return 0;
}
End result is the same, but it's a little easier to follow, and would probably execute a bit faster since you only have one variable being incremented every iteration of the loop instead of two. Just another way of approaching the problem.
@suicidaleggroll
I think that it could work that way using the for loop as well. You are right. I only chose the while loop for the practice. Also, I tried changing the while condition using the range from 0 to 7, and that did not work. I have to keep the condition from ++ch < = 72, or it will give me an infinite loop.
You know you can compare against character literals too, right? The value 72 is not particularly useful (unless you can remember the ASCII values). I find it more readable to use the character you're interested in comparing with (as suicidaleggroll did above). With a while loop, this is (C++; obviously you can use printf() rather than cout), e.g.
Also, I tried changing the while condition using the range from 0 to 7, and that did not work. I have to keep the condition from ++ch < = 72, or it will give me an infinite loop.
You must not have done it correctly.
Code:
int a = 65;
while ( ++a <= 72 )
{
do stuff with ch
}
is exactly the same as
Code:
int a = 0;
while ( ++a <= 7 )
{
do stuff with ch
}
Last edited by suicidaleggroll; 04-09-2012 at 03:49 PM.
I am going to try some more with the code, that you are talking about. Using different conditions for the while loop. I think I did it wrong when I put the values in myself, I will try again.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.