-1

Is it okay if I don't include else{ } every time I use if-else? or should I include it? C++!

#include <iostream>
using namespace std;

int main(){
    int x,sum=0,d=0;
    do{
        cout<<"Enter a number: ";
        cin>>x;
        if(x>0){
            sum+=x;
            d++;
        }
        else{}
    }while(x!=0);
    {
        cout<<"Sum: "<<sum<<endl;
        cout<<"Average: ";
        if(d==0){
            cout<<"0";
        }
        else{
            cout<<(float)sum/d;
        }
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 3
    That's like asking "is a passenger-seat in my car necessary?" – Dai Nov 24 '22 at 03:06
  • 3
    `else{}` is not necessary. It does nothing. – drescherjm Nov 24 '22 at 03:09
  • 5
    Having else block it not necessary. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. https://www.tutorialspoint.com/cplusplus/cpp_if_else_statement.htm – Dark Sorrow Nov 24 '22 at 03:11
  • It _can_ be required to disambiguate nested if-statements not enclosed in braces. But that's not happening in this program. – paddy Nov 24 '22 at 03:28
  • Where did you get the idea that `else {}` could be useful? (I mean as a general rule, ignoring special cases.) Sure, if you don't include the `else {}` part, then you no longer have an `if-else` statement, so strictly speaking the `else {}` might be needed for you to use an `if-else`. However, in that case I would ask why are you so set on using `if-else` instead of `if`? – JaMiT Nov 24 '22 at 03:45
  • 1
    Related question: [https://stackoverflow.com/questions/35388241/does-an-empty-else-clause-have-any-significance-in-c](https://stackoverflow.com/questions/35388241/does-an-empty-else-clause-have-any-significance-in-c) – drescherjm Nov 24 '22 at 03:45
  • 2
    Despite all the "is not necessary" statements, there is one case where it could serve a purpose. This is when you nest `if` statements without braces: `if (A) if (B) doB(); else {} else notA();`. Here, if you remove the `else {}`, the meaning is changed completely: the `else notA();` no longer is the alternative branch of `if (A)`. That said, this case is usually corrected by using braces, not by inserting `else {}`. – j6t Nov 24 '22 at 07:14
  • Seen the comment of j6t, there is nothing opinion-based about this question, so I decided to re-open it. – Dominique Nov 24 '22 at 07:50
  • It's not required by language rules. But there are code guidelines which require it, e.g. MISRA C. It's code standard unrelated to language itself, just a requirement for code maintenance. – Swift - Friday Pie Nov 24 '22 at 08:07
  • 2
    @j6t Code like that would never pass my code review. I would tell the coder to add braces and remove the empty "else" – cptFracassa Nov 24 '22 at 09:19
  • 1
    @j6t It is not possible to use `else {}` in `if` statements without braces. Why? Because the instant you add `else {}`, you've added braces and no longer have "without braces". For the use you envision, you would have to use `else ;` (semicolon instead of braces for the empty branch). Or you could rephrase the situation as "`if` statements without braces except for empty statements". – JaMiT Nov 24 '22 at 10:14

1 Answers1

0

else is not required if it is not necessary, for example, you can write:

if(1)
    cout<<"hello world";

and this would work with no problem