could someone review my code for odd-even for C? and maybe tell me what i can put more into it Note: this is already error-free
#include <stdio.h> #include <stdlib.h> #include<conio.h> main() { int a, b, c; start: printf("Enter a number: \t"); scanf("%d", &a); b = a%2; if (b==0) { printf("\nThe number is an even number"); } else { printf("\nThe number is odd"); } printf("\n\nWould you like to try again? \n[1] YES\n[2]NO\t"); scanf("%d", &c); if (c==1) { system("cls"); goto start; } else { return 0; } getch(); }
I was always advised to avoid using goto whenever possible in C. Instead of goto, you can use a do-while loop that will keep on evaluating as long as the user enters 1, like do { odd/even code }while (scanf("%d", &c))
To add on to what FoolAroundMath said, which I completely agree with, you should develop coding habits that is conducive to producing readable code. ex: #include <stdio.h> #include <stdlib.h> #include<conio.h> main() { int a, b, c; do { printf("Enter a number: \t"); scanf("%d", &a); b = a%2; if (!b) printf("\nThe number is an even number"); else printf("\nThe number is odd"); printf("\n\nWould you like to try again? \n[1] YES\n[2]NO\t"); } while(scanf("%d", &c)); return 0; getch(); } is MUCH more readable than what you posted initially. I removed the unnecessary brackets after the if and the else statement because you have only 1 statement after each which means you don't need them. Furthermore, b==0 is nonstandard in most languages so it's usually better to use the NOT operator, which is ! Uniform indenting also makes it easier to read, and putting your opening brackets on the same line as a block starts or on the line after is really preference but I prefer to put them on the same line as in main() { rather than main() {
coding habits that are**
why avoid goto?
1 goto is okay, but generally it is considered to avoid them altogether because it improves readability by tons. Goto statements create obstructions in code flow that can create very very confusing code which is very hard to understand, especially when you didn't write it initially. If that's not enough for you, using a goto in a code sample written in anything but assembly language is likely going to keep you from getting a job in Computer Science, for the reasons stated above. Code with lots of goto's is generally known as "spaghetti code" because it is so jumbled.
Here's some more info on the subject: http://en.wikipedia.org/wiki/Structured_programming
uhmm okay...so what do you mean by conducive to producing readable code
lol yeah my wording was a bit poor on that. pretty much you just want to be in a habit of doing things which make your code more readable, if that makes it a bit clearer what I meant.
how is my code not readable?
Your brackets were all over the place, and your indentation varied for statements which should have been at the same "depth". The uniform indentation is useful because it makes it obvious which statements are part of which loop. I'm not saying I couldn't understand it; I'm saying that it's better to create these habits now because when you produce code later in much larger projects, it's going to be much less obvious what you are trying to do when everything is not uniform. Although, I take back what I said about using the ! operator in this case. b == 0 is more clear. I had a brain lapse and was thinking as b of a boolean value, but in this situation you were right.
of b as a boolean value*
For your specfic code, there probably isn't that much different between writing a while loop or using goto. However, in general, the advice is sound: loops produce code that is more easily read by a stranger than goto does. It's the difference between a piece of music with a coda sign telling you to go back and repeat several measures and a Choose Your Own Adventure Book. The coda is like a while loop...it says "repeat this section," is clearly understood, and everyone knows what it means and what to look for. A Choose Your Own Adventure book has pages telling you to go to different pages all over the place, and if you were to just read from page 1 to the end, you wouldn't be able to follow what's going on.
Join our real-time social learning platform and learn together with your friends!