1

I am writing a code in which some process is done in if block. But if result of process is not aa expected, I want to execute else block.

if(condition1)
{
// Some processing
   if(another condition)
        //Do some task;
   else 
        //Execute else of outer block;
}
else
{
}
4
  • 16
    Don't. Just don't do it. If you have common code that needs to be executed from multiple places, put it in a function that you call when needed. Or refactor the code. Commented Jun 4, 2019 at 11:27
  • 1
    Or you can go with if, else if and else? Of course this does not mean you should not put it in a function as suggested. Commented Jun 4, 2019 at 11:30
  • 2
    @John What led you to conclude that OP is using C++ rather than C? Commented Jun 4, 2019 at 11:32
  • @KamalPancholi, you tagged both C and C++, but they're not compatible tags so I removed C. If you want C answers please re-tag the question with only C. Don't put both C and C++. C++ answers might use constructs C doesn't have, and C answers are sometimes poor C++ answers. Commented Jun 4, 2019 at 11:48

4 Answers 4

9

You can do this by rewriting your if condition:

if (condition1) {
    // Some processing
}

if (condition1 && another condition) {
    // Do some task;
} else {
   // Execute else of outer block;
}
Sign up to request clarification or add additional context in comments.

3 Comments

I don't understand the downvote on this answer - aside from a potential issue with the re-evaluation of condition1 it's by far the clearest of the answers presented thus far, including mine. It's there something missing?
Yeah I made a mistake. I'll restore it. I wrote a convoluted answer and thought this was wrong. But in fact, this is a simplified version of mine.
@AvinKavish: Made a trivial edit to circumvent the locked-in downvote rule.
4

While the admonitions against such construction are certainly worth considering in earnest, it is possible to do what you ask in a structured way using a switch statement with a fallthru.

e.g.:

switch(condition1)
{
    case true:
        // some processing
        if(condition2)
        {
            // Do some task;
            break;
        }
        [[fallthrough]]; // or just comment: fallthru if not using c++17
    case false:
        // execute else of outer block;
}

5 Comments

"explicit int() casts for clarity". i.e., They offer no confusion and may help the reader who sees this usage for the first time.
For me it is confusion, not clarity.
OK, sure. Edited.
Well, that's c++17, so I didn't want to assume (since the OP had originally been tagged C as well, there's no telling what version of C++ this is)
@Konrad Rudolph 's Answer is also good. But I liked this one more because of possibly better performance. I don't have to evaluate condition1 twice.
3

You can rely on the short-circuiting property of && and the expression separator operator , and write

if (condition1 && (some processing, another condition)){
    // Do some task
} else {
    // The outer block
}

Here some processing and another condition are only evaluated if condition1 is true.

This does have the advantage that condition1 is only evaluated once which is useful if it has side-effects.

It's also succinct but is not really feasible if some processing is not a simple expression (it works nicely if it's a function call).

3 Comments

In some context, this might not be doable due to a MISRA rule being violated (right-hand side of short-circuit operator cannot have side effect)
this cannot work. As "some processing" are so many lines of code. And if I put all into one line, it would become unreadable.
@KamalPancholi: That could well be the case in your case, but answers are not necessarily targetted at the OP. In some instances, such as where some processing is brief or a function call, this solution can work well.
1

Another technique to add to existing answers:

bool skip_X = false;
if (condition1)
{
    // Some processing
    if (another condition)
    {
        //Do some task;
        skip_X = true;
    }
}
if (!skip_X)
    ....

This avoids evaluating condition1 twice, which can be problematic if it has side effects or values in the expression could be changed by processing inside the if or even in another thread.


That said...

How can I jump from if block to else block without using goto?

The language has goto for a reason, and - just like people who say "how can my constructor report an error without throwing an exception" - by artificially restricting yourself from using the language feature clearly best suited to your need you create worse code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.