Question/ doubt about something in C
Posted by Zedy28@reddit | learnprogramming | View on Reddit | 11 comments
I am very new to programming, I am just learning the basics at school and have a question. In language C, is it mandatory to use else after I used an else if?
ToThePillory@reddit
Give it a go.
Or Google it.
As a programmer, a habit you need to form is solving problems yourself.
Don't ask people here, go and find out for yourself.
Budget_Putt8393@reddit
There are also "code standards" that companies/organizations adopt or follow. There is a chance that your professor believes that code missing an else is harder to follow/understand in the long run.
What they should have said is "you should not skip the else". But, as the compiler pointed out, it is a preference rather than a requirement.
aqua_regis@reddit
Try it. Write some code with and without it, compile it, run it.
Then, report back your findings.
This is the best way to learn. Try before asking. Experiment. Play around.
Zedy28@reddit (OP)
I did, I just needed confirmation because my teacher said it’s mandatory, but when I try it for myself it works even without else
nerd4code@reddit
There are three kinds of context for
if
:Case 1 is most appropriate where either outcome of
cond
is reasonable.Case 2 is used0 to take some special action if
cond
, and then run some code regardless.Case 3 might be used with any control transfer statement, infinite loop, or nonreturning function call (e.g.,
break
,continue
,return
,longjmp
,exit
,abort
,for(;;) something();
), and should be preferred for error, unusual/unexpected, or one-off cases in order to get them out of the way and resume focus on more important cases. Treatelse
as subdividing the reader’s attention.You can restructure any
if
-per-se into anif
-else
, of course—e.g., Case 2 is justbut there’s no reason to do that as a human, unless it’s on the way to some grander refactoring. And vice versa, Case 1 might become
or
Now, if you use a conditional expression (
cond ?ifyes : ifno
), then you do always need the “else”/ifno
branch, because otherwise the expression might have no value. (Elder GNU-dialect compilers would actually let you omit the “then”/ifyes
asa ?: b
, but only as a shorthand fora ? a : b
, but that’s nonstandard and mostly not supported any more.)captainAwesomePants@reddit
You will sometimes run into disagreements between what people tell you and what seems to be the case. When this happens, what the compiler itself does is ultimately true (you can't convince the compiler that it's wrong), but you have another weapon: the language spec. Most programming languages have some standards body describing how the language works in minute detail, and C is definitely no exception. For example, here's a PDF containing a fairly up to date C language spec: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2310.pdf
If you head to page 109 (section 6.8.4), you'll find the description of how if statements work. They are described using this grammar:
That means that, in C, an if statement can either take the form
if (expression) statement
, or it can take the form ofif (expression) statement else statement
. Astatement
is a bit tougher to describe, but basically it's either a single command ("printf()") or it's several commands with curly braces around it.Anyway, you can either just know that your teacher is wrong (or that you misunderstood what they were saying), or you could, if you wanted to start a fight, bring the spec to the teacher.
aqua_regis@reddit
There you have your answer.
Either your teacher is wrong or you tore it out of context.
Jealous_Tomorrow6436@reddit
perfect response. couldn’t have said it better myself
MaxAndDylan4Ever@reddit
100% what this person said.
0sted@reddit
else statements are never mandatory in the c language.
captainAwesomePants@reddit
Nope!
That's perfectly legal!