if statements (1 Viewer)

Zadss

New Member
Joined
Oct 7, 2014
Messages
5
Gender
Undisclosed
HSC
N/A
i was wondering can if statements be written like this (in pseudo-code) :

IF condition is met
do this
IF condition is met
do this
IF condition is met
do this
.
.
.

or do they have to be written like this:

IF condition is met
do this​
ELSE
IF condition is met​
Do this​
ELSE​
IF condition is met​
Do this​

Thank you!
 

duhdevitt

Active Member
Joined
Oct 6, 2014
Messages
191
Gender
Male
HSC
2015
I'm pretty sure its your second one. I think

IF condition is True or met
do this
ELSE
do this
 

seventhroot

gg no re
Joined
Aug 3, 2014
Messages
2,809
Gender
Male
HSC
2013
something like this would be better

...if (true) {
......execute something
...} else if (true) {
......execute something
...} else {
......execute something
...}


//make sure you indent
 

Zadss

New Member
Joined
Oct 7, 2014
Messages
5
Gender
Undisclosed
HSC
N/A
can CASEWHERE be used instead of multiple IF statements?
 

brent012

Webmaster
Webmaster
Joined
Feb 26, 2008
Messages
5,284
Gender
Male
HSC
2011
Multiple ifs are different to else ifs, having multiple ifs (not else/else if) suggests more than one can be true or that the condition could be changed in one of the code blocks.

You can also use switches/case where instead of the else if way. Not really a concern in pseudocode, but when writing code in practice you need to be careful as the typical switch implementation is flawed and will break through if you don't return/break.

Depending on how you use it, if the compiler can't optimise it to use something like a jump table the switch will likely just end up exactly the same as a bunch of if/else ifs anyway.
 

nathan98

New Member
Joined
Mar 31, 2015
Messages
14
Gender
Male
HSC
2016
A CASEWHERE statement is more of a better approach than multiple IF statements. With a CASEWHERE statement your code appears to be cleaner and concise.
 

Eluate

Member
Joined
May 13, 2015
Messages
55
Gender
Male
HSC
2017
In the Software Design and Development Course Specifications, under Psuedocode guidelines, it says that
for binary selection
IF condition THEN
statements
ELSE
statements
ENDIF
and

for multi-way selection
CASEWHERE expression evaluates to
A: process A
B: process B
………………..
OTHERWISE: process …
ENDCASE
If you want to do "nested" if statements, you can do:
Code:
IF condition THEN
    IF condition THEN
        // Do something
    ELSE
          IF condition THEN
              // Do something
          ELSE
             // Do something
          ENDIF
    ENDIF
ENDIF
You should be putting ENDCASE and ENDIF according to the course specifications.
 

Eluate

Member
Joined
May 13, 2015
Messages
55
Gender
Male
HSC
2017
A CASEWHERE statement is more of a better approach than multiple IF statements. With a CASEWHERE statement your code appears to be cleaner and concise.
There are some instances where this is not true, since you can only evaluate one expression in a casewhere against other possible values where in multiple if statements, you can use multiple expressions.

If you are using a casewhere, it should normally be used for situations where an expression or value can be more than 2 different values and more than 2 values do something different for each value.
 

Eluate

Member
Joined
May 13, 2015
Messages
55
Gender
Male
HSC
2017
IF condition is met
do this
IF condition is met
do this
IF condition is met
do this
.
.
.

IF condition is met
do this​
ELSE
IF condition is met​
Do this​
ELSE​
IF condition is met​
Do this​
Just remember the logic in the first example is different to the second example. For every if statement that should be in a "nested" else, you need to
evaluate the expression to include the first if expression with the keyword AND.
 

brent012

Webmaster
Webmaster
Joined
Feb 26, 2008
Messages
5,284
Gender
Male
HSC
2011
Pseudo code technically has no functionality haha.

I think this has been discussed here before (it has, see my comment on page 1), they mean different things. If there is no else both conditions will be evaluated and both could be true -- possibly by side effect of something in an earlier if block. If you intend on short circuiting the other checks then you should use else ifs.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top