Is it possible to have an 'IF' conditional statement nested within a 'CASE' conditional statement? (pseudocode) - pseudocode

I'm designing a pseudocode version of a programme thingy I made, in which one of the sections is someone inputting a number to select an option. When someone inputs a number, a value from a list is output. I thought using an 'IF' statement nested within a 'CASE' statement would make that task run more efficiently, but I'm not sure if that would still conform to the acceptable 'CASE' statement format. This is what I was envisioning for the first option:
**
CASE category OF
'1' : PRINT "Members who have chosen to work as volunteers,"
IF MemberInfo[2] = 'yes'
PRINT "MemberInfo[0], MemberInfo[1]"
**
The following numbers in the main 'CASE' statement would then follow the same format. Is this okay, or should I just make various 'IF' statements?

It does not makes to use CASE here as we use CASE when there are multiple options to choose from. Given its just one condition and action based on that an 'if' is more appropriate.
An if nested within CASE is not a good programming structure. Go with either CASE or IF.
Input a number as category
If category is a number
then
print list
end if

Sure, that makes sense. Real code can do it, so why not pseudocode?
(But if the other cases have the same format, there's probably a better way to do it: maybe a map lookup or using the category more dynamically, depending on what's changing between each case statement, and what stays the same.)

Related

What is the colon before Fortran if `something:if(some_condition) then`?

I am working through some code other people have written and found a piece of Fortran syntax that I haven't seen yet and don't exactly understand nor can seem to find anything on the web about (probably because I don't know what it's called).
The code looks like this:
bisection_or_ordering:if(ordering /= 'bisection') then
...
do stuff
...
end if bisection_or_ordering
bisection_or_ordering is not a variable and not declared anywhere in the code.
What does this do? What is it for? And what is it called?
The part before the colon is a construct name.
Executable constructs with blocks - if, block, associate, critical, select case, select type and of course do - may optionally have these construct names.
They are useful both for identification (for clarity with nested or long constructs) but also for control under the exit statement (except to escape critical or do concurrent blocks).
The construct name may appear on the closing statement of the block, as in the question's example, but it is optional and must match if present.

How to avoid checking each condition inside if statement which contains complex operation

Let's say I have this code:
if (condition1 || condition2) {
some_complex_operation(); // memory or time wise, doesn't matter
if (condition1) {
doJob1();
}
if (condition2) {
doJob2();
}
}
Now of course normal procedure here would be to leave out the first if condition, but since I want my complex operation to happen only when one of conditions is satisfied I'd want to avoid doing it every time.
Is there syntactically better way to rewrite this part of the code?
Unless the check for your conditions is expensive, I think your code is fine as it is (and if the check is expensive, you could convert them into booleans before the first if).
Alternatives would be:
move the call to the complex operation inside both if's and remove the outer if, but I like it better as it is now, because it's in one block.
depending on the values of your conditions, you could use a switch statement instead of the two if's, but that won't make much of a difference.
If both conditions can't be true at the same time you could place an else before the second inner if. You could even remove the second check and only use else but then you'd have to make sure the number of conditions never changes.
And of course, in general with or, always place the condition that occurs the most first (in both cases) so there's no need to check the second condition when the first evaluates to true.

In Bash, Why `then` is needed in the conditional constructs?

The conditional construct of if command looks like this:
if TEST-COMMANDS; then
CONSEQUENT-COMMANDS;
[elif MORE-TEST-COMMANDS; then
MORE-CONSEQUENTS;]
[else ALTERNATE-CONSEQUENTS;]
fi
And the loop construct of while command looks like this:
while TEST-COMMANDS; do CONSEQUENT-COMMANDS; done
I was wondering why then is needed in if command but not in while command? Why couldn't it be ommited?
do in the while syntax serves a similar purpose to then in the if syntax. They both signify the start of the body of the statement - differentiating it from the condition part of the statement.
The if conditional statement is a compound statement in the shell. The if & then sections of the statement are executed as two parts, the then section is only invoked if the if section ends with an exit status of 0. Both sections may contain multiple statements; therefore, a semi-colon alone is insufficient to separate these sections.
Like #shibley is saying in his answer, the do and then words are used to indicate the beginning of the block of actions to perform.
I have done some research and could not find the historical reasons, so I am going to guess the logical ones. It might be too subjective, so do not hesitate to comment your impressions below.
The bash syntax is quite "symmetrical": Whenever you have an case you finish it with esac. Also, it was designed in a very human way, so it is easily understandable.
That said, if you are in a while loop, it means that you are going to do something while a condition is true. Then, when it is not true anymore you are done.
However, in an if condition, you are saying that if something happens, then something needs to be executed.
In short: do and then are human-readable ways to indicate the same, that is, the beginning of a block of commands to be performed upon a while or if condition.

Qtp dynamic execution order

So I have a keyword driven framework that executes on keywords. In one of the functions I have a if element exist condition. Now if that element doesn't exist I want qtp to not execute the next 3 keyword functions following it. Is there a way to do this? Thank you!
You could a global variable that records the number of keywords that should be skipped. When your element doesn't exist, you could set the skip count to 3. In your framework that reads each keyword, you could first check the current skip count. If it's 0, you execute the keyword normally. Otherwise, you decrease the skip count by 1 and exit without executing that keyword.
Can you not insert a conditional statement?
Therefore if the element exists, you can put the next 3 statements within the loop? Else, do nothing. Then have the code follow on as normal?
If you aren't comfortable in expert view, this shows how to do it in keyword view also:
http://www.softwaretestinghelp.com/conditional-loop-statements-qtp-tutorial-4/

