Matchmaker. com: Sign up now for a free trial. Date Smarter!ConditionalAnd Iterative Data TypesA programming language cannot bea programming language with out its conditional and iterative structures.
Programming languages are built to accomplish the task of controlling computerinput and output. A programmer must use every tool available to completehis/her given tasks, and conditional as well as iterative statements arethe most basic items of programming which must be mastered. Manydifferent programming languages can demonstrate conditional and iterativestatements including C++, Java, Pascal, Qbasic, COBOL, and Scheme. Most of these languages implement conditional and iterative statementsin a similar fashion; however, there are a few differences. The conditional structure is easyto understand and self-defining. The whole statement is base on acondition and its veracity.
When the statement or “test” is foundto be true, a statement is executed, and if it is false, another test isgiven or the program continues to the next block. Conditional structuresinclude the simple, two-alternative, multi-alternative, and non-deterministicconditional. The simple conditional is the easiest to understandbeing the IF-THEN statement. if <Boolean expression> then<block of statements>IF a condition is met THEN execute a statement. The two-alternative conditional or IF-ELSE is also easy to understand. if <Boolean expression> then<block of statements>else<block of statements>IF a condition is met execute a statement;ELSE the condition was not met so execute a different statement.
The multi-alternative conditional is very close to the two-alternativeconditional. if <condition-1> then<statement-block-1>elseif <condition-2> then<statement-block-2>. . . elseif <condition-n> then<statement-block-n>[else <statement-block-(n+1)>]end ifThe IF question is asked about a statement,and if it is not true, the next statement is examined.
If statementnumber two is not true, the next statement is examined; then the next statementis examined and so forth until a condition is met, and the control is carriedout of the multi-alternative conditional. The non-deterministic conditionalis similar to the multi-alternative conditional, because it has multipleconditionals. if <condition-1> <statement-sequence-1>when <condition-2> <statement-sequence-2>. . .
when <condition-n> <statement-sequence-n>end ifThe reason multi-alternative and non-deterministicconditionals are different, stems from the release of program’s controlor flow. The non-deterministic conditional tests each statement tosee if its condition is met; whereas the multi-alternative conditionalonly tests statements until one of the conditions is met. After oneof the conditions is met, the multi-alternative conditional releases theprogram control and fails to check anymore statements. The iterative structure is a bitmore complicated than the conditional structure, but it is easy to understand. An iterative structure consists of a block of statements that are executedrepetitively.
They might be executed once or a hundred times dependingon the controls placed on the structure. Iterative structures includenon-terminating, pretest, posttest, in-test, fixed-count, and non-deterministiciteration. The non-terminating iterative structure is not very common,because once it has program control, it does not release its control untilthe whole program is terminated. loop;sequence-of-statements;end loopOnce a non-terminating iterative structureis activated it continues to run forever.
The pretest iterative structuretests a condition first, then if it is true, the following statements areexecuted repeatedly – testing the statement each loop – until thestatement is false. while ;condition; loop;sequence-of-statements;end loopOnce the statement is found to be false,control is returned to the program ending the pretest iterative statement. The posttest iterative statement is the same as the pretest statement exceptthe condition is tested after the sequence of statements. With theposttest iterative statement, the programmer is guaranteed to have thesequence of statements executed at least once. The statements areexecuted before the conditional statement has a chance to be tested. With the in-test iterative statement, the conditional statement is testedin the middle of the sequence of statements.
It can be useful whenit is necessary to run a portion of the statement at least once regardlessof the outcome of the conditional. Fixed-count iteration is verypopular, because it allows a sequence of statements to be executed a finitenumber of times instead of relying on a condition to be true or false. The fixed-count iteration is simply do ;sequence-of-statements; x numberof times. The non-deterministic iterative statement is similar tothe non-deterministic conditional statement. dowhen ;condition-1; ;#61614;;statement-sequence-1;when ;condition-1; ;#61614;;statement-sequence-1;. .
. when ;condition-n; ;#61614;;statement-sequence-n;end doThe iterative statement executes untilnone of the conditions are found to be true. At that time, controlis release back to the program from the non-deterministic iterative statement. C++ has every type of control structurea programmer could ever need, and they are all easy to implement. The three types of condition statements include the if, if/else, and theswitch.
The simple conditional is implemented through the if statement. The two-alternative conditional can be shown in C++ by using the if/elsestatements, and the multi-alternative can be demonstrated by using theswitch statement. When it come to iterative structures C++ uses thewhile, do while, and for statements. Pretest works closely with thewhile statement. Posttest uses C++’s do while statement, and thefor statement is used in fixed count iteration. The code belowgives an example of each structure.
if (x < y)while (x < y) {cout << x;cout << x;}————————————————————if (x < y)————————————————————cout << x;do {elsecout << x;if (y < x)} while (x < y);cout << y;————————————————————————————————————————for (int i=0; i < 10; i++)switch (fruit)i = i + 1;case ‘orange’:cout << “yum”;case ‘banana’:cout << “yummy”;case ‘apple’:cout << “ooh yes”;The simple conditional or “if” statementsimply asks if x is less than y and if it less than y x is printed to thescreen. The two-alternative/multi-alternative conditional using if/elseasks if x is less than y and if true prints x. If it is not lessthan y then it asks if y is less than x. If this is true y is printedto the screen. More statements can be added to this conditional tolengthen the multiple alternatives.
Another multi-alternative conditionalinvolves the case statement. The variable “fruit” is measured against’orange’. If fruit is orange the “yum” is printed. Next “fruit”is checked to see if it is ‘banana’. If so “yummy” is printed. Last ‘apple’ is compared.
Iterative statements start withthe while on the right. The pretest is initiated with the comparisonof x to y. If the statement is true the loop is started and lastsuntil x is greater than y. Post test is exemplified by the do while.
It say print x then check to see if x is less than y. Next continuethe loop until x is greater than y. The for loop continues to addone to i until is not less than ten; the for loop demonstrates fixed countiteration. Java is very similar to C++.
As a matter of fact, Java control structures are almost exactly the sameas C++. When it comes to conditional statements, Java uses the ifand if/else statements. The else if statements can substituted forthe case statement, but case is permitted. Iterative structures areseen throughout Java in the form of while, do while, and for statements. Examples of each statement are in the examples below.
if (distance < r) return true;while(i– > 0) {————————————————————-Object o = get_object();if (a == 2)if (o != null) {System. out. println(“My exception”);do {. . .
else} while(j != 0);if (a < 25)}System. out. println(“Handledat fault pt”); }—————————————————————————————————————-switch ( i ) {for (int i = 0; i < shapes. length; i++){case 0:c = a + 3;throw new MyException(“too low”);}case 1:throw new MySubException(“stilltoo low”);default:return i*i;}After viewing the C++ code on theprevious page, it is easy to see the similarities between C++ and Java. The first simple conditional or if statement compares distance to r returningtrue if the condition is true.
If distance is not less than r nothinghappens. The second if statement is part of a two-alternative conditional. If the condition a == 2 is met statement one is executed. If thecondition is not met then the second alternative is asked and if foundtrue the second statement is executed. The switch statement or non-deterministicconditional is just like the C++ implementation. The variable i iscompared with the first case (0) and if true then the too low exceptionis executed.
If not true the next case is examined and if true thestill too low exception is sent back to the calling block. If noneof the cases match then i * i is returned. The pretest iterativestatement while is seen on the right side. It simply states if iminus one is greater than 0 perform all statements within its loop.
And while i minus one is still greater than 0 keep on looping. Thefor loop or fixed count iterative statement allows all statements belowit to be performed while i is less than the shapes length. The integeri is incremented every time the loop is performed as indicated by the i++located in the actual for statement. Looking at the different aspectsof conditional and iterative structures C++ and Java are almost one inthe same. They both use the same statements to provide examples ofthe different types of structures.
It is easy to see that Java wasbased on the syntax of the C and C++ programming languages. Pascal is a versatile language withcontrol structures that almost match C and Java. The key differencebetween Pascal and C++ is not the actual conditional or iterative structure,but the difference is the syntax and key words. The key words usedby Pascal for conditional structures include if, if/else, and case.
Iterative structures use the key words repeat/until, while/do, and for. The following examples display the usage of the key words. repeat case Chofi := 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 beginend; goodmood:= true;writeln(‘*’); end else beginy := y+1; goodmood:= false;until keypressed; end;—————————————-for i := 0 to 9 doread(f, numbers[i]);readln(F);end;The repeat block is a perfect exampleof the iterative control structure and it contains the while/do block. The repeat block executes all the statements at least once then test tosee if a key has been pressed; this is the posttest operation of the iterativecontrol structure. The while/do in the middle of the repeat blockshows the pretest of the iterative control structures. It tests tosee if i is less than x before performing the statements that follow. Fixed count iteration is pointed out by the for loop.
The statementsunder the for loop are executed ten times. Non-deterministic conditionalstructure is found with the case block. The variable ‘Ch’ is examinedagainst lowercase letters then against uppercase letters. The simpleconditional and two-alternative conditional is cover in the one if/elseblock of code. If ‘Ch’ equals ‘Y’ then ‘goodmood’ gets the valueof true.
With the else statement on the end, the simple conditionalbecomes the two-alternative conditional. ‘Goodmood’ gets the valueof false when ‘Ch’ does not equal ‘Y’. Pascal structures are verysimilar to other languages but keywords and syntax are a little different. Qbasic is a simple language, butit still contains a wonderful assortment of conditional and iterative structures.
As for conditional structures, if, else/if, and case are the most popularchoices. When it comes to iterative structures, Qbasic is packedfull 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 uponthe heavy use of conditional and iterative control structures such as theif and do structures. To further illustrate the Qbasic control structures,the following examples are referenced and explained. if (yrs > 20) then forctr = 100 to 200print “give a gold watch”total = total + ctrelseif ((yrs > 10) and (yrs <20) then next ctrprint “give a paperweight” ——————————-end if while(A > 5)——————————————-print A, Bselect case numB = B – 1case 1 wendbeep ——————————-case 2 do while(ans$ <> “Y”)beep : beep print”you must type Y”case 3input “hello”; ans$beep : beep : beep loopend select —————————————————————————do until (ans$ = “Y”)dobeepprint “type Y”print “type Y”input “answer”; ans$input “answer”; ans$loop while (ans$ <> “Y”)loopThere are many examples above, but mostof the examples are of iterative control structures. The simple conditionaland 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 addedto the end checks to see if the years are in-between 10 and 20. Ifthe years meet the criteria the next message is printed. The caseblock demonstrates non-deterministic conditional structures just like C++,Java, and Pascal. Based on the variable ‘num’ one to three beepsare heard. The do/while loop is one of many iterative control structures. The do/while is a posttest operation that checks the users input afterthe loop runs once.
The for loop indicates a fixed count structure;it loops while the variable ‘ctr’ is incremented. The while loopis a pretest iterative structure testing ‘A’ before the loop even begins. The do/while loop is also a pretest iterative structure that checks theusers input before the loop starts. The do/until block is like ain-test iterative structure. It tests each line as it is processedto see if ‘ans$’ is equal to ‘Y’. When ‘ans$’ is equal to ‘Y’, theloop releases control back to the main program.
Qbasic can be comparedto the previous three languages, but Qbasic seems to be less streamlinedbecause it has so many possible iterative structures. The same programscan be written in Java using only for or while loops. Now for the mighty programming languageCOBOL. COBOL is the older language with the ability to keep up withtoday’s fast paced programming tasks.
COBOL is not very comparableto the previous four languages. Its is capable of performing thesame tasks with its conditional and iterative structures as C++, Java orany 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/untilis COBOL’s definite solution to iteration. Exemplified by the followingcode , COBOL’s conditional and iterative structures are easily understood. if operation = ‘-‘subtract field-1 from field-2 givingresult——————————————————————————-if operation = ‘+’add field-1 field-2 giving resultelseif operation = ‘/’divide field-1 by field-2 givingresult——————————————————————————-evaluate taxable-wageswhen zero thru 200multiply taxable-wages by. 15 giving federal-taxeswhen 200 thru 300multiply taxable-wages by.
22 giving federal-taxesend-evaluate. ——————————————————————————–performvarying power-value from 1 by 1until power-value > field-2end-performThe example above lengthy for demonstratingonly four structures by COBOL is not designed to be easy to write; it isdesigned to crunch numbers. The if statement shows a simple conditional. If the question is evaluated to be true the subtraction is performed onthe two fields.
The second block of code shows the use of if/elseor the two-alternative conditional. If the first condition is notmet the second condition is evaluated. If the second is found truedivision is performed on the two fields and an answer is given. Evaluateis the last conditional block of code; this is a non-deterministic conditionalstructure. Each case is evaluated line by line, and “when” (justlike in the code) a condition is met the applicable statements are executedsuch as finding an employees federal taxes for a company. The iterativestructures 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’; ifpower-value is greater than field-2 then the loop is broken. COBOLis a competitive language, but it is lengthy when writing. Then conditionaland iterative structures are easy to use but require much time during theplanning phase of writing a program.
Scheme is the last and most unusualprogramming language of this paper. Scheme can implement both conditionaland iterative structures, but the syntax of scheme is totally differentfrom C++, Java, Pascal, Qbasic, or COBOL. The Scheme programminglanguage is designed in a recursive manner. All Scheme programs arerecursive and are actually iterative because the program iterates overand over again. The recursive nature of Scheme is not however aniterative structure. The conditional structures of Scheme us suchkeywords as if, else, case, and cond.
The iterative structure ofScheme seems to only be seen with the keywords loop or do. Schemeconditional and iterative structures are seen below. (if (< n 0)(case (+ x y)(- 1 n))((1 3 5 7 9) ‘odd)((02 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 differentfrom any other languages in this paper, but it behaves in a similar fashionto other simple conditional structures. The if statement states thatif ‘n’ is less than zero then subtract one from ‘n’. The cond withthe else is a multi-alternative conditional structure. The firstline asks if the variable ‘msg’ contains the string ’empty?’.
Thesecond line asks if the variable ‘msg’ contains the string ‘push!’, andthe else line says if neither of the previous two lines are true print’oops’. The cond block on the bottom is a two-alternative conditionalstructure. The first condition is if x is less than zero, and thesecond condition is if x is greater than zero. The case block isanother multi-alternative conditional structure. If x plus y is equalto and odd number then ‘odd’ is printed, and if x plus y is equal to aneven number then ‘even’ is printed.
The loop code is a pretest iterativestructure. ‘n’ is tested against zero for equality. If theyare equal then the loop is stopped. The do block corresponds to theposttest iterative structure. The first line is executed until ‘i’is greater than or equal to ‘n’, but the condition is not tested untilthe 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 iterativestructures in all the languages previously mentioned are very similar toone another. All of the simple conditional structures even use thesame key word ‘if’. The conditionals in the languages all accomplishthe same tasks only some languages use different methods. The iterativestructures allow similar tasks to be completed by all languages as well. For the most part the iterative structures of all the languages use atleast the keyword ‘do’ or perhaps ‘while’. The key words may varybut the function is all the same.
The structures are used for iteration. The blocks of statements in the iterative loop must be performed time andtime again. C++ and Java are the most similar languages when theconditional and iterative structures are compared. The two languagesthat are the most dissimilar are COBOL and Scheme. COBOL is lengthywith a few control structures.
Scheme is compact with many controlstructures. When comparing the control structures of C++, Java, Pascal,Qbasic, COBOL, and Scheme, no one language can be said to be better thanany of the others.