C++ elegant way to combine switch with if and variable assignment [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
My code looks like this
enum possible_cases; //assigned somewhere
bool decision; //assigned somewhere
//basically the default action for my possible_cases
int value = 10;
do_something(value);
switch (possible_cases)
{
case 0:
//assume covered by do_something(value)
break;
case 1:
if ( decision )
{
value = get_other_value();
do_something(value);
}
break;
case 2:
value = get_other_value(); //will return same value as in case 1
do_something(value);
break;
}
As you see
it has to run do_something() with one specific value
it might have to run do_something() with other values additionally, and the list of cases might grow, to a point, where having bools is impractical
but overall I am not happy as it is kinda redundant and I think there is a way to do it better.
I would like to stay with enum of cases
Edit:
It seems not clear where the problem is:
I see the call of the same function in 3 places, while I assume I could reduce it to 2 as I know I need to run it only 2 times at max.
It is really more the aesthetic aspect

The redundant part in your code is do_something(value)
So I would suggest you to separate that part of the code
enum possible_cases; //assigned somewhere
bool decision; //assigned somewhere
//basically the default action for my possible_cases
int value = 10;
switch (possible_cases)
{
case 1:
if ( decision )
value = get_other_value();
break;
case 2:
value = get_other_value(); //will return same value as in case 1
break;
}
do_something(value);

By adding extra layer you might do:
std::optional<int>
get_additionnal_value(Epossible_cases possible_cases, bool decision)
{
//basically the default action for my possible_cases
const int value = 10;
switch (possible_cases)
{
default:
case 0: return std::nullopt;
#if 1
case 1: return decision ? get_other_value() : value;
#else
case 1: if (!decision) return value;
[[fallthrough]];
#endif
case 2: return get_other_value(); //will return same value as in case 1
}
}
and then
do_something(10);
if (auto opt_value = get_additionnal_value(possible_cases, decision)) {
do_something(opt_value);
}

Related

Various options in switch case [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I was trying to do a quiz in C. 5 answers, only one is correct. I'm using the switch. My doubt is on the case you have the number of the correct answer and the consequence of being correct. But on the other hand, I need to make the other 4 wrong. how can I select multiple answers? is it case 1,3,4,5? P.s. There's also the option of being invalid, and i need a string for each case! pontos mean points and p1 is the answer. Thank you
switch (p1) {
case 2 :
correct answer
pontos = pontos + 1;
break;
case 1, 3, 4, 5 :
wrong answer
pontos = pontos - 1;
default :
Invalid answer
pontos = pontos - 1;
}
You can stack up multiple case statements with the same body, like this:
switch (foo) {
case 1:
case 2:
case 3:
case 5: {
printf("Sorry, wrong answer.");
break;
}
case 4: {
printf("You got it right!");
break;
}
}
Or you could just use a default case to catch everything that's not right:
switch (foo) {
case 4: {
printf("You got it right!");
break;
}
default: {
printf("Sorry, wrong answer.");
break;
}
}

In C++, if break; keyword not exist for Switch case block, both case "b" & default is executed [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
bool condition = true;
string input = "b";
switch (input)
{
case "b":
if (condition)
{
Console.WriteLine("B");
}
default:
Console.WriteLine("Default");
break;
}
C++:
B
Default
you missed a break after case,
bool condition = true;
string input = "a";
switch (input)
{
case "b":
if (condition)
{
Console.WriteLine("B");
}
break; // You missed break;
default:
Console.WriteLine("Default");
break;
}
C# does not allow to execute more than one case which is logically incorrect so prevented by C# compiler.

I want to blink the LED's in reverse order [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want to reverse the code as it complete first time. For example pin 1-pin 2-pin 3-pin 4 (it's complete) now it should run as pin 4-pin 3-pin 2-pin 1.
I wrote this code but it's not working in reverse order. Please guide me in this way.
#include<htc.h>
__CONFIG(1,OSCSDIS & HSPLL);
__CONFIG(2,BORDIS & PWRTDIS &WDTDIS);
__CONFIG(3,CCP2RC1);
__CONFIG(4,LVPDIS & STVREN);
__CONFIG(5,UNPROTECT);
__CONFIG(6,WRTEN);
__CONFIG(7,TRU);
define _XTAL_FREQ 40000000
void delay_sec(unsigned char seconds) // This function provides delay in terms of seconds
{
unsigned char i,j;
for(i=0;i<seconds;i++)
for(j=0;j<100;j++)
__delay_ms(10);
}
void led_display(char a)
{
switch(a)
{
case 0: PORTB=0x01;PORTD=0x08; break;
case 1: PORTB=0x02;PORTD=0x04; break;
case 2: PORTB=0x04;PORTD=0x02; break;
case 3: PORTB=0x08;PORTD=0x01; break;
}
}
void main()
{
TRISB=0x00; TRISD=0x00; char a,b;
while(1) {
led_display(a);
a++;
delay_sec(1);
if(a==4) {
a--;
}
}
}
For doing that, you have to remember in which order you are running (reverse or not). So you will have a variable indicating if the order is reverse or not, and change it when you reach the extremities of your counter (0 and 3)
And you can optimize the code with using 1 and -1 for the variable which remembers order and adding it to a.
Your code will seem like this in your main :
int reverse = 1,
char a = 0;
while(1)
{
led_display(a);
delay_sec(1);
if(a==3)
{
reverse=-1;
}
if(a==0)
{
reverse=1;
}
a+=reverse;
}
Regards.

How to keep track of order in which switch case values are executed? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How can I keep track of the order in which switch case statements are executed?
For example:
while (some_boundary) {
switch (value) {
case a:
do something;
move to next value;
break;
case b:
do something;
move to next value;
break;
case c:
do something;
move to next value;
break;
}
}
I want to know if the switch was executed abc or bac or cab, etc.
Any idea ? Or will implementing via if/else make more sense ?
You can save a vector at every iteration with the value of the corresponding iteration:
std::vector<int> sequence;
while (some_boundary) {
int temp = computeValue(); // Or however you get your value.
sequence.push_back(temp);
switch (temp) {
case a:
//do something;
break;
case b:
//do something;
break;
case c:
//do something;
break;
}
}
Edit: This is assuming that value is set somewhere between the while and the switch, so you can save it in advance. Other option is to include the push_back instruction in every case, but is more "dirty". Preallocating the vector could save some computation time as well.
Edit2: code modified according the suggestions so that it is ensured that a new value will be computed.
If you just want to know (and not save the results) you could just output the value at each iteration. Try
cout << value << endl;
as the first line within the while loop.

Proper use of a switch statement [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm wondering how to use the switch statement, and I have a couple questions.
What is the case '': for? as in, how do I set the conditions?
How many cases can I have?
How do I set the conditions?
It's very simple: assuming an integral type Type you can use:
Type i;
switch (i) {
case x:
// ...
break;
case y:
// ...
break;
// ...
default:
// ...
break; // optional
};
where x, y, ... etc. are values convertible to the integral type Type that you want to check for equality in i.
So for example:
int x = 3;
switch (x) {
case 1:
std::cout << '1';
break;
case 2:
std::cout << '2';
break;
case 3:
std::cout << '3';
break;
default: break;
};
would print:
3
For more detailed informations on the switch statement, please visit this page.
How many cases can i have?
As many as you want.