A Conversation for Conditional Statements and Loops in Programming

Peer Review: A18344702 - Conditional Statements and Loops in Programming

Post 1

AlexAshman

Entry: Conditional Statements and Loops in Programming - A18344702
Author: Alex 'Tufty' Ashman [!] - U566116


Considering the variety of programming topics already covered, I'm surprised no one has bothered to cover this. smiley - erm

Hope I've got it all right.

Alex smiley - smiley


A18344702 - Conditional Statements and Loops in Programming

Post 2

Leo


Ehem. This little student is still towing around a "Starting Out With C++" book. I could follow, but shouldn't you explain every notation before you use it? Like n++ isn't explained nor %.


A18344702 - Conditional Statements and Loops in Programming

Post 3

AlexAshman


I think I've got everything properly explained now smiley - cheers

I find it more useful when the basics are explained before the example and the details appear after - that way you actually have to think a little, which helps.


A18344702 - Conditional Statements and Loops in Programming

Post 4

Icy North

A fine entry, Alex.

I think there are a few alternative syntaxes you haven't covered, but these should be obvious after reading this article. I may go and ferret out some of my old notes in any case.

Could you just confirm whether your for-loop example is correct:

y = 0
for n = 0; n < 5; n++
&#8194;&#8195;y = y + n
end

It depends how the condition is tested I suppose, but why doesn't this return the fifth triangular number rather than the fourth?

smiley - cheers Icy


A18344702 - Conditional Statements and Loops in Programming

Post 5

Icy North

smiley - yikes Ignore those unprintable characters - they were tabs. It should read

y = 0
for n = 0; n < 5; n++
y = y + n
end


A18344702 - Conditional Statements and Loops in Programming

Post 6

Gnomon - time to move on

The triangular numbers are 1, 3, 6, 10 etc.

The loop calculates 0, 1, 3, 6, 10 etc. So on five iterations it calculates the fourth triangular number. It would make more sense to make the loop start with n=1, wouldn't it?


A18344702 - Conditional Statements and Loops in Programming

Post 7

AlexAshman


Yes, I did consider whether it should start with n=0 or n=1, but since most textbooks use examples of Sigma using things like n=0 to n=+infinity or n=0 to n=4, I thought I ought to include zero. I'll go and change it smiley - oksmiley - run


A18344702 - Conditional Statements and Loops in Programming

Post 8

AlexAshman


Sorted, plus I think the syntax is ok - I based that bit of the 'generic language' on C. smiley - ok


A18344702 - Conditional Statements and Loops in Programming

Post 9

sigsfried

Well it is recognizable but would not BASIC syntax make more sense as an initial syntax. After all it is what most people who aren't going to do serious programming learn. C is seen for whatever reason as more advanced.


A18344702 - Conditional Statements and Loops in Programming

Post 10

Gnomon - time to move on

BASIC doesn't contain most of the standard conditional and loop structures. There have been many different additions to Basic which do include them, but they are non-standard. Every version of "Super BASIC" has its own syntax.


A18344702 - Conditional Statements and Loops in Programming

Post 11

sigsfried

OK but they are all very similar. They also share much of it with Fortran and the commands are more intuitive to someone without any IT knowledge.


A18344702 - Conditional Statements and Loops in Programming

Post 12

AlexAshman


I wasn't really planning on making the generic language I've used close to any particular language - I just wanted to get the concepts across more clearly through the use of examples.


A18344702 - Conditional Statements and Loops in Programming

Post 13

Gnomon - time to move on

If you're not tied to C syntax, then you could make it more explanatory by using AND instead of &&.


A18344702 - Conditional Statements and Loops in Programming

Post 14

AlexAshman


Ok, I've changed that bit and reworded the explanation that follows. smiley - oksmiley - cheers


A18344702 - Conditional Statements and Loops in Programming

Post 15

Gnomon - time to move on

Two points:

"Note that if the first comparison made is enough to decide whether the whole statement is true or false4, the computer will ignore the other comparisons and just get on with it." -- this is not strictly true, as it is implementation dependent. In some compilers, it may evaluate the right-hand-most expression first. You could change it to "may, depending on the particular language, choose to ignore".

"number = number + 1"
"the '=' in the third line simply tells the computer to make the variable on the left equal to the expression on the right hand side."

I know that this particular syntax can be very confusing for beginners, who ask "How can something be equal to itself plus one?"

Perhaps you could phrase your explanation better, along the lines of:

"tells the computer to calculate the value of the expression on the right hand side and to store the result in the variable on the left".

smiley - smiley G


A18344702 - Conditional Statements and Loops in Programming

Post 16

AlexAshman


Both good points - sorted smiley - cheers


A18344702 - Conditional Statements and Loops in Programming

Post 17

Icy North

Good points, Gnomon.

On the second point, the operation is assignment, and some languages use ":=" rather than "=" to denote this (if my memory serves - it's been a while since I was a code monkey)


A18344702 - Conditional Statements and Loops in Programming

Post 18

Gnomon - time to move on

Pascal uses := but Basic, Fortran and C all use a plain = sign.


A18344702 - Conditional Statements and Loops in Programming

Post 19

AlexAshman

I've added a footnote mentioning ':='.


A18344702 - Conditional Statements and Loops in Programming

Post 20

Icy North

Thanks Alex,

One more thing:

I know you say that But as the title's generic, you might explain somewhere what the differences were in conditional/loop programming in the more basic languages. It makes an interesting comparison, for one thing.

You mention the GOTO statement at one point, and you could say that this transferred control to an explicit address (or line) of the program. A simple example would be good.

Regressing further, there's also an explicit jump command in X86 machine language: JMP , as well as a number of conditional jumps: JNE (jump if not equal), etc.

smiley - cheers Icy


Key: Complain about this post