Conditional And Iterative Data Types

Conditional And Iterative Data Types Matchmaker.com: Sign up now for a free trial. Date Smarter! Conditional And Iterative Data Types A programming language cannot be a programming language with out its conditional and iterative structures. Programming languages are built to accomplish the task of controlling computer input and output. A programmer must use every tool available to complete his/her given tasks, and conditional as well as iterative statements are the most basic items of programming which must be mastered. Many different programming languages can demonstrate conditional and iterative statements including C++, Java, Pascal, Qbasic, COBOL, and Scheme.

Most of these languages implement conditional and iterative statements in a similar fashion; however, there are a few differences. The conditional structure is easy to understand and self-defining. The whole statement is base on a condition and its veracity. When the statement or “test” is found to be true, a statement is executed, and if it is false, another test is given or the program continues to the next block. Conditional structures include the simple, two-alternative, multi-alternative, and non-deterministic conditional. The simple conditional is the easiest to understand being the IF-THEN statement.

if then IF a condition is met THEN execute a statement. The two-alternative conditional or IF-ELSE is also easy to understand. if then else 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-alternative conditional. if then elseif then .. elseif then [else ] end if The IF question is asked about a statement, and if it is not true, the next statement is examined.

If statement number two is not true, the next statement is examined; then the next statement is examined and so forth until a condition is met, and the control is carried out of the multi-alternative conditional. The non-deterministic conditional is similar to the multi-alternative conditional, because it has multiple conditionals. if when .. when end if The reason multi-alternative and non-deterministic conditionals are different, stems from the release of program’s control or flow. The non-deterministic conditional tests each statement to see if its condition is met; whereas the multi-alternative conditional only tests statements until one of the conditions is met. After one of the conditions is met, the multi-alternative conditional releases the program control and fails to check anymore statements.

The iterative structure is a bit more complicated than the conditional structure, but it is easy to understand. An iterative structure consists of a block of statements that are executed repetitively. They might be executed once or a hundred times depending on the controls placed on the structure. Iterative structures include non-terminating, pretest, posttest, in-test, fixed-count, and non-deterministic iteration. The non-terminating iterative structure is not very common, because once it has program control, it does not release its control until the whole program is terminated.

loop end loop Once a non-terminating iterative structure is activated it continues to run forever. The pretest iterative structure tests a condition first, then if it is true, the following statements are executed repeatedly – testing the statement each loop – until the statement is false. while loop end loop Once 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 except the condition is tested after the sequence of statements. With the posttest iterative statement, the programmer is guaranteed to have the sequence of statements executed at least once. The statements are executed before the conditional statement has a chance to be tested.

With the in-test iterative statement, the conditional statement is tested in the middle of the sequence of statements. It can be useful when it is necessary to run a portion of the statement at least once regardless of the outcome of the conditional. Fixed-count iteration is very popular, because it allows a sequence of statements to be executed a finite number of times instead of relying on a condition to be true or false. The fixed-count iteration is simply do x number of times. The non-deterministic iterative statement is similar to the non-deterministic conditional statement.

do when when .. when end do The iterative statement executes until none of the conditions are found to be true. At that time, control is release back to the program from the non-deterministic iterative statement. C++ has every type of control structure a programmer could ever need, and they are all easy to implement. The three types of condition statements include the if, if/else, and the switch.

The simple conditional is implemented through the if statement. The two-alternative conditional can be shown in C++ by using the if/else statements, and the multi-alternative can be demonstrated by using the switch statement. When it come to iterative structures C++ uses the while, do while, and for statements. Pretest works closely with the while statement. Posttest uses C++’s do while statement, and the for statement is used in fixed count iteration.

The code below gives an example of each structure. if (x < y) while (x < y) { cout << x; cout << x; } -------------------------------------------------- ---------- if (x < y) -------------------------------------------------- ---------- cout << x; do { else cout << 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" statement simply asks if x is less than y and if it less than y x is printed to the screen. The two-alternative/multi-alternative conditional using if/else asks if x is less than y and if true prints x. If it is not less than y then it asks if y is less than x. If this is true y is printed to the screen.

More statements can be added to this conditional to lengthen the multiple alternatives. Another multi-alternative conditional involves 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 with the while on the right. The pretest is initiated with the comparison of x to y. If the statement is true the loop is started and lasts until 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 continue the loop until x is greater than y. The for loop continues to add one to i until is not less than ten; the for loop demonstrates fixed count iteration. Java is very similar to C++.

As a matter of fact, Java control structures are almost exactly the same as C++. When it comes to conditional statements, Java uses the if and if/else statements. The else if statements can substituted for the case statement, but case is permitted. Iterative structures are seen 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("Handled at 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("still too low"); default: return i*i; } After viewing the C++ code on the previous page, it is easy to see the similarities between C++ and Java. The first simple conditional or if statement compares distance to r returning true if the condition is true. If distance is not less than r nothing happens. The second if statement is part of a two-alternative conditional.

If the condition a == 2 is met statement one is executed. If the condition is not met then the second alternative is asked and if found true the second statement is executed. The switch statement or non-deterministic conditional is just like the C++ implementation. The variable i is compared with the first case (0) and if true then the too low exception is executed. If not true the next case is examined and if true the still too low exception is sent back to the calling block. If none of the cases match then i * i is returned. The pretest iterative statement while is seen on the right side.

It simply states if i minus one is greater than 0 perform all statements within its loop. And while i minus one is still greater than 0 keep on looping. The for loop or fixed count iterative statement allows all statements below it to be performed while i is less than the shapes length. The integer i is incremented every time the loop is performed as indicated by the i++ located in the actual for statement. Looking at the different aspects of conditional and iterative structures C++ and Java are almost one in the same.

They both use the same statements to provide examples of the different types of structures. It is easy to see that Java was based on the syntax of the C and C++ programming languages. Pascal is a versatile language with control structures that almost match C and Java. The key difference between Pascal and C++ is not the actual conditional or iterative structure, but the difference is the syntax and key words. The key words used by Pascal for conditional structures include if, if/else, and case.

Iterative structures use the key words repeat/unti …