You are on page 1of 14

COMPUTER PROGRAMMING

INKEY$ and RND Function

Dr. USMAN AKMAL


Dr. ALI AHMAD

DEPARTMENT OF CIVIL ENGINEERING


UNIVERSITY OF ENGINEERING AND TECHNOLOGY,
LAHORE
CONTENTS

❑ INKEY$

❑ RND Function

2
INKEY$ FUNCTION
The INKEY$ function gets input from the
keyboard. INKEY$ can get only one character at a
time from the keyboard, not an entire string of
characters, as INPUT can. Following is the syntax;

INKEY$

The return value of INKEY$ is the character typed


at the keyboard. This value is usually assigned to a
string variable.
INKEY$ gets its character input and passes control
to the next statement without waiting for an Enter
keypress.
3
INKEY$ FUNCTION
The INKEY$ function requires one other
consideration. When you want to get a character
with INKEY$, you must put the INKEY$ function
in a loop. Otherwise, INKEY$ does not wait for the
user to type anything. If a key is not pressed at
exactly the same time that the INKEY$ function
executes, it returns a null character and moves on.

4
INKEY$ FUNCTION [EXAMPLE 1]

CLS
100 PRINT
PRINT
PRINT "Civil Engineering"
PRINT
PRINT
PRINT "Print again (Y/N)"
DO
ans$ = INKEY$
LOOP UNTIL ((UCASE$(ans$) = "Y") OR (UCASE$(ans$) = "N"))
IF UCASE$(ans$) = "Y" THEN GOTO 100
IF UCASE$(ans$) = "N" THEN END
END

5
INKEY$ FUNCTION [EXAMPLE 1]

6
INKEY$ FUNCTION [EXAMPLE 2]
CLS
PRINT "What do you want to do"
PRINT
PRINT "1. Calculate area of rectangle"
PRINT "2. Calculate area of triangle"
PRINT "3. Calculate area of circle"
PRINT
PRINT "Please press the number of your
choice..."
DO
choice$ = INKEY$
LOOP UNTIL ((choice$ = "1") OR (choice$ = "2")
OR (choice$ = "3"))
PRINT "You selected "; choice$
END
7
INKEY$ FUNCTION [EXAMPLE 2]

8
RND FUNCTION
RND is the function for generating a random
number. It gives a decimal fractional value between
0 and 1 (but never exactly 0 or 1)
Output
CLS
.7055475
PRINT RND

To generate 20 random integers between 0 and 9


following code is used;
CLS
FOR i = 1 TO 20
PRINT INT(10*RND)
NEXT i
9
RND FUNCTION
If random fraction is less than 0.1 the previous code
will print 0 and if it is greater or equal to 0.9 it will
print 9. To get random numbers between 1 and 10,
add 1 in the previous code as follows;

CLS
FOR i = 1 TO 20
PRINT INT(10*RND)+ 1
NEXT i

Hence, to produce random number from 1 to N, use


the following statement;
INT(N*RND)+ 1
10
RND FUNCTION
RND function is always used with RANDOMIZE
statement. IF RND is used without this, every time
we run the program same output will be obtained.
You could put the following RANDOMIZE
statement at the beginning of any program that
uses the RND function;
RANDOMIZE TIMER
TIMER is the computer time in seconds and is
always changing.
RANDOMIZE TIMER
FOR i = 1 TO 20
PRINT INT(10*RND)+ 1
NEXT i
11
RND FUNCTION [EXAMPLE 1]
[EXAMPLE 1] Program to roll a dice.

CLS
RANDOMIZE TIMER
PRINT INT(6 * RND) + 1
END

12
RND FUNCTION [EXAMPLE 1]
[EXAMPLE 1] Program to print 10 random numbers
from 1 to 100.

CLS
RANDOMIZE TIMER
FOR i = 1 TO 10
n(i)=INT(100*RND)+1
NEXT i
END

13
END OF LECTURE

You might also like