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.
After I ran the program I get nothing. Does that mean it will just keep running forever. j++
#include <stdio.h>
int main(){
int i=0,j=0,k=0;
while(((i < 5) || (j < 20)) && (k != 5)){
if((j % 3) == 0){
k++;
i = i + 2;
}
i--;
j++;
}
return 1;
}
Any help would be geat
No, it will not run forever. Eventually the value of k will hit 5 and the loop will exit. If you want some output on the screen, you might want to add something like
printf("i=%d, j=%d, k=%d\n", i, j, k);
right after the line "i = i + 2". It also helps if you include your code into the code tags, it will be more readable, like this:
Code:
#include <stdio.h>
int main(){
int i=0,j=0,k=0;
while(((i < 5) || (j < 20)) && (k != 5)){
if((j % 3) == 0){
k++;
i = i + 2;
printf("i=%d, j=%d, k=%d\n", i, j, k);
}
i--;
j++;
}
return 1;
}
Funny that you're returning 1. Is there any particualr reason for that?
Another thing. Try reading your code as if it were just plain text. It's a good way to understand what the program is doing. In your case it will look like this:
Make i, j and k zero, then, while the condition "(i is less than 5 or j is less than 20) and k is not five" is true, do the following:
if j is divisible by three, increment k and add 2 to i.
subtract 1 from i
increment j by one.
Just to add to what Uncle_Theodore said about reading the code a good self-test would be to decide what the values of i, j and k would be each time the code prints out the values, and what the final values will be - before you actually run the code. Were you correct? If not why were you wrong? So long as you understand those questions you will have learnt something from the code.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.