Conditional And Iterative Data Types

.. l, while/do, and for. The following examples display the usage of the key words. repeat case Ch of i := 1; ‘a’ . ‘z’: writeln(‘small’); x := round(r) + 12; ‘A’ .

‘Z’: writeln(‘big’); while i < x do begin end; write(' '); ----------------------------------------- inc(i); if Ch = 'Y' then begin end; goodmood := true; writeln('*'); end else begin y := y+1; goodmood := false; until keypressed; end; ---------------------------------------- for i := 0 to 9 do read(f, numbers[i]); readln(F); end; The repeat block is a perfect example of the iterative control structure and it contains the while/do block. The repeat block executes all the statements at least once then test to see if a key has been pressed; this is the posttest operation of the iterative control structure. The while/do in the middle of the repeat block shows the pretest of the iterative control structures. It tests to see if i is less than x before performing the statements that follow. Fixed count iteration is pointed out by the for loop.

We Will Write a Custom Essay Specifically
For You For Only $13.90/page!

order now

The statements under the for loop are executed ten times. Non-deterministic conditional structure is found with the case block. The variable ‘Ch’ is examined against lowercase letters then against uppercase letters. The simple conditional and two-alternative conditional is cover in the one if/else block of code. If ‘Ch’ equals ‘Y’ then ‘goodmood’ gets the value of true. With the else statement on the end, the simple conditional becomes the two-alternative conditional. ‘Goodmood’ gets the value of false when ‘Ch’ does not equal ‘Y’.

Pascal structures are very similar to other languages but keywords and syntax are a little different. Qbasic is a simple language, but it still contains a wonderful assortment of conditional and iterative structures. As for conditional structures, if, else/if, and case are the most popular choices. When it comes to iterative structures, Qbasic is packed full of them. Qbasic has for, do/while, while, do/until, and do/loop.

