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?
Related
I read Why I don't need brackets for loop and if statement, but I don't have enough reputation points to reply with a follow-up question.
I know it's bad practice, but I have been challenged to minimise the lines of code I use.
Can you do this in any version of C++?
a_loop()
if ( condition ) statement else statement
i.e. does the if/else block count as one "statement"?
Similarly, does if/else if.../else count as one "statement"? Though doing so would become totally unreadable.
The post I mentioned above, only says things like:
a_loop()
if(condition_1) statement_a; // is allowed.
You can use ternary operator instead of if...else
while(true) return condition_1 ? a : b;
while seems redundant here if the value of its argument is always true so you can simply write
return condition_1 ? a : b;
Yes, syntactically you can do that.
if/else block is a selection-statement, which is a kind of statement.
N3337 6.4 Selection statements says:
selection-statement:
if ( condition ) statement
if ( condition ) statement else statement
switch ( condition ) statement
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.
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.
In fortran 90, does an if stop statement require a closing endif?
example:
if(foo.eq.1) stop
!do some stuff
Is do some stuff part of the loop or does stop imply endif as the program is ended?
There are mainly two places (apart from the arithmetic if) where the if keyword can be met.
Firstly it is the logical if statement
if (condition) statement_if_true
If the condition is true, the statement_if_true is executed. Anything that follows is not part of the if statement. There is no then and no end if here.
Secondly there is the if conditional construct
if (condition) then
body with statements
end if
The body can contain any number of statements or constructs and must be followed by end if. The then keyword is obligatory for the construct and the body begins on a new line after the then.
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