Home > Basic Programming > What I’ve been working on for the past week ‘-‘

What I’ve been working on for the past week ‘-‘

QBasic Tutorial 7 If Statements and Syntax

Some beginning Syntax:

IF<Condition> THEN  Do Something

ELSEIF<Conditon>THEN  Do Something

Else  Do Something

END IF

The Relational Operators

< Less Than

<= Less Than Or Equal To

> Greater Than

>=Greater Than Or Equal To

= Equal To

<> Not Equal To

Starting with a simple if statement:

If 5 > 2 THEN  Print “5 IS Greater Than 2” End IF

5 Is Greater Than 2 Appears which that statement is true.

If we replace the > sign with a < then this statement is not true and nothing pops up in the preview box.

Ex. IF 5 > 10 THEN     PRINT “5 is greater than 10” ELSE     PRINT “5 is less than 10” END IF

Now we’re gonig to have the user enter in two integers and we will check to see which integer is larger than the other.

So we start:

DIM Num1 AS INTEGER DIM Num2 AS INTEGER

CLS

INPUT “Enter First Integer: “, Num1 INPUT “Enter Second Integer: “, Num2

IF Num1 > Num2 THEN     PRINT Num1; “Is Greater Than”; Num2 ELSE     PRINT Num2; “Is Greater Than”; Num1 END IF

Try entering the integers 10 and 10

You get a respose 10 is greater than 10 and that’s not true so let’s have a look at that.

Change this by placing an Else IF statement.

Ex.

DIM Num1 AS INTEGER DIM Num2 AS INTEGER

CLS

INPUT “Enter First Integer: “, Num1 INPUT “Enter Second Integer: “, Num2

IF Num1 > Num2 THEN     PRINT Num1; “Is Greater Than”; Num2 ELSE*if Num2 > Num1 then     PRINT Num2; “Is Greater Than”; Num1 ELSE      —There is only 3 possible                          situations here.  Print “The numbers are the same”                —Num1 is Greater than Num2,Num2 is                                                     Greater than Num1, or that the     End IF                                             numbers are the same.

So the finaly code:

DIM Num1 AS INTEGER DIM Num2 AS INTEGER

CLS

INPUT “Enter First Integer: “, Num1 INPUT “Enter Second Integer: “, Num2

IF Num1 > Num2 THEN     PRINT Num1; “Is Greater Than”; Num2 ELSEIF Num2 > Num1 THEN     PRINT Num2; “Is Greater Than”; Num1 ELSE     PRINT “The Numbers Are The Same” END IF

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: