0
$\begingroup$
  • for $e\leftarrow1$ to 8 do
  • for $e\in[1,8]$ do
  • for $e\in\{x\in\mathbb{N}|1\le x\le8\}$ do

What is more preferred?

$\endgroup$
0
$\begingroup$

Just use whatever is clearest in your particular situation. Pseudocode is not a formal language with formal syntax.

Note that for $e\in [1,8]$ do doesn't necessarily imply that the values $1$, $2$, ..., $8$ are assigned in any particular order. Note that $\{x\in \mathbb{N}\mid 1\leq x\leq 8\}$ is somewhat redundant: an algorithm can't possibly loop over all the reals or rationals between $1$ and $8$, so the set can only really be a set of natural numbers. Finally, note that the association $\mathrm{e}=2.71828\ldots$ is very strong, so $e$ is often a poor choice of variable name.

$\endgroup$
  • $\begingroup$ Thus, the most prefered way for looping from 1 to 8 is the first choice after replacing e with another variable. Right? $\endgroup$ – Abdulkader Jun 6 '18 at 14:07
  • $\begingroup$ Of the three options you give, I think that's the one. But something like "for $i = 1$ to $8$ do" would also work: the most important thing is to be clear. $\endgroup$ – David Richerby Jun 6 '18 at 14:17
0
$\begingroup$

Pseudocode does not need to be so mathematical just for the case of being fancy. Making your algorithms understandable is always priority number one.

S = Dynamic array with integers
P = New empty AVL Tree

for every element x in S
    P.insert(x)

Something like this is very easy to follow and does not include any form of formal descriptions. If the formal descriptions are important to understand, then we could of course show them just to clearify. The most preferable for the one receiving your pseudocode is that its simple and easy to follow.

$\endgroup$
0
$\begingroup$

The first one is much more compact and widely used specially in books like Introduction to Algorithms ,pseudocodes are meant to be simple and every reader with a little background of knowledge would be able to understand it

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.