## QBasic Boolean Techniques

QBasicTutorial 20 Boolean Techniques

What this program will do will make an array of integers and have the user enter in

the numbers and a number to search for. If the computer finds the number then a value

will be loaded into a variable called found. That we can use to see if the computer

has found the variable or not.

First we declare the numbers array. There will be 5 elements as integer:

DIM Numbers(5) AS INTEGER

Next we declare the search number:

DIM SearchNum AS INTEGER

Severl for loops so make x as integer:

DIM x AS INTEGER

Declare Found as integer:

DIM Found AS INTEGER

Clear the screen

So far we have:

DIM Numbers(5) AS INTEGER DIM SearchNum AS INTEGER DIM x AS INTEGER DIM Found AS INTEGER

CLS

Start the for loop for x equals 1 to 5:

FOR x = 1 TO 5 INPUT “Enter Number: “, Numbers(x) —-User Input, then place the input into

the numbers array. NEXT x

CLS

INPUT “Search Number: “, SearchNum —User input the search number,placing

the input into SearchNum.

Print 2 spacers by:

PRINT PRINT

Set found to equal 0:

Found = 0

So now we have:

DIM Numbers(5) AS INTEGER DIM SearchNum AS INTEGER DIM x AS INTEGER DIM Found AS INTEGER

CLS

FOR x = 1 TO 5 INPUT “Enter Number: “, Numbers(x) NEXT x

CLS

INPUT “Search Number: “, SearchNum

PRINT PRINT

Found = 0

In Boolean Logic 0 is False and 1 is True, so setting found to 0 basically what it is

were doing is saying Found starts off as false.

So next we’ere going to search the array for the number.

FOR x = 1 TO 5 IF SearchNum = Numbers(x) THEN Found = 1 —-So we’ve set found to equal true to

this point.

END IF

NEXT x —Ending the FOR statement.

Now we will print the confirmation: –If found = 1 then we know we have found

the number.

IF Found = 1 THEN PRINT “The Number IS In The Array” ELSE PRINT “The Number Is Not In The Array” END IF

Ok so go ahead and run:

DIM Numbers(5) AS INTEGER DIM SearchNum AS INTEGER DIM x AS INTEGER DIM Found AS INTEGER

CLS

FOR x = 1 TO 5 INPUT “Enter Number: “, Numbers(x) NEXT x

CLS

INPUT “Search Number: “, SearchNum

PRINT PRINT

Found = 0

FOR x = 1 TO 5 IF SearchNum = Numbers(x) THEN Found = 1 END IF NEXT x

IF Found = 1 THEN PRINT “The Number is in the Array” ELSE PRINT “The Number is not in the Array” END IF

Another teqnique would be to go to the top and replace DIM Found AS INTEGER with DIM Found AS STRING.

Change Found = 0 to Found = “FALSE”

Instead Found = 1 set to Found = “TRUE” –Get both of these Found = 1

There is a downside to using this technique it is that if I was using lowercase for

the true and false statements when ran the program gives bad results. This means

uppercase is different than lowercase. Make sure if you use this technique you are

using the same case for the whole program.

The final technique

Go back and replace FOUND AS INTEGER at the top in the declare statements.

Then we will make 2 constants: One called false set to = 0, and true set to = 1

CONST False = 0 CONST True = 1

This follows your declare statements.

The first Found we will set to FOUND = False

The Found statement under SearchNum = Numbers(x) THEN:

Found = True

Right under this you have an IF statement, for this method replace the statement with

this:

IF Found = True THEN

So Found is initialized as Flase which also equals 0 so Found = 0.

Here at this statement:

Found = True

This means if we find the number then Found will = True which is also Found will = 1.

Then the final statement changed:

IF Found = True THEN

So for the final techniques the code would be:

DIM Numbers(5) AS INTEGER DIM SearchNum AS INTEGER DIM x AS INTEGER DIM Found AS INTEGER

CONST False = 0 CONST True = 1

CLS

FOR x = 1 TO 5 INPUT “Enter Number: “, Numbers(x) NEXT x

CLS

INPUT “Search Number: “, SearchNum

PRINT PRINT

Found = False

FOR x = 1 TO 5 IF SearchNum = Numbers(x) THEN Found = True END IF NEXT x

IF Found = True THEN PRINT “The Number is in the Array” ELSE PRINT “The Number is not in the Array” END IF

Go ahead and run and you will see the array works correctly as well as the program.

Hello there, I found your website via Google while searching for a related topic, your site came up, it looks good. I’ve bookmarked it in my google bookmarks.

fantastic post, very informative. I wonder why the other experts of this sector don’t notice this. You should continue your writing. I’m sure, you have a huge readers’ base already!