A Conversation for Conditional Statements and Loops in Programming
- 1
- 2
Peer Review: A18344702 - Conditional Statements and Loops in Programming
AlexAshman Started conversation Dec 21, 2006
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.
Hope I've got it all right.
Alex
A18344702 - Conditional Statements and Loops in Programming
Leo Posted Dec 22, 2006
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
AlexAshman Posted Dec 22, 2006
I think I've got everything properly explained now
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
Icy North Posted Jan 3, 2007
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++
  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?
Icy
A18344702 - Conditional Statements and Loops in Programming
Icy North Posted Jan 3, 2007
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
Gnomon - time to move on Posted Jan 3, 2007
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
AlexAshman Posted Jan 3, 2007
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
A18344702 - Conditional Statements and Loops in Programming
sigsfried Posted Jan 5, 2007
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
Gnomon - time to move on Posted Jan 6, 2007
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
sigsfried Posted Jan 7, 2007
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
AlexAshman Posted Jan 8, 2007
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
Gnomon - time to move on Posted Jan 8, 2007
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
Gnomon - time to move on Posted Jan 8, 2007
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".
G
A18344702 - Conditional Statements and Loops in Programming
Icy North Posted Jan 8, 2007
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
Gnomon - time to move on Posted Jan 8, 2007
Pascal uses := but Basic, Fortran and C all use a plain = sign.
A18344702 - Conditional Statements and Loops in Programming
AlexAshman Posted Jan 8, 2007
I've added a footnote mentioning ':='.
A18344702 - Conditional Statements and Loops in Programming
Icy North Posted Jan 8, 2007
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.
Icy
Key: Complain about this post
- 1
- 2
Peer Review: A18344702 - Conditional Statements and Loops in Programming
- 1: AlexAshman (Dec 21, 2006)
- 2: Leo (Dec 22, 2006)
- 3: AlexAshman (Dec 22, 2006)
- 4: Icy North (Jan 3, 2007)
- 5: Icy North (Jan 3, 2007)
- 6: Gnomon - time to move on (Jan 3, 2007)
- 7: AlexAshman (Jan 3, 2007)
- 8: AlexAshman (Jan 3, 2007)
- 9: sigsfried (Jan 5, 2007)
- 10: Gnomon - time to move on (Jan 6, 2007)
- 11: sigsfried (Jan 7, 2007)
- 12: AlexAshman (Jan 8, 2007)
- 13: Gnomon - time to move on (Jan 8, 2007)
- 14: AlexAshman (Jan 8, 2007)
- 15: Gnomon - time to move on (Jan 8, 2007)
- 16: AlexAshman (Jan 8, 2007)
- 17: Icy North (Jan 8, 2007)
- 18: Gnomon - time to move on (Jan 8, 2007)
- 19: AlexAshman (Jan 8, 2007)
- 20: Icy North (Jan 8, 2007)
More Conversations for Conditional Statements and Loops in Programming
- A88060494 - 'Northanger Abbey' - a Novel by Jane Austen [2]
6 Days Ago - A88057290 - FV4005 [3]
3 Weeks Ago - A88040063 - Neolassicistic Art - Mass Market and Industrialisation [6]
5 Weeks Ago - A88048849 - Gulls - a Beginner's Guide to Identification [5]
Oct 31, 2024 - A88057191 - 'Cabin Pressure' - the Radio Comedy [11]
Oct 24, 2024
Write an Entry
"The Hitchhiker's Guide to the Galaxy is a wholly remarkable book. It has been compiled and recompiled many times and under many different editorships. It contains contributions from countless numbers of travellers and researchers."