In eclipse CDT, is there a way to find the loop statement associated with a break (or continue) statement? - eclipse-cdt

I have some "complicated" C code that has multiple nested for and while loops as well as nested if statements. Within the loops, there are break statements buried down in if statements. Can eclipse show me which loop the break (or continue) statement is going to act on? Like a hotkey, F3 for example.

Related

How to use [[(un)likely]] at do while loop in C++20?

do [[unlikely]]
{...}
while(a == 0);
This code can be compiled.
But is this the correct way to tell compiler that a is usually non-zero.
Structurally, this is a correct way to say what you're trying to say. The attribute is placed in a location that tags the path of execution that is likely/unlikely to be executed. Applying it to the block statement of the do/while loop works adequately. It would also work within the block.
That having been said, it's unclear what good this would do practically. It might prevent some unrolling of the loop or inhibit prefetching. But it can't really change the structure of the compiled code, since the block has to be executed at least once and the conditional branch has to come after the block.

MARIE Assembly if else

Struggle with MARIE Assembly.
Needing to write a code that has x=3 and y=5, is x>y then it needs to output 1, if x<y it needs to output one,
I have the start but don't know how to do if else statements in MARIE
LOAD X
SUBT Y
SKIPCOND 800
JUMP ELSE
OUTPUT
HALT
Structured statements have a pattern, and each one has an equivalent pattern in assembly language.
The if-then-else statement, for example, has the following pattern:
if ( <condition> )
<then-part>
else
<else-part>
// some statement after if-then-else
Assembly language uses an if-goto-label style.  if-goto is a conditional test & branch; and goto alone is an unconditional branch.  These forms alter the flow of control and can be composed to do the same job as structure statements.
The equivalent pattern for the if-then-else in assembly (but written in pseudo code) is as follows:
if ( <condition> is false ) goto if1Else;
<then-part>
goto if1Done;
if1Else:
<else-part>
if1Done:
// some statement after if-then-else
You will note that the first conditional branch (if-goto) needs to branch on condition false.  For example, let's say that the condition is x < 10, then the if-goto should read if ( x >= 10 ) goto if1Else;, which branches on x < 10 being false.  The point of the conditional branch is to skip the then-part (to skip ahead to the else-part) when the condition is false — and when the condition is true, to simply allow the processor to run the then-part, by not branching ahead.
We cannot allow both the then-part and the else-part to execute for the same if-statement's execution.  The then-part, once completed, should make the processor move on to the next statement after the if-then-else, and in particular, to avoid the else-part, since the then-part just fired.  This is done using an unconditional branch (goto without if), to skip ahead around the else-part — if the then-part just fired, then we want the processor to unconditionally skip the else-part.
The assembly pattern for if-then-else statement ends with a label, here if1Done:, which is the logical end of the if-then-else pattern in the if-goto-label style.  Many prefer to name labels after what comes next, but these labels are logically part of the if-then-else, so I choose to name them after the structured statement patterns rather than about subsequent code.  Hopefully, you follow the assembly pattern and see that whether the if-then-else runs the then-part or the else-part, the flow of control comes back together to run the next line of code after the if-then-else, whatever that is (there must be a statement after the if-then-else, because a single statement alone is just a snippet: an incomplete fragment of code that would need to be completed to actually run).
When there are multiple structured statements, like if-statements, each pattern translation must use its own set of labels, hence the numbering of the labels.
(There are optimizations where labels can be shared between two structured statements, but doing that does not optimize the code in any way, and makes it harder to change.  Sometimes nested statements can result in branches to unconditional branches — since these actual machine code and have runtime costs, they can be optimized, but such optimizations make the code harder to rework so should probably be held off until the code is working.)
When two or more if-statements are nested, the pattern is simply applied multiple times.  We can transform the outer if statement first, or the inner first, as long as the pattern is properly applied, the flow of control will work the same in assembly as in the structured statement.
In summary, first compose a larger if-then-else statement:
if ( x < y )
Output(1)
else
Output(one)
(I'm not sure this is what you need, but it is what you said.)
Then apply the pattern transformation into if-goto-label: since, in the abstract, this is the first if-then-else, let's call it if #1, so we'll have two labels if1Done and if1Else.  Place the code found in the structured pattern into the equivalent locations of the if-goto-label pattern, and it will work the same.
MARIE uses SkipCond to form the if-goto statement.  It is typical of machine code to have separate compare and branch instructions (as for a many instruction set architectures, there are too many operands to encode an if goto in a single instruction (if x >= y goto Label; has x, y, >=, and Label as operands/parameters).  MARIE uses subtract and branch relative to 0 (the SkipCond).  There are other write-ups on the specific ways to use it so I won't go into that here, though you have a good start on that already.

A single C++ variable has conflicting values in debugger?

Please see the screenshot below. I noticed some erroneous results in my code so I went to debug.
As you can see, I put a break point when the variable 'latest' is equal to "5". BUT, apparently the application is hitting on this break point even thought 'latest' is equal to "2", not "5". Any idea what is going on here?
Format your code like this (>> denoting the breakpoint):
if (latest == "5")
{
>> ;
}
rather than this:
>> if (latest == "5") {;}
In the latter case the breakpoint is at the if, not at the ; inside the {}.
Cramming too many statements on the same line makes step by step debugging painful and makes the code less readable.
I put a break point when the variable latest is equal to "5"
No, you put a breakpoint where the variable latest is compared to "5". The comparison has to happen before the if statement knows which branch to take.
Your code rather than this:
if (latest == "5") {;}
Only use single-line if statements on a single line
The problem occurs when a single-line if the statement is broken up into two lines. While the compiler sees this as one statement guarded by a single condition, humans often accidentally read this is an if block, whether there are curly braces or not, thanks to the indentation. Humans notice the indentation, the compiler does not.
if (latest == "5")
;
If the statement you’re guarding is small, and not worth the extra heft of curly braces, just put it on the same line.

Pseudocode elseifs syntax

In pseudocode, is it more better to be indenting 'else if' statements almost as though the 'if' part is nested within the 'else' part?
As you see here, I feel like the 3rd/5th/7th line of code should not be indented, but instead at the same level as the 'if' statement. Is it true that these 'if' part should be nested within the preceding "else" part? If so, why aren't there more 'End if' statements, surely there wouldn't be one 'End if' which closes 3 'if' statements.
Here is an example from php code (random language, this following concept applies to any other language anyways):
As you see here, the elseif statements are not 'nested' - as if the 'elseif' statement are two separate statements but the 'elseif' is instead, one coherent statement. This 'elseif' does indeed have the same meaning as an 'if' nested within an 'else' yes, however, surely that does not mean the 'else if' statements should be nested within the previous 'else if' (refer back to 1st screenshot).
In pseudo-code, which style of indentation is correct in syntax and is more readable to other programmers?

Is it break my for loop in c++?

for(size_t i=0;i<vec.size();i++){
if(n>vec[i]){
a=i;
break;
}
}
in this example, am I breaking the if statement or the for loop?
The break statement is used to break out of a switch or iteration statement i.e. a while, do or for loop. The C++ draft standard section 6.6.1 The break statement says:
The break statement shall occur only in an iteration-statement or a switch statement and causes termination of the smallest enclosing iteration-statement or switch statement; control passes to the statement following the terminated statement, if any.
Since the if is not an iteration statement or a switch then the break will leave the for loop.
A break statement ends only the do, for, switch, or while statement that immediately encloses it. It doesn't break out of an if statement, so your code is breaking out of the loop.
It will break from the for loop.
In loops, the break statement ends execution of the nearest enclosing
do, for, or while statement.
Source: Microsoft docs