How can I tell a program to continue executing the next line of code in c++

Hi,

Is there a way to tell the program to continue reading the next line of code whithing a nested IF statement?

The reason I want to do this is because the two "else" statements in the following sample (the main and the nested else) will contain the same exact code and I don't want to repeate it twice. I know I can do this by creating a function and calling it from each else statement but I was just wondering if what I'm asking is even possible without using a function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    if(1 < 2)
    {
	// yes 1 is less than 2

        if(5 > 10)
        {
          // do something
        }
        else
        {
          // no, 5 in not greater than 10
	  // here is where I want to tell the program to continue reading the next else statement
	  // since it contains the code I want, otherwise I would need to add the same
          // code here again
        }
        
    }
    else
    {
        // doing something else
    }


Something like...


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    if(1 < 2)
    {
	// yes 1 is less than 2

        if(5 > 10)
        {
          // do something
        }
        else
        {
	   continue; // go to the next line
        }
        
    }
    else
    {
        // doing something else
    }


Sorry if what I want to do is just crazy.

Thanks
I don't understand your question. Unless you put a loop in, i.e. do {...} while(condition); or for(int idx=0; idx<total; ++idx){...} etc, then the next line of your program will be run...
a switch case ( you can also express a default case with that construct ) ?
Line 5 will always be true and is likely to just be optimized out. What are you trying to test here?
Last edited on
Line 5 will always be true and is likely to just be optimized out. What are you trying to test here?


Erm, I don't think 5 is greater than 10 lol.

I think I might know what he is asking. Perhaps what he means is how can he after going through the nested if-branch execute what's in the else statement in the first if-branch.

If that is indeed what is being asked, no. That doesn't work. It already tested for that if-branch and will skip any other scenarios part of it.
Last edited on
Aaarrgghh! Good catch Austin J.

@ OP: Line 5 will always be false, and likely be optimized out.
The code you presented doesn't make sense, but..

1
2
3
4
5
6
7
8
9
if ( x )
{
    if ( y )
        do_something() ;
    else
        do_something_else() ;    
}
else
    do_something_else() ;


can be rewritten as:

1
2
3
4
if ( x && y )
    do_something() ;
else
    do_something_else();
@Austin J

I think I might know what he is asking. Perhaps what he means is how can he after going through the nested if-branch execute what's in the else statement in the first if-branch.

If that is indeed what is being asked, no. That doesn't work. It already tested for that if-branch and will skip any other scenarios part of it.


This answers my question, thanks a lot and sorry about the confusion.

Thank you all very much.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
bool ElseThing = false; 

if(x < y)
{
  do something
  
  if(p > z)
  {
    do something more
  }
  else
  {
    ElseThing = true;
  }

}
else
{
  ElseThing = true;
}

if(ElseThing)
{
  do it!
}
Topic archived. No new replies allowed.