Introduction
The human life is filled with decisions. Every day, we as humans make millions of decisions, everything from when to get up to what to eat and each of those decisions brings on even more decisions. If we decide that we want something to eat, we must decide what we want to eat, where we're going to get it from, whether we are going to cook it ourselves or go get it from somewhere, how we want it cooked, if we have enough money to buy it, if we have enough time to eat it, and so forth. We make decisions based on our past experiences and our personal preferences.Computers are very good at making decisions. Computers make decisions, but their decisions are usually based on the content of a variable or the value of an expression instead of the computer's past experiences [unless of course you're dealing with artificial intelligence and then it's still based on variables].
Computers have several different decision making statements. These statements can be classified into two categories, a branch and a loop.
Branch Statements
Humans use branch statements daily on common decision making tasks. For instance, the decision whether a person has enough money to buy a particular item is considered to be a branch statement. The reason this is considered to be a branch statement is because if the person does not have enough money to buy the item then the person could buy a cheaper item or do something else. Basically a branch statement will branch off into two or more courses of action, depending upon the decision. Branch statements in English usually use the words if, then, and else. Branch statements in computers are very similar to those in English. Almost all computer languages have some form of a branch statement, most using the same word if to mark that a statement is a branch statement. For example, if the user pressed the "a" key, then do this else do something else. Both the Qbasic and the C/C++ language have a form of the branch statement.
Branch Statements in Qbasic
In Qbasic, the form of the branch statement is similar to the form in the English language in that it uses if-then-else. The if statement is followed by an expression that could have the value of true or false, if the expression is false then the computer does what comes after the then part of the statement and ignores the else part of the statement, otherwise it will ignore the then part of the statement and does the else part of the statement instead. The else part of the statement can be left alone if there is nothing to be done.A good example of this is how the absolute function works. The absolute value function checks to see if a number is less than 0, if it is then it takes the negative of that number [which makes it positive since the number is already negative], otherwise it leaves the number alone. So, the statement would be IF number<0 THEN number=-number. Since there is nothing to do if the number is 0 or greater, the else part of the statement is left off. The type of if statement shown here is considered to be a simple if statement since it only includes at most one command on the then branch and at most one command on the else branch. The following program illustrates the if statement.
IF a=1 THEN PRINT "Hello" |
IF a=1 THEN PRINT "Hello" ELSE PRINT "Bye" |
IF a=1 THEN ELSE PRINT "Bye" |
Program 19 - Qbasic IF Example
The other type of if statement is called a block if statement. The block if statement can have multiple commands on either the then branch or the else branch or both. The way to do a block if statement is to do the if statement as normal up to and including the then, then doing the program as normal with each statement on a line by itself, then when the block is done either do the else statement or tell the computer that this is the end of the if by including the statement end if. The following example demonstrates a block if with the block on the then, the block on the else, and the block on both.IF number<0 THEN PRINT "Number is negative." number=-number ELSE PRINT "Number is positive." END IF |
IF number<0 THEN PRINT "Number is negative." ELSE PRINT "Number is positive." number=2*number END IF |
IF number<0 THEN PRINT "Number is negative." number=-number ELSE PRINT "Number is positive." number=2*number END IF |
Program 20 - Qbasic Block IF Example
If statements can also be compound or nested and contain other if statements within them. The only caution here is that the programmer must make sure to close each block if statement by using the end if statement.Block if statements are also allowed to change the expression for each else statement included in the if. For example, if the programmer wanted to see if a>1 and if that was false then to check if c<0, that could be accomplished by using a compound if or by using the elseif command. Program 21 demonstrates the use of the compound if and also the use of the eleseif command.
IF x>0 AND x<50 THEN IF y>0 AND y<50 THEN PRINT z ELSE PRINT x*y END IF ELSE IF z>0 AND z<10 THEN PRINT y ELSE IF a=1 THEN PRINT x,y,z END IF END IF END IF |
IF x>0 AND x<50 THEN IF y>0 AND y<50 THEN PRINT z ELSE PRINT x*y END IF ELSE IF z>0 AND z<10 THEN PRINT y ELSEIF a=1 THEN PRINT x,y,z END IF END IF |
Program 21-Qbasic Compund If example
Branch Statements in C/C++
Simple if statements in C/C++ are very similar to simple if statements in Qbasic. The only differences between C/C++'s simple if statements and Qbasics are that in C/C++, the expression must be enclosed in a set of parenthesis ( () ), and also that the word then is omitted. Also, as usual in C/C++, a semicolon is added to the end of the line. Also, there is no difference between the C version of an if statement and the C++ version.Unlike most statements and functions covered so far in the C/C++ language, the if statement does not require an include file as the if statement is part of the language.
The following table illustrates the differences between the Qbasic simple if statement and the C/C++ version of the simple if statement. Note that the output commands are C++ output commands for simplicity and endl is not included.
Qbasic | C/C++ |
IF a=1 THEN PRINT a | if (a==1) cout<<a; |
IF a=5 THEN PRINT a ELSE PRINT b | if (a==5) cout<<a else cout<<b; |
Table 26 – Comparison of Qbasic and C/C++ Simple if statement
Block if statements in C/C++ are also similar to the Qbasic block if statements except that the form of a block in C/C++ is different. In C/C++, a block is defined as a sequence of programming statements that are between a set of braces ( { } ). What this means is that C/C++ does not require a end if statement, but instead requires that a program block be created. The following C++ code segments illustrate the block if in C/C++. Notice how the line that contains the if does not require a semicolon [if the line is broken between the parenthised expression and the next command], even though it is on a line by itself, also the else is the same way and does not require a semicolon. Also, notice how if only one command follows the if or the else, it does not require the braces, but if it has more than one command following it it does require the braces.if (number<0) { cout<<"Number is negative."; number=-number; } else cout<<"Number is positive."; |
if (number<0) cout<<"Number is negative."; else { cout<<"Number is positive."; number=2*number; } |
if (number<0) { cout<<"Number is negative."; number=-number; } else { cout<<"Number is positive."; number=2*number; } |
Program 22 - C/C++ Block if example
C/C++ also allows compound or nested if statements and these are done the same way as they are in Qbasic. It is advisable to put nested if statements into their own program block to keep from getting yourself confused as to which else goes with which if.As was done in Qbasic, the else if command allows the expression to be changed within the if statement. The only difference between the else if command in Qbasic and in C/C++ is that else if is two words in C/C++, else if, whereas it is only a single word, elseif, in Qbasic. The following program illustrates the compound if as well as the else if command.
if (x>0&&x<50) if (y>0&&y<50) cout<<z; else cout<<x*y; else if (z>0&&z<10) cout<<y; else if (a==1) cout<<x<<y<<z; |
if (x>0&&x<50) if (y>0&&y<50) cout<<z; else cout<<x*y; else if (z>0&&z<10) cout<<y; else if (a==1) cout<<x<<y<<z; |
Program 23 - C/C++ Compound If Example
Loops
Loops are useful in many different type of programs. A loop gets the computer to repeat a series of steps while something is true, until something is true, a certain number of times, or even infinitely.Humans use loops on nearly every task we do in our lives. Walking is performing a loop of taking a series of steps until we reach a certain destination. Reading is performing a loop within a loop. With reading, we read the a series of letters until we hit something that tells us that that series of letters is a word, and then we continue to the next word until the end of the sentence or book. Even speaking is a loop, we speak until we have nothing more to say. Loops are probably just as common as decisions are as far as humans go.
Computers are no different with the respect to loops. Some of the most common tasks can only be accomplished by using some form of a loop. For example, almost all games require some sort of loop that continues until the player wins or loses. Typing a character on the keyboard requires a loop because the computer has to wait until the user presses a key.
In Qbasic and in C/C++, there are several types of loops. There are loops that are used to count (for loops), loops that perform a task while a certain condition is true (do-while and while loops), and loops that perform a task until a condition is true (do-until loops).
For Loops in Qbasic
One of the easiest ways to get a Qbasic program to count is to use a for loop. The for loop repeats a series of steps and counts from a given starting value until a given ending value and increases by a given value each time. For example, the programmer may wish a variable J to count from 1 to 100 and increase by .5 each time through the loop, therefore it would repeat a certain series of steps 200 times. The format for the for statement is the word FOR, the variable to be used as a counter along with its initiation, the word TO, then the maximum count (can be a variable or expression), and optionally the word STEP and a step value (can be a variable or expression). For example, to make a for loop that counts from 1 to 5 and counts by 1 [the default], the for loop should look like: FOR j=1 TO 5. After the for statement there should be a program segment that needs to be repeated the number of times specified by the for loop. After the program segment should be the command next and the variable name which tells Qbasic where the for loop should end. This is best explained by the following program which counts from 1 to 10.
FOR J=1 TO 10 PRINT J NEXT J |
Program 24 - Simple Qbasic For Loop
If the programmer wanted to print the numbers from 10 to 1 and count down by -0.5, the program would look like the following:FOR count=10 TO 1 STEP -.5 PRINT count NEXT count |
Program 25 - Qbasic For loop showing Step Option
Just like if statements, for loops can be nested to perform fairly complex operations. For example the following program will print a multiplication table from 1 to 5. Notice how the latest for loop (K) is the one to have the soonest appearing next. The variable names on the nexts are optional.FOR J=1 to 5 FOR K=1 to 5 PRINT J*K; NEXT K PRINT NEXT J |
1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25 |
Program 26 - Nested QBasic For Loop
For loops can also be used as timing loops by not including a program segment between the for statement and its corresponding next statement. This technique is sometimes used in games or wherever a program needs to be slower. As a word of note, this technique is not very stable as the same for loop will run at different speeds on different machines. For instance, a loop that counts from 1 to 1,000,000 could take several hours on a machine built 5 years ago, but could only take a minute on a modern machine.For loops in Qbasic are useful when the maximum count is known, but are not very useful when the maximum count is an unknown value. Other loops are useful in this case.
Do Loops in Qbasic
In Qbasic, there are several forms of do loops. There is a do while a condition is true, and also a do until a condition is true. Each of these loops can be either a test at top or test at bottom loop. Each of these loops can be used in place of each other, but some are better suited for a particular task. The do while and do until loops are opposite of each other logically speaking so there is no need to specifically cover them individually but the test at top and test at bottom will next some explanation.Test at Bottom Do Loops
The do loop will continue to execute until a condition is true, or while a condition is true. The until or while clause must be specified as well as the condition. The format of the test at bottom do loop is the word do, followed by the sequence of commands to be executed in the loop, then the word loop followed by the clause and then the condition.In the case of the test at bottom do loops, the loop is allowed to be executed at least once before any testing of the condition is done. This type of loop could be useful in the case of input from a user and making sure they enter a specific value as is shown in the next program. As you can see, the two programs are identical except for the changing of until to while and also the "negative" logic where equals changes to not equals other than that, there is no difference in the programs and there is also no difference at all in what the programs do.
DO PRINT "Enter the number 1"; LINE INPUT A$ LOOP UNTIL A$="1" |
DO PRINT "Enter the number 1"; LINE INPUT A$ LOOP WHILE A$<>"1" |
Test at Top Do Loops
Sometimes it is useful to perform a test at the top to prevent a loop from executing. An example of this would be in a math-related program that must rely on a variable being a certain value before the equation will work [division for instance]. The format of the loop is exactly the opposite of the test at bottom loops, as expected. The do is followed by the clause and also by the expression and the loop is left by itself. The following program shows a test at top loop that assumes that an illegal input has been entered beforehand.DO WHILE a$<>"B" PRINT "Incorrect Value" PRINT "Please Press B" LINE INPUT A$ LOOP |
DO UNTIL a$="B" PRINT "Incorrect Value" PRINT "Please Press B" LINE INPUT A$ LOOP |
While Loops
Qbasic also supplies a while loop, but it is essentially the same as the test at top do while loop. It is included in the do loops for completeness. The format of this loop is similar to the do loops except that the do while is replaced by while and the word loop is replaced by wend [i.e. while end]. This loop is shown below.
WHILE a$<>"B" PRINT "Incorrect Value" PRINT "Please Press B" LINE INPUT A$ WEND |
DO WHILE a$<>"B" PRINT "Incorrect Value" PRINT "Please Press B" LINE INPUT A$ LOOP |
Infinite Loops in Qbasic
Infinite loops are useful in many cases and are sometimes done accidentally. An infinite loop is a loop that never ends and they can be created using any of the Qbasic loops.By using a for loop and setting the step equal to zero, an infinite loop can be created. By using the do-while [top or bottom test], or while-wend loops, a infinite loop can be created by making sure the expression is always true. By setting the expression to false, the do-until loop also becomes an infinite loop. The following programs show infinite loops.
FOR j=1 to 100 STEP 0 NEXT j |
a=0 DO LOOP WHILE a=0 |
DO UNTIL a<>a LOOP |
WHILE 1 WEND |
Ending Loops Prematurely in Qbasic
It is sometimes useful to exit a loop before it ends. For example, if an unexpected error occurred and there was no way to recover, it would be best to exit the loop, let the user know an error occurred and exit the program. With the exclusion of the while loop, the command exit can be used followed by the type of loop. The following program illustrates both exiting a for loop as well as a do loop.FOR J=-5 TO 5 IF J=0 THEN EXIT FOR PRINT 20/J; NEXT J X=0 DO WHILE X<50 X=X+1 IF X=20 THEN EXIT DO PRINT X LOOP |
For Loops in C/C++
The for loop in C/C++ is similar to the for loop in Qbasic, except the for loop in C/C++ is more versatile than it is in Qbasic. In C/C++, the for loop is a counting loop but it can also be used in more advanced topics such as moving between objects [near the end of the book you'll learn this].The for loop in C/C++ has three parameters, the same three that are used in Qbasic, the initiation, count, and incrementer. All of the parameters in the C/C++ for loop are enclosed in a set of parenthesis ( () ), similar to how an expression is enclosed in parenthesis in the C/C++ if statement. Besides all the parameters being enclosed in parenthesis, each parameter must be separated by a semicolon (;).
The first parameter of the for loop is the variable initiation. This is similar to the variable initiation in the Qbasic for loop. It is also common to declare the for loop variable within the loop initiation itself, but the variable's scope is only within that for loop.
The second parameter is a logical condition or expression, similar to what would be seen in a while statement. The for loop will exit when the while statement is false. In Qbasic, the C/C++ for statement expression would be considered to be a test at top do-while loop.
The third parameter is the incrementer or decrementor. This tells the loop how much to increase or decrease the variable by on each time through the loop. The loop variable should be specified along with how much it should be incremented or decremented by. For instance, if the loop's variable is J and to increase it by 1 each time through the loop, use J+1 [a common alternative to using J+1 is to use J++ and use J—for J-1].
Most for statements in C/C++ are usually block for statement, so just as with the if statement, enclose the sequence of statements within braces ( { } ) after the for loop.
The following C++ program shows a for loop that counts from 1 to 5. As with the if statement, the for loop does not require an include directive.
#include <iostream.h> int main() { for(int j=1;j<=5;j++) { cout<<j<<endl; } return 0; } |
#include <iostream.h> int main() { for(int j=1;j<=5;j++) { for(int k=1;k<=5;k++) { cout<<j*k<<" "; } cout<<endl; } return 0; } |
1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25 |
for(int j=0;j<100000;j++); |
Do Loops in C/C++
Unlike Qbasic, C/C++ only has a single version of the do loop and it is similar to Qbasic's test at bottom do-while loop. The format of the loop is the word do, followed by a sequence of statements, then the word while followed by the expression enclosed in parenthesis and then finally a semicolon. The following example illustrates the C/C++ do-while loop.#include <iostream.h> int main() { int a=0; do { cout<<"Enter the number 1"; cin>>a; } while (a!=1); return 0; } |
While loops in C/C++
C/C++ offers a loop similar to the while-wend loop in Qbasic. This loop is simply called the while loop. This is a test at top loop. The loop is created by using the while statement followed by an expression within parenthesis, like the do-while loops in C/C++ and then followed by a sequence of statements. The following program illustrates the while loop.#include <iostream.h> int main() { int a=0; cout<<"Enter the number 1"; cin>>a; while (a!=1); { cout<<"Please Enter the number 1"; cin>>a; } return 0; } |
Infinite Loops in C/C++
Infinite loops are useful in many cases and are sometimes done accidentally. An infinite loop is a loop that never ends and they can be created using any of the C/C++ loops.By using a for loop and setting each of the parameters to nothing [just putting 2 semicolons within the parenthesis], an infinite loop can be created and also an infinite loop can be created by incrementing the variable by nothing. By using the do-while, or while loops, a infinite loop can be created by making sure the expression is always true. The following program segments show infinite loops.
for(;;) { cout<<"1"; } |
do { cout<<"Here"; } while(1); |
while(1) { cout<<"Goodbye"; } |
for(int j=0;1;) { cout<<"Hello"; } |
Ending Loops Prematurely in C/C++
It is sometimes useful to exit a loop before it ends. For example, if the programmer designed an infinite loop to retrieve data from the user but the user does not wish to enter any more data, the loop would have to end prematurely. To end any type of loop in C/C++, the break command should be used. Program 38 illustrates the use of the break command to prematurely end loops.#include <iostream.h> int main() { for(int j=-5;j<=5;j++) { if(j==0) break; cout<<20/j<<endl; } int x=0; while(x<50) { x=x+1; if (x==20) break; cout<<x<<endl; } return 0; } |
Conclusion
Decision statements come in two major categories, the branch statement and the loop statement. In Qbasic and in C/C++, the branch statement is similar to the English if statement. The branch statement has an if part that tests whether a condition is true or false, if the condition is true, it does the then part of the statement, if it is false, it does the else part of the statement. If statements can also be blocked or in other words have multiple commands on the then or the else part of the statement or on both. If statements can also be compound or nested within each other. Table 27 shows a comparison of the Qbasic and C/C++ if statements.
Type | Qbasic | C/C++ |
Simple | IF a=1 THEN PRINT a | if (a==1) cout<<a; |
Simple | IF a=2 THEN PRINT a+2 ELSE PRINT b | if (a==2) cout<<a+2; else cout<<b; |
Block | IF b=5 THEN PRINT c b=c ELSE PRINT b END IF | if (b==5) { cout<<c; b=c; } else cout<<b; |
Block | IF b=5 THEN PRINT c ELSE PRINT b c=b END IF | if (b==5) cout<<c; else { cout<<b; c=b; } |
Block | IF b=5 THEN PRINT c b=c ELSE PRINT b c=b END IF | if (b==5) { cout<<c; b=c; } else { cout<<b; c=b; } |
Nested | IF b=5 THEN IF c=1 THEN PRINT B PRINT c ELSE b=c END IF ELSE PRINT b c=b END IF | if (b==5) { if (c==1) { cout<<c; cout<<b; } else b=c; } else { cout<<b; c=b; } |
Simple w/ else if | IF b=2 THEN PRINT c ELSEIF c=2 THEN PRINT B | if (b==2) cout<<c; else if (c==2) cout<<b; |
Both languages allow for breaking out of a loop before the loop condition becomes false. In Qbasic, the exit command will exit out of a loop and in C/C++, the break command will exit out of a loop. These are also illustrated in Table 28.
Loop can also be designed to never end or be infinite. Infinite loops are useful in games or in cases where an unknown amount of information will be retrieved. Table 28 also describes infinite loops.
Type | Qbasic | C/C++ |
For loop | FOR J=1 TO 10 PRINT J NEXT J | for(int j=1;j<=10;j++) { cout<<j<<endl; } |
For loop with non-default increment and premature exit | FOR J=-5 TO 5 STEP .5 IF J=0 THEN EXIT FOR PRINT 20/J NEXT J | for(float j=-5;j<=5;j+.5) { if(j==0) break; cout<<20/j<<endl; } |
For loop with decrement | FOR J=10 TO 1 STEP -1 PRINT J NEXT J | for(int j=10;j>=1;j--) { cout<<j<<endl; } |
Infinite For loop | FOR J=1 TO 10 STEP 0 PRINT J NEXT J | for(int j=1;j<=10;j+0) { cout<<j<<endl; } |
Infinite For loop | n/a | for(;;) { cout<<"Hello"<<endl; } |
Infinite For loop | n/a | for (int j=1;;) { cout<<"Hello"<<endl; } |
Infinite For loop | n/a | for (;1;) { cout<<"Hello"<<endl; } |
Test at Top While | DO WHILE a<>1 PRINT "Incorrect Value" PRINT "Please Press 1" INPUT A LOOP | int a=0; while (a!=1) { cout<<"Incorrect Value"<<endl; cout<<"Please press 1"; cin>>a; } |
Test at Bottom While | DO PRINT "Enter number 1"; INPUT A LOOP WHILE A<>1 | int a=0; do { cout<<"Enter number 1"; cin>>a; }while(a!=1); |
Test at Top Until | DO UNTIL a=2 PRINT "Incorrect Value" PRINT "Please Press 2" INPUT A LOOP | int a=0; while (a!=2) { cout<<"Incorrect Value"<<endl; cout<<"Please Press 2"; cin>>a; } |
Test at Bottom Until | DO PRINT "Enter 1"; INPUT A LOOP UNTIL A=1 | int a=0; do { cout<<"Enter 1"; cin>>a; }while(a!=2); |
While Loop | WHILE a<>2 PRINT "Incorrect Value" PRINT "Please Press 2" INPUT A WEND | while (a!=2) { cout<<"Incorrect Value"<<endl; cout<<"Please Press 2"; cin>>a; } |
Infinite While Loop | DO LOOP WHILE 1 | do { }while(1); |
Infinite Until Loop | DO LOOP UNTIL A<>A | do { }while(a==a); |
While Premature exit | n/a | int a=0; while(1) { a++; if(a>=50) break; cout<<a; } |
Do Premature exit | DO X=X+1 IF X=20 THEN EXIT DO PRINT X LOOP WHILE x<50 | int x=0; do { x=x+1; if(x==20) break; cout<<x; }while(x<50); |