14 Replies - 519 Views - Last Post: 02 February 2018 - 02:12 PM Rate Topic: -----

#1 derekromania  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-January 18

how to change switch to if else statement c++

Posted 24 January 2018 - 06:15 AM

void Logger::logging(const int slevels, const string smsgs)

{
	string logging; 


	switch (slevels)
	{
	case 0:
		logging = "[INFO]" + smsgs + '\n';
		vect.push_back(logging);
		lognum++;
		ofs << logging;
		break;

	case 1:
		logging = "[DEBUG]" + smsgs + '\n';
		vect.push_back(logging);
		lognum++;
		ofs << logging;
		break;

	case 2:
		logging = "[ERROR]" + smsgs + '\n';
		vect.push_back(logging);
		lognum++;
		ofs << logging;
		break;

	case 3:
		logging = "[WARNING]" + smsgs + '\n';
		vect.push_back(logging);
		lognum++;
		ofs << logging;
		break;

	default:
		Logger::logging(2, "Invalid\n");
		ofs << logging;
		break;

	}
}

This post has been edited by ndc85430: 24 January 2018 - 06:17 AM
Reason for edit:: Added code tags. Please do this yourself in future.


Is This A Good Question/Topic? 0
  • +

Replies To: how to change switch to if else statement c++

#2 ndc85430  Icon User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 797
  • View blog
  • Posts: 3,256
  • Joined: 13-June 14

Re: how to change switch to if else statement c++

Posted 24 January 2018 - 06:18 AM

Well, how do you think you should make the change? Do you understand how switch works?
Was This Post Helpful? 0
  • +
  • -

#3 Thomas1965  Icon User is offline

  • D.I.C Regular

Reputation: 95
  • View blog
  • Posts: 365
  • Joined: 09-September 16

Re: how to change switch to if else statement c++

Posted 24 January 2018 - 08:20 AM

Why do you want to change it ?
According to the CPP Core Guidelines you should prefer switch of if-else
https://isocpp.githu...s#Res-switch-if
Was This Post Helpful? 0
  • +
  • -

#4 albert003  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 470
  • Joined: 15-December 14

Re: how to change switch to if else statement c++

Posted 24 January 2018 - 09:28 PM

Switch statements can only check against constants. Do you understand how to use switch statements?.
Was This Post Helpful? 0
  • +
  • -

#5 tony jay  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 50
  • Joined: 31-January 18

Re: how to change switch to if else statement c++

Posted 31 January 2018 - 08:04 PM

View PostThomas1965, on 24 January 2018 - 08:20 AM, said:

Why do you want to change it ?
According to the CPP Core Guidelines you should prefer switch of if-else
https://isocpp.githu...s#Res-switch-if

Yes!
Using a switch, if possible, is much preferable to a complicated forest of multiple if...else statements.
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3782
  • View blog
  • Posts: 13,708
  • Joined: 08-August 08

Re: how to change switch to if else statement c++

Posted 01 February 2018 - 07:57 PM

Who needs a forest?
#include <iostream>
#include <string>

using namespace std;

string logging(const int slevels, const string smsgs)
{
	string logging;
	string levels [4] = {"[INFO] ","[DEBUG] ","[ERROR] ","[WARNING] "};
	if(slevels < 0 || slevels > 3) {
		logging = "Invalid\n";
	} else {
		logging = levels[slevels] + smsgs + '\n';
	}
	return logging;
}

