0% found this document useful (0 votes)
13 views

expanded_basics_programming_class5

Basic programming
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

expanded_basics_programming_class5

Basic programming
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Beginners' All-purpose Symbolic Instruction Code (BASIC) Programming

Introduction to BASIC Programming

BASIC (Beginner's All-purpose Symbolic Instruction Code) is a simple and easy-to-learn

programming language.

It is designed to be an excellent starting point for beginners who want to understand programming

and logic.

BASIC helps develop problem-solving skills and introduces key programming concepts that can be

applied to many other languages.

In this document, we will learn the fundamental concepts of BASIC programming, such as variables,

input/output, arithmetic operations,

conditional statements, loops, and how to write simple programs.

Setting Up the Environment

You can start coding in BASIC using online compilers like Replit or other local interpreters.

1. Visit Replit's BASIC interpreter: [https://replit.com/](https://replit.com/).

2. Create a new BASIC project and start writing your code.

It's easy to get started and see results instantly in these environments.

Basic Concepts: Variables, Input, and Output


**Variables:**

In BASIC, a variable is a name used to store a value. You don't need to declare a variable before

using it;

just assign a value to it.

Example:

```basic

LET Age = 10

LET Name$ = "Alice" ' The $ symbol is used for string variables

```

**Input and Output:**

- `INPUT` statement asks for user input.

- `PRINT` statement displays output to the screen.

Example for Input:

```basic

INPUT "Enter your age: ", Age

```

Example for Output:

```basic

PRINT "Hello, "; Name$

```
The `;` symbol allows you to print multiple items in one line.

Arithmetic Operations: Addition, Subtraction, Multiplication, Division

**Addition:**

```basic

LET Sum = 5 + 3

PRINT "Sum: "; Sum

```

**Subtraction:**

```basic

LET Difference = 10 - 4

PRINT "Difference: "; Difference

```

**Multiplication:**

```basic

LET Product = 6 * 7

PRINT "Product: "; Product

```

**Division:**

```basic

LET Quotient = 12 / 4
PRINT "Quotient: "; Quotient

```

These are the basic arithmetic operations you can perform in BASIC.

Conditional Statements: IF...THEN...ELSE

**IF...THEN...ELSE:**

This is used to make decisions in the program based on certain conditions.

Example:

```basic

IF Age >= 18 THEN

PRINT "You are an adult."

ELSE

PRINT "You are a minor."

END IF

```

If the condition is true, the code inside `THEN` will execute; otherwise, the `ELSE` part will run.

Loops: FOR...NEXT and WHILE...WEND

**FOR...NEXT Loop:**

Repeats a block of code a specific number of times.


Example:

```basic

FOR I = 1 TO 5

PRINT "This is loop iteration "; I

NEXT I

```

**WHILE...WEND Loop:**

Repeats a block of code while a condition is true.

Example:

```basic

LET Counter = 1

WHILE Counter <= 5

PRINT "Counter is "; Counter

LET Counter = Counter + 1

WEND

```

Loops are useful when you need to repeat tasks multiple times.

Functions and Procedures

**Defining a Function:**

Functions are used to perform specific tasks and return values.


Example:

```basic

DEF FN Square(X) = X * X

LET Result = FN Square(4)

PRINT "The square of 4 is "; Result

```

This function calculates the square of a number.

Practical Examples

**Example 1: Simple Calculator**

```basic

INPUT "Enter first number: ", Num1

INPUT "Enter second number: ", Num2

LET Sum = Num1 + Num2

LET Difference = Num1 - Num2

LET Product = Num1 * Num2

LET Quotient = Num1 / Num2

PRINT "Sum: "; Sum

PRINT "Difference: "; Difference

PRINT "Product: "; Product

PRINT "Quotient: "; Quotient

```
**Example 2: Age Checker**

```basic

INPUT "Enter your age: ", Age

IF Age >= 18 THEN

PRINT "You are an adult."

ELSE

PRINT "You are a minor."

END IF

```

**Example 3: Multiplication Table**

```basic

INPUT "Enter a number: ", Number

FOR I = 1 TO 10

LET Result = Number * I

PRINT Number; " x "; I; " = "; Result

NEXT I

```

**Example 4: Counting Down**

```basic

LET Counter = 10

WHILE Counter >= 1


PRINT "Countdown: "; Counter

LET Counter = Counter - 1

WEND

```

**Example 5: Simple Interest Calculator**

```basic

INPUT "Enter principal amount: ", Principal

INPUT "Enter rate of interest: ", Rate

INPUT "Enter time in years: ", Time

LET Interest = (Principal * Rate * Time) / 100

PRINT "Simple Interest: "; Interest

```

You might also like