问题
To begin, the code that I have got up to this point is below.
The calculator does everything I want it to, minus the fact that it still includes negative numbers in its calculations. I would like for it to do nothing if the number presented as the radius is less than zero, yet still calculate if the number is non-negative. However, I am having issues with using an if statement. I have not used these before, as I am really just a beginner. I just need a push in the right direction. Do I need to have an "else"? Any help is greatly appreciated!
#include <stdio.h>
int radius, area;
main()
{
printf("Enter radius: ");
scanf("%d", &radius);
{
if (&radius > -1); {
area = (int)(3.14159 * radius * radius);
printf("Area = %d\n", area);}
else {
return(0); }
}
return(0);
}
回答1:
Remove semi-colon
remove semi-colon this lines
if (&radius > -1); {
should be
if (radius > -1) {
Should do this for easier tracking if-else statement
change these lines
printf("Area = %d\n", area);}
return(0); }
to
printf("Area = %d\n", area);
}
return(0);
}
Here is style for if-else statement, I think it's easier for you to track your code
if (condition) {
statements;
}
else if (condition) {
statements;
}
else {
statements;
}
回答2:
The C compiler doesn't care about formatting, in theory you can do whatever you like, and there's no consensus for what is/isn't "proper formatting".
However, most programmers stick to a specific style so that it's easier to read the source code; and if you're working in a team then it's nice if all programmers on the team use the same style so that all of the source code is consistent. To achieve that, there may be a formal "style guide", and if there is you should follow it.
Beyond that, there are some common rules that almost everyone follows:
nested blocks that are delimited by braces are indented somehow (with either "N space characters" or "N tab characters") relative to the parent block
cases of a switch statements will be exceptions to the indentation rules. Typically each case's statements are indented even though there's no braces; and the case keyword itself may or may not be indented by the parent switch's braces.
either all braces are always on a line by themselves; or starting braces are at the end of the line and ending braces may be at the start of a line containing a related statement
when a block consists of a single statement; either it always uses braces and is takes up a line by itself, or it never uses braces and shares the same line as its parent.
the
else if
pair is always an exception to the "block consists of a single statement" rule (theif
is a single statement that is never treated as a separate block, and people pretendelse if
is a singleelseif
keyword).
What this means is that (depending on who got their way when arguing about it) this might acceptable:
int main() {
int area;
printf("Enter radius: ");
scanf("%d", &radius);
switch(radius) {
case 0:
return 0;
case 1:
return 1;
}
if (&radius > -1) {
area = (int)(3.14159 * radius * radius);
printf("Area = %d\n", area);
} else return -1;
return area;
}
..and this might also be acceptable:
int main()
{
int area;
printf("Enter radius: ");
scanf("%d", &radius);
switch(radius)
{
case 0:
return 0;
case 1:
return 1;
}
if (&radius > -1)
{
area = (int)(3.14159 * radius * radius);
printf("Area = %d\n", area);
}
else
{
return -1;
}
return area;
}
回答3:
if (&radius > -1);
Lose the &
before radius
and lose the semicolon:
if ( radius > -1 ) { ... }
Do I need to have an "else"?
In this particular case, no. You're not doing anything in the else
branch that you aren't already doing unconditionally (i.e., the return
statement). You'd only need an else
branch if you needed to take a specific action only when the condition fails, such as printing an error message:
if ( radius > -1 )
{
...
}
else
{
printf( "Radius needs to be non-negative!\n" );
}
return 0;
As for how to format, there are multiple styles with their own advantages and disadvantages. The style I prefer has the {
and }
on their own lines:
if ( condition )
{
// if branch
}
else if ( another condition )
{
// else if branch
}
else
{
// else branch
}
This style is easiest for me to read, makes it clearer where blocks begin or end, etc. It also increases the use of vertical space, so you get less code in a window, meaning you may have to scroll back and forth a bit more.
There's what's known as the K&R style as it was popularized by Kernighan & Ritchie, where the opening {
is on the same line as the if
or else
:
if ( condition ) {
// if branch
}
else if ( condition ) {
// else if branch
}
else {
// else branch
}
Not quite as easy to pick out where a block starts, but not too bad, and somewhat more compact, so you can fit more lines of code into a limited space.
Then there's a style I flat-out hate, which puts the closing }
on the same line as the else
or else if
:
if ( condition ) {
// if branch
} else if ( condition ) {
// else if branch
} else {
// else branch
}
I find this style very hard to read and follow. But, it's the most compact style, and commonly used in situations where screen real estate is very limited (such as in a PowerPoint slide in a lecture or tutorial or something).
Note that the compiler doesn't care which style you use, as long as the opening and closing braces match up properly. You could put everything on a single line with almost no whitespace like:
if(radius>-1){area=(int)(3.14159*radius*radius);printf("Area = %d\n", area);}else{print("Radius needs to be non-negative!\n");}
Formatting only matters for you and anyone who has to read/maintain your code. Whatever style you pick, be consistent with it.
来源:https://stackoverflow.com/questions/54522817/using-if-statements-in-basic-c-programming-how-can-i-properly-format-them