3 ms·
Here's some simple COBOL code: http://www.csis.ul.ie/cobol/examples/Conditn/Conditions.htm http://www.csis.ul.ie/cobol/examples/Conditn/Conditions.htm ide
by msla 7y ago
Here's some simple COBOL code:
http://www.csis.ul.ie/cobol/examples/Conditn/Conditions.htm http://www.csis.ul.ie/cobol/examples/Conditn/Conditions.htm
identification division.
program-id. letters.
data division.
working-storage section.
01 Char PIC X.
88 Vowel VALUE "a", "e", "i", "o", "u".
88 Consonant VALUE "b", "c", "d", "f", "g", "h"
"j" THRU "n", "p" THRU "t", "v" THRU "z".
88 Digit VALUE "0" THRU "9".
88 ValidCharacter VALUE "a" THRU "z", "0" THRU "9".
procedure division.
begin.
display "Enter lower-case character or digit. No data ends.".
accept Char.
perform until not ValidCharacter
evaluate true
when Vowel display "The letter " Char " is a vowel."
when Consonant display "The letter " Char " is a consonant."
when Digit display Char " is a digit."
when other display "problems found"
end-evaluate
accept Char
end-perform
stop run.
(I removed some chaff which GnuCOBOL doesn't need and fixed an apparent bug.)
So... where is it actually testing what kind of character you input? Where is the code for that? You input a specification for what a Vowel is, for example, and you write explicit code for what to do when a Vowel is input, but where is the code which goes through the specification and decides, yep, that's a Vowel?
COBOL is kind of an odd language. It's verbose in some respects and quite concise in others. Rewriting COBOL into something else would take actual human effort if you wanted the "something else" to look like code a human wrote, as opposed to the intermediate pass of an optimizing compiler, which is what the C GnuCOBOL can output looks like. Re-writing the COBOL might be the best move in some cases, or replacing it with entirely new code, but it isn't something you'd be able to do "for free" in any sense, especially with regards to time.
- metreo 7y agoIt's actually still better on my eyes than JavaScript.
- number6 7y agoNever saw cobol in my life before, guess its "evaluate true when" part. not very explicit but readable. maybe it's just a well written example but something to work with
- YeGoblynQueenne 7y ago"when Vowel" is not very different to bog standard switch-statement magic. Actually in this case it looks like Cobol is almost doing a kind of pattern matching. Which is cool. Although (my Cobol is a bit rusty) I think there might be a more elaborate way to write "when Vowel" that hides less of the actual process? Not sure. Cobol is really not such a bad language. It's just got a lot of ...ceremony. All those forced divisions and sections. But that's a feature: in the olden days, structured programming was a big thing. And an experienced Cobol programmer can take a quick look at a big Cobol file and find where everything is in a blink.
- ptx 7y agoJava similarly imposes a lot of structure that allows you to more easily find where everything is, but the trade-off is that there is a lot more of this everything to find, whereas in a more flexible language you would have less code to read through and therefore less need for organizing it.
- msla 7y ago> "when Vowel" is not very different to bog standard switch-statement magic. Actually in this case it looks like Cobol is almost doing a kind of pattern matching. It is, but the "magic" is that the pattern-matching is part of the variable declaration, so you can reuse those patterns wherever you can use the variable.
- ptx 7y agoThat looks a lot like Visual Basic, except it allows you to name the patterns. sub letters dim char as string do char = inputbox("Enter lower-case character or digit. No data ends.") select case char case "a", "e", "i", "o", "u" msgbox "The letter " & char & " is a vowel." case "b", "c", "d", "f", "g", "h", _ "j" to "n", "p" to "t", "v" to "z" msgbox "The letter " & char & " is a consonant." case "0" to "9" msgbox char & " is a digit." case else exit do end select loop end sub (This example is actually LibreOffice Basic, as I'm not on a Windows machine right now, but it should be the same in VBA and VB6.)
- msla 7y ago> That looks a lot like Visual Basic, except it allows you to name the patterns. More to the point, it allows you to keep the patterns near the variable declaration, so you can reuse them. The pattern matching is part of how the data is declared.
- ptx 7y agoCan you also reuse them for different variables, if you want to store two of these chars? This program appears to have the patterns declared as part of the variable declaration.
- prashnts 7y agoThanks for posting this. Our computer teacher in high school skipped the outdated COBOL sections (just an intro, around 2004) I realise i'd never seen an actual code sample yet! Aside, RE testing, a wild guess: could the "accept char" be tested with emulated keyboard inputs?
- deleted 7y ago[deleted]