All of the previous control structures are the basic type found in C++, Java, and Pascal. Again, Qbasic is similar to the previous languages, but the key words are different. Qbasic contains and relies upon the heavy use of conditional and iterative control structures such as the if and do structures. To further illustrate the Qbasic control structures, the following examples are referenced and explained. if (yrs > 20) then for ctr = 100 to 200 print “give a gold watch” total = total + ctr elseif ((yrs > 10) and (yrs < 20) then next ctr print "give a paperweight" ------------------------------- end if while (A > 5) ——————————————- print A, B select case num B = B – 1 case 1 wend beep ——————————- case 2 do while (ans$ <> “Y”) beep : beep print”you must type Y” case 3 input “hello”; ans$ beep : beep : beep loop end select ——————————– ——————————————- do until (ans$ = “Y”) do beep print “type Y” print “type Y” input “answer”; ans$ input “answer”; ans$ loop while (ans$ <> “Y”) loop There are many examples above, but most of the examples are of iterative control structures.

The simple conditional and two-alternative conditional are shown with the if and else/if structures. If yrs is greater than 20 then print the message. The else if added to the end checks to see if the years are in-between 10 and 20. If the years meet the criteria the next message is printed. The case block demonstrates non-deterministic conditional structures just like C++, Java, and Pascal.

Based on the variable ‘num’ one to three beeps are heard. The do/while loop is one of many iterative control structures. The do/while is a posttest operation that checks the users input after the loop runs once. The for loop indicates a fixed count structure; it loops while the variable ‘ctr’ is incremented. The while loop is a pretest iterative structure testing ‘A’ before the loop even begins.

The do/while loop is also a pretest iterative structure that checks the users input before the loop starts. The do/until block is like a in-test iterative structure. It tests each line as it is processed to see if ‘ans$’ is equal to ‘Y’. When ‘ans$’ is equal to ‘Y’, the loop releases control back to the main program. Qbasic can be compared to the previous three languages, but Qbasic seems to be less streamlined because it has so many possible iterative structures.

The same programs can be written in Java using only for or while loops. Now for the mighty programming language COBOL. COBOL is the older language with the ability to keep up with today’s fast paced programming tasks. COBOL is not very comparable to the previous four languages. Its is capable of performing the same tasks with its conditional and iterative structures as C++, Java or any of the other languages, but COBOL has its own special way of performing.

The syntax is quite different, but some of the key words are the same. Also, COBOL has considerably less iterative structures compared to Qbasic. COBOL has such conditional structures as if, if/else and evaluate/when. Iterative structures are best described in one word “perform”; the perform/until is COBOL’s definite solution to iteration. Exemplified by the following code , COBOL’s conditional and iterative structures are easily understood. if operation = ‘-‘ subtract field-1 from field-2 giving result ————————————————– —————————– if operation = ‘+’ add field-1 field-2 giving result else if operation = ‘/’ divide field-1 by field-2 giving result ————————————————– —————————– evaluate taxable-wages when zero thru 200 multiply taxable-wages by .15 giving federal-taxes when 200 thru 300 multiply taxable-wages by .22 giving federal-taxes end-evaluate. ————————————————– —————————— perform varying power-value from 1 by 1 until power-value > field-2 end-perform The example above lengthy for demonstrating only four structures by COBOL is not designed to be easy to write; it is designed to crunch numbers.

The if statement shows a simple conditional. If the question is evaluated to be true the subtraction is performed on the two fields. The second block of code shows the use of if/else or the two-alternative conditional. If the first condition is not met the second condition is evaluated. If the second is found true division is performed on the two fields and an answer is given.

Evaluate is the last conditional block of code; this is a non-deterministic conditional structure. Each case is evaluated line by line, and “when” (just like in the code) a condition is met the applicable statements are executed such as finding an employees federal taxes for a company. The iterative structures posttest, in-test, and fixed count can be performed using the”perform” keyword in COBOL. The example above displays the posttest. The loop is performed then ‘power-value’ is tested against ‘field-2’; if power-value is greater than field-2 then the loop is broken. COBOL is a competitive language, but it is lengthy when writing. Then conditional and iterative structures are easy to use but require much time during the planning phase of writing a program. Scheme is the last and most unusual programming language of this paper.

Scheme can implement both conditional and iterative structures, but the syntax of scheme is totally different from C++, Java, Pascal, Qbasic, or COBOL. The Scheme programming language is designed in a recursive manner. All Scheme programs are recursive and are actually iterative because the program iterates over and over again. The recursive nature of Scheme is not however an iterative structure. The conditional structures of Scheme us such keywords as if, else, case, and cond.

The iterative structure of Scheme seems to only be seen with the keywords loop or do. Scheme conditional and iterative structures are seen below. (if (< n 0) (case (+ x y) (- 1 n)) ((1 3 5 7 9) 'odd) ((0 2 4 6 8) 'even) --------------------------------- ------------------------------------------- (cond (loop (eqv? msg 'empty?) (if (= n 0) (break ls)) (eqv? msg 'push!) (set! ls (cons 'a ls)) (else "oops")) (set! n (- n 1))) ---------------------------------- -------------------------------------------- (cond (do (( i 2 (+ i 1))) (< x 0) ((>= i n)) (> x 0) The syntax of the if block looks very different from any other languages in this paper, but it behaves in a similar fashion to other simple conditional structures. The if statement states that if ‘n’ is less than zero then subtract one from ‘n’. The cond with the else is a multi-alternative conditional structure.

The first line asks if the variable ‘msg’ contains the string ’empty?’. The second line asks if the variable ‘msg’ contains the string ‘push!’, and the else line says if neither of the previous two lines are true print’oops’. The cond block on the bottom is a two-alternative conditional structure. The first condition is if x is less than zero, and the second condition is if x is greater than zero. The case block is another multi-alternative conditional structure. If x plus y is equal to and odd number then ‘odd’ is printed, and if x plus y is equal to an even number then ‘even’ is printed. The loop code is a pretest iterative structure. ‘n’ is tested against zero for equality. If they are equal then the loop is stopped.

The do block corresponds to the posttest iterative structure. The first line is executed until ‘i’ is greater than or equal to ‘n’, but the condition is not tested until the first line is executed at least once. Scheme is an odd language, but once it is mastered is can accomplish any programming task set forth. Comparatively, the conditional and iterative structures in all the languages previously mentioned are very similar to one another. All of the simple conditional structures even use the same key word ‘if’. The conditionals in the languages all accomplish the same tasks only some languages use different methods.

The iterative structures allow similar tasks to be completed by all languages as well. For the most part the iterative structures of all the languages use at least the keyword ‘do’ or perhaps ‘while’. The key words may vary but the function is all the same. The structures are used for iteration. The blocks of statements in the iterative loop must be performed time and time again. C++ and Java are the most similar languages when the conditional and iterative structures are compared.

The two languages that are the most dissimilar are COBOL and Scheme. COBOL is lengthy with a few control structures. Scheme is compact with many control structures. When comparing the control structures of C++, Java, Pascal, Qbasic, COBOL, and Scheme, no one language can be said to be better than any of the others.