Why use short-circuit code?

Related Questions: Benefits of using short-circuit evaluation, Why would a language NOT use Short-circuit evaluation?, Can someone explain this line of code please? (Logic & Assignment operators)
There are questions about the benefits of a language using short-circuit code, but I'm wondering what are the benefits for a programmer? Is it just that it can make code a little more concise? Or are there performance reasons?
I'm not asking about situations where two entities need to be evaluated anyway, for example:
if($user->auth() AND $model->valid()){
$model->save();
}
To me the reasoning there is clear - since both need to be true, you can skip the more costly model validation if the user can't save the data.
This also has a (to me) obvious purpose:
if(is_string($userid) AND strlen($userid) > 10){
//do something
};
Because it wouldn't be wise to call strlen() with a non-string value.
What I'm wondering about is the use of short-circuit code when it doesn't effect any other statements. For example, from the Zend Application default index page:
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
This could have been:
if(!defined('APPLICATION_PATH')){
define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
}
Or even as a single statement:
if(!defined('APPLICATION_PATH'))
define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
So why use the short-circuit code? Just for the 'coolness' factor of using logic operators in place of control structures? To consolidate nested if statements? Because it's faster?
For programmers, the benefit of a less verbose syntax over another more verbose syntax can be:
less to type, therefore higher coding efficiency
less to read, therefore better maintainability.
Now I'm only talking about when the less verbose syntax is not tricky or clever in any way, just the same recognized way of doing, but in fewer characters.
It's often when you see specific constructs in one language that you wish the language you use could have, but didn't even necessarily realize it before. Some examples off the top of my head:
anonymous inner classes in Java instead of passing a pointer to a function (way more lines of code).
in Ruby, the ||= operator, to evaluate an expression and assign to it if it evaluates to false or is null. Sure, you can achieve the same thing by 3 lines of code, but why?
and many more...
Use it to confuse people!
I don't know PHP and I've never seen short-circuiting used outside an if or while condition in the C family of languages, but in Perl it's very idiomatic to say:
open my $filehandle, '<', 'filename' or die "Couldn't open file: $!";
One advantage of having it all in one statement is the variable declaration. Otherwise you'd have to say:
my $filehandle;
unless (open $filehandle, '<', 'filename') {
die "Couldn't open file: $!";
}
Hard to claim the second one is cleaner in that case. And it'd be wordier still in a language that doesn't have unless
I think your example is for the coolness factor. There's no reason to write code like that.
EDIT: I have no problem with doing it for idiomatic reasons. If everyone else who uses a language uses short-circuit evaluation to make statement-like entities that everyone understands, then you should too. However, my experience is that code of that sort is rarely written in C-family languages; proper form is just to use the "if" statement as normal, which separates the conditional (which presumably has no side effects) from the function call that the conditional controls (which presumably has many side effects).
Short circuit operators can be useful in two important circumstances which haven't yet been mentioned:
Case 1. Suppose you had a pointer which may or may not be NULL and you wanted to check that it wasn't NULL, and that the thing it pointed to wasn't 0. However, you must not dereference the pointer if it's NULL. Without short-circuit operators, you would have to do this:
if (a != NULL) {
if (*a != 0) {
⋮
}
}
However, short-circuit operators allow you to write this more compactly:
if (a != NULL && *a != 0) {
⋮
}
in the certain knowledge that *a will not be evaluated if a is NULL.
Case 2. If you want to set a variable to a non-false value returned from one of a series of functions, you can simply do:
my $file = $user_filename ||
find_file_in_user_path() ||
find_file_in_system_path() ||
$default_filename;
This sets the value of $file to $user_filename if it's present, or the result of find_file_in_user_path(), if it's true, or … so on. This is seen perhaps more often in Perl than C, but I have seen it in C.
There are other uses, including the rather contrived examples which you cite above. But they are a useful tool, and one which I have missed when programming in less complex languages.
Related to what Dan said, I'd think it all depends on the conventions of each programming language. I can't see any difference, so do whatever is idiomatic in each programming language. One thing that could make a difference that comes to mind is if you had to do a series of checks, in that case the short-circuiting style would be much clearer than the alternative if style.
What if you had a expensive to call (performance wise) function that returned a boolean on the right hand side that you only wanted called if another condition was true (or false)? In this case Short circuiting saves you many CPU cycles. It does make the code more concise because of fewer nested if statements. So, for all the reasons you listed at the end of your question.
The truth is actually performance. Short circuiting is used in compilers to eliminate dead code saving on file size and execution speed. At run-time short-circuiting does not execute the remaining clause in the logical expression if their outcome does not affect the answer, speeding up the evaluation of the formula. I am struggling to remember an example. e.g
a AND b AND c
There are two terms in this formula evaluated left to right.
if a AND b evaluates to FALSE then the next expression AND c can either be FALSE AND TRUE or FALSE AND FALSE. Both evaluate to FALSE no matter what the value of c is. Therefore the compiler does not include AND c in the compiled format hence short-circuiting the code.
To answer the question there are special cases when the compiler cannot determine whether the logical expression has a constant output and hence would not short-circuit the code.
Think of it this way, if you have a statement like
if( A AND B )
chances are if A returns FALSE you'll only ever want to evaluate B in rare special cases. For this reason NOT using short ciruit evaluation is confusing.
Short circuit evaluation also makes your code more readable by preventing another bracketed indentation and brackets have a tendency to add up.

Resources