int main()
{
	for (int i = -2; i < 5; i++) {
		cout <<"Level " << i << ": " << logging(i, "testing") << endl;
	}
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#7 tony jay  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 50
  • Joined: 31-January 18

Re: how to change switch to if else statement c++

Posted 01 February 2018 - 08:00 PM

Ever heard of general advice?
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3782
  • View blog
  • Posts: 13,708
  • Joined: 08-August 08

Re: how to change switch to if else statement c++

Posted 01 February 2018 - 08:22 PM

Are you saying that my solution doesn't apply generally?
Was This Post Helpful? 0
  • +
  • -

#9 tony jay  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 50
  • Joined: 31-January 18

Re: how to change switch to if else statement c++

Posted 01 February 2018 - 08:33 PM

You remind me of a nightclub bouncer spoiling for a fight...

Actually, nothing in what I said has anything to do with your accusation.

In general terms it is very good advice to avoid a forest of if..else statements in favour of a switch statement, or, perhaps, even another construct altogether.
You, of course, already know this...

The fact that your solution comes to a very manageable and readable if..else construct does not change the general advice I gave in the slightest.
Do I use if..else constructs?
Sure.
But, I abandon them when things start to get messy.
Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7160
  • View blog
  • Posts: 14,924
  • Joined: 16-October 07

Re: how to change switch to if else statement c++

Posted 02 February 2018 - 04:07 AM

I avoid switch for the same reason I use braces on one liners: future proofing.

Consider:
void foo_switch(int n) {
    switch(n) {
        case 1: 
            bar("Alice");
            break;
        case 2: 
            bar("Bob");
            break;
        default:
            bar("Voldemort");
    }
}

void foo_if(int n) {
    if (n==1) { 
        bar("Alice");
    } else if(n==2) { 
        bar("Bob");
    } else { 
        bar("Voldemort");
    }
}


This is a basic one to one: if the OP is still there, hope that helped.

However, in the future, something like this might come up:
void foo_if(int n) {
    if (n==1) { 
        bar("Alice");
    } else if(n==2) { 
        bar("Bob");
    } else if(n<100) { 
        bar("Harry");
    } else { 
        bar("Voldemort");
    }
}


At that point, my switch will simply need to be rewritten: there's just no recovering.

Personally, I find the if-else easier to read. I find the switch syntax to be an outlier in the C lexicon with a greater tendency to become convoluted than standard code blocks. I was disappointed that Stroustrup didn't agree, but then he also leaves the braces off one liners.
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6155
  • View blog
  • Posts: 21,226
  • Joined: 05-May 12

Re: how to change switch to if else statement c++

Posted 02 February 2018 - 05:15 AM

In my opinion, the switch statement is a carry over from the older assembly programming practice of using jump tables. It let the former assembly programmer, now C programmer, to do something that looked familiar.
Was This Post Helpful? 0
  • +
  • -

#12 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7160
  • View blog
  • Posts: 14,924
  • Joined: 16-October 07

Re: how to change switch to if else statement c++

Posted 02 February 2018 - 11:52 AM

It has a certain JMPiness to it, but I don't know. Wikipedia has an interesting entry for it, but it offers no more insight other than a mathy origin: https://en.wikipedia...witch_statement

I mean, you can see how a compiler writer would endorse the thing, but today we have optimizing compilers and I would hope they would produce similar code for either switch or comparable if-else.
Was This Post Helpful? 1
  • +
  • -

#13 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6155
  • View blog
  • Posts: 21,226
  • Joined: 05-May 12

Re: how to change switch to if else statement c++

Posted 02 February 2018 - 12:39 PM

View Postbaavgai, on 02 February 2018 - 06:07 AM, said:

Personally, I find the if-else easier to read.


That is only more readable by breaking the 1TBS rule where every block is suppose to have braces around them. If 1TBS were consistently applied, this would be the result:
void foo_if(int n) {
    if (n == 1) { 
        bar("Alice");
    } else {
        if (n == 2) { 
            bar("Bob");
        } else {
            if (n < 100) { 
                bar("Harry");
            } else { 
                bar("Voldemort");
            }
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#14 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7160
  • View blog
  • Posts: 14,924
  • Joined: 16-October 07

Re: how to change switch to if else statement c++

Posted 02 February 2018 - 01:35 PM

Agreed, which is why I'm not following that rule. :P

When Java came out, the adopted their own K&R variant which I prefer. Found here and here.

I format my C/C++/Java/C# with astyle -A2

Though I expect there are a few places I might diverge from it. The key to indent styles is consistency and I try to be consistent.
Was This Post Helpful? 4
  • +
  • -

#15 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6155
  • View blog
  • Posts: 21,226
  • Joined: 05-May 12

Re: how to change switch to if else statement c++

Posted 02 February 2018 - 02:12 PM

View Postbaavgai, on 02 February 2018 - 03:35 PM, said:

Though I expect there are a few places I might diverge from it. The key to indent styles is consistency and I try to be consistent.

+1010

I can't agree more. I could be reading somebody else's code and be hating their indent style and naming conventions, but as long as they are consistent, I can read it more easily that code with inconsistent style and naming.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1