-2
$\begingroup$

Let's say your designing a new language with syntax and semantics similare to other C-style languages (C#, Java, C++, JS, etc). You want the language to have if-statements.

Does it need support for else-statements? Is there anything you can't do with the early return design pattern?

edit

The question was if you could design a new language without else statements, or if there would be some algoritms in such a language that would be impossible to code. In other words; is else necessary or simply a convenience?

$\endgroup$
7
  • 2
    $\begingroup$ else is not a kind of statement, it is a part of an if statement. There is no language I know of where you can write else as a statement by itself. Your question also doesn't say what you mean by "need" ─ need for what purpose? If the goal is only to have "syntax and semantics similar to other C-style languages" then the answer is yes or no depending on what you mean by "similar". It's also unclear what benefit you might expect from a language not having else clauses. $\endgroup$
    – kaya3
    Nov 5, 2023 at 15:09
  • 1
    $\begingroup$ While the question is on topic IMO, it is extremely trivial; as @kaya3 said, what benefit is there not to have else? $\endgroup$
    – Seggan
    Nov 5, 2023 at 15:17
  • 1
    $\begingroup$ Nonetheless, the answer would be "no", because you can evaluate the predicate into a local variable and then have a second if with a not to mimic the behavior of the else part. However, nobody would do that because it would require extra code and real code would likely have a performance penalty because users would reevaluate predicates. $\endgroup$
    – feldentm
    Nov 5, 2023 at 15:19
  • $\begingroup$ @kaya3: In some interpreted languages, an "if" statement doesn't contain other statements, but rather manipulates a combination of state variables which suppresses normal execution of other statements unless/until an "else" or "endif" is executed. $\endgroup$
    – supercat
    Nov 5, 2023 at 20:07
  • 1
    $\begingroup$ @Knarf In that case, I think what you want is Turing-completeness. (In short, it's a convenience.) $\endgroup$
    – user23013
    Nov 7, 2023 at 6:09

2 Answers 2

3
$\begingroup$

Consider a typical conditional statement.

if (condition) {
    // Do stuff if true
} else {
    // Do stuff if false
}

In general, you should be able to rewrite this to not use else.

let c = condition;
if (c) {
    // Do stuff if true
}
if (!c) {
    // Do stuff if false
}

Replacing else with an early return gets a little trickier.

if (some_environment_variable()) {
    os.write_to_file("The environment variable is true!")
} else {
    os.write_to_file("The environment variable is false.")
}

os.write_to_file("Both cases considered!")

This could be transformed to

fn test_env_var() {
    if (some_environment_variable()) {
        os.write_to_file("The environment variable is true!")
        return
    }
    os.write_to_file("The environment variable is false!")
    return
}

test_env_var();
os.write_to_file("Both cases considered!")

But just because you can do this does not mean you should. Examples in the real world are not as trivial as these. Imagine being forced to write 10 different functions with early returns instead of a simple if/else chain.

$\endgroup$
1
  • 1
    $\begingroup$ I'm not an adherent, but the proponents of else-less programming would probably disagree with you. $\endgroup$
    – Barmar
    Nov 7, 2023 at 22:06
0
$\begingroup$

The ability to evaluate conditions and have different portions of the following code execute based upon their values is useful, and would be far less useful if the it were not possible to have some code's execution conditioned on a condition being true while other execution is conditioned upon it being false.

On the other hand, I don't think there's necessarily anyhing magical about the "if/else" paradigm. In some cases, it might be useful to design a language around flags and combinations thereof, where instead of writing something like:

if (x==y)
{
  equalAct1();
  commonAct1();
  equalact2();
}
else
{
  unequalAct1();
  commonAct1();
  unequalact2();
}

one could instead write:

            setflag equalPos (x==y)
equalPos:   equalAct1();
!equalPos:  unequalAct1();
            commonAct1();
equalPos:   equalAct2();
!equalPos:  unequalAct2();

For many tasks, repeating execution conditions would be far more trouble than it would be worth, but for some tasks involving many combinations of conditions which should be handled almost identically, being able to separate out what pieces of code should execucte for what combinations of conditions might be helpful.

$\endgroup$
3
  • $\begingroup$ This could be structured in/with tables, if tables ever come to source code. $\endgroup$
    – Pablo H
    Nov 10, 2023 at 14:42
  • $\begingroup$ @PabloH: That reminds me of something I've long thought (for almost 30 years), which is that programming languages should allow embedding of formatting information via standard means. At the time, I was mainly coding in assembly language rather than C, and the interest would have been having a means by which text files could indicate to editors how they should set up things like tab stops, but I would also want a means of auto-formatting comment sections to the right of a block of code. Nowadays, I think other details are more important, such as allowing identifiers... $\endgroup$
    – supercat
    Nov 10, 2023 at 15:51
  • $\begingroup$ ...to have a canonical name spelled with ASCII text as well as an optional human-readable name which may be shown using whatever representation is most understandable for humans. If a program which uses e.g. Hebrew human-readable identifiers needs to be read by someone who doesn't know Hebrew, showing the code with canonical names would allow someone who doesn't know Hebrew to edit the code without having to know the Hebrew alphabet and how to type all the letters therein. $\endgroup$
    – supercat
    Nov 10, 2023 at 15:54

Not the answer you're looking for? Browse other questions tagged .