Basic learn program




















This tells the program to go to a subroutine starting on line The same subroutine can be called in many places , so we can separate a piece of program logic we use frequently in a subroutine, and call it anywhere in the program we want. Subroutines also make the program more structured by logically separating its parts. Why include two subroutines on lines and in our example? But they do this a little differently. As you can see, in our example the subroutine starting on LINE is never called.

In your programs, sometimes you want to include several subroutines which accomplish the same thing differently, to test which one works better faster, uses less memory, etc.

Another computer number format: hexadecimal numbers. Earlier in this tutorial we looked at binary numbers. They are helpful for understanding how computers store and communicate information, but typically are not used explicitly in computer programs because writing numbers in binary format is not very convenient. Instead, another, more compact format, called hexadecimal or Hex, for short , directly related to binary format, is often used when we want to work with individual bits.

The base of this format is 16, which corresponds to 4 binary bits. It means that every byte or 8 bits can be written as 2-symbol hexadecimal number. The hex format uses 16 symbols to represent 16 different values the 4 bits can represent: numbers to represent values from zero to nine, letters A,B,C,D,E,F to represent values from ten to fifteen.

The following program prints numbers 0 to 20 in decimal and hexadecimal format:. The procedure of converting a decimal number to hex format is moved to a subroutine starting at LINE The conversion algorithm is similar to the one we used for binary numbers , except instead of base 2 we use base Divide the digital number A by 16 and take the remainder of the division.

Let's call this remainder B. Possible values of B are zero to fifteen. Subtract B from A. Lets call the result of this subtraction C. Number C will always be a multiple of Divide C by 16 and assign the result to variable A. If A equals to zero, we are finished converting the number to hex format. If A is greater than zero, go to step 1 with new value of A and write the next hexadecimal letter to the left of the letters you already written.

Sometimes we want to display text information which does not fit in one screen. A table with rows, for example, will not fit in screen with 24 rows. In this case we can split it into several "pages", letting the user to move between pages by pressing a key often different keys are utilized to move up or down the document. The following program uses "paging": it prints every next 20 numbers in decimal and hex format, refreshing the screen with new set of numbers when user presses a key.

It is using a subroutine beginning at LINE to do the decimal to hex conversion. Suggested exercise : another number format used in computers, called Octal , has base of 8. The symbols used in this format are digits zero to seven. Modify the paging program shown above to add a new subroutine, which converts decimal numbers to octal format, and print a third column showing octal numbers, as shown in the picture below:.

Converting numbers from one base format to another is a good way to practice your programming skills in BASIC. This is why I encourage you to do more: Suggested exercises : Write a program to convert Hex number to decimal.

As input, you have a string containing only digits 0 to 9 and letters A to F. Use a subroutine for this and call it each time you need to convert hex string to a decimal format.

You may recall that when we converted binary string to decimal , we used 2 different approaches: parsing input string from right to left, and parsing it from left to right.

You can use the same 2 methods when converting a hex number to decimal. Which of the two methods is easier to do on paper? Which one is faster? Convert hex string to binary string by calling two subroutines in a row: the first one converts hex to decimal, and second one converts the decimal we got to a binary.

Is this the most efficient way of converting a string representing a hex number to binary format? Drawing Fibonacci spiral with arcs advanced. The Fibonacci numbers we looked at earlier can be also represented visually in a two-dimensional space, by drawing adjacent squares with sides equal to the subsequent Fibonacci numbers : 1,1,2,3,5,8 and so on.

The method is the following:. We draw the first square with side equal to 1. Then just above it we draw another square with side of 1; the two squares must border each other.

Then we move left and draw a third square with side of 2, which borders the two squares with already have. Then we move down and draw another square with side 3, having a common border with squares 1 and 3. Fibonacci spiral is a line consisting of a sequence of arcs each being one quarter of a circle drawn within each of the Fibonacci squares.

The radius of each arc equals to the side of the corresponding square. As user presses a key, it draws a new square and next part of the Fibonacci spiral. We draw squares in purple and spiral in white. As spiral grows, eventually we will go outside of the allowed screen coordinates. E gets checked on lines and - in case it is set to 1, we clear the screen and all variables LINE 10 and start the program all over.

Until now we used variables to store individual values. What if we need to store a number of values, for example, we want to store 12 values, each containing a number of days in a given month of the year let's leave issue of number of days in February aside for now?

Do we need to declare 12 individual variables? What if we want to store a values? It would be very inconvenient to individually declare variables and coming up with unique name for each.

Fortunately, there's a better way: arrays. Arrays are specifically designed to be declared once and be able to store a number of values, which are referenced by array INDEX. Arrays are declared by keyword DIM, followed by a unique array name similar to variable names, only the first 2 symbols of the name are important in AppleSoft BASIC and the array size in parenthesis , specifying how many elements it should hold.

Size must be integer value greater than zero. It can be specified as integer constant or as numeric variable holding integer value. We reference individual elements of the array by index.

Here's the example:. In this example, we declare array A which can hold up to 5 numbers. We then ask the user to enter 5 numbers and put them into array elements, one by one. Staring from line , I test what happens if I access array by an invalid index such as 0, -1 or 6. Interestingly, index 0 does not show an error, and values -1 and 6 both invalid in our case result in different errors.

Try it yourself by commenting out line Arrays also can hold strings. Similar to string variables, to declare a string array we end its name with dollar symbol. The following snippet asks the user to enter 5 strings, stores them in a string array and outputs all of them in a single line separated by commas. In this tutorial we will be using arrays in many examples, but to keep things simple I use one-dimensional arrays in most cases. These functions are blocking : the program is suspended while waiting for user input until user presses Enter or in case of GET command waits for a single key being pressed.

What if we are programming a game where we want to allow user to press navigation keys such as arrows but at the same time don't want the program to stop waiting for the input?

We can accomplish this by checking the keyboard status with PEEK which returns true only when keyboard was pressed. Then we call GET to retrieve the pressed symbol and use it:. They include all English alphabet letters both uppercase and lowercase , digits, punctuation characters and special keys such as Escape, Tab, Space, arrow keys and so on. For example, Left, Right, Up and Down arrow keys have codes 8, 21, 11 and 10 correspondingly. We can use ASCII codes not just to detect special navigation keys such as arrows but also for example, for converting strings to uppercase or lowercase.

Checking the ASCII table we see that English uppercase alphabet characters have decimal codes 65 A to 90 Z , and the lowercase characters have decimal codes 97 a to z. The following code demonstrates how to convert any English string to lowercase:.

Suggested exercise : modify the above program to convert all symbols in the entered string from lowercase to uppercase letters. This version will use TEXT character mode and will draw a snake of unlimited length as user presses arrow keys. In this case I am using a subroutine which starts at line It detects which arrow key was pressed, adjusts the cursor position X,Y , moves the cursor there and outputs letter "A".

However, we still get undesired effect of screen scrolling down when snake moves to 40,24 - the bottom right corner of the screen. To avoid this, we limit X to the maximum value of 39 not It is fairly easy to adjust the program for the graphics mode: instead of drawing letters we draw square outlines this is much faster than drawing the solid the rectangle of the same size.

In my case I made each square 8x8 pixels in size, and change the color as I move the snake something that was not possible in the text mode :. Below is the result: Note that on lines and we compare X and Y against 0 not against 1, which is the minimum allowed value in the TEXT mode version. The next step is to draw the snake of a given limited length of N. To accomplish this we use arrays: to store all positions X,Y snake moves to, and by removing the oldest position the tail as we move forward when total length exceeds N.

I am using 2 arrays, XM and YM both sized to keep up to N elements to store all sequential snake positions, and a special variable C to keep track of the head position this variable is initially set to 1 within these arrays.

The tail position also pointed by POS is "erased" by printing space, and corresponding position in the XM and YM arrays is immediately filled with new position of the current snake head. The technique is commonly known as queue or FIFO first in, first out. The modified version for Snake program in text mode now looks like this:. In HGR2 mode, make it change color only when snake changes direction as opposed changing color on every move. Often we need to order the data which is stored in the array.

Think for example of your cell phone contacts: when you open the list of contacts they appear on screen ordered alphabetically, from A to Z. But most likely you have not entered them in this order on your phone! So the program displaying them somehow re-ordered them for you, so that you could find the contact quicker by looking it up alphabetically. This process is known as sorting, and it's very common in programming.

The method we will use is called Bubblesort algorithm. This is not the quickest way to sort an array but it is fairly simple and is good for our purposes. If given two elements are not in the order we want them to be, we swap them. Then we increment I by one and repeat the process again until we reach the end of the array. We also keep track of how many swaps we made during the latest walk. After this, we repeat the whole process again until no swaps were done in the latest walk.

The following program shows sorting array of eight numbers entered by user in the ascending order:. Suggested exercises : Modify the above program to sort numbers in the descending order large to small. Make program faster almost 2 times by using the fact that after the Nth walk the last N elements are already sorted. For example, when sorting numbers in the ascending order as in the example above after pass one the largest number is stored in A 8.

After second pass two the second largest number is in A 7 and so on. Hint: store number of passes in a separate variable. We can apply the same bubblesort algorithm to sort strings.

We declare a string array, enter data and use the same compare operator to compare strings. Strings will be compared alphabetically. Here is the modified program:.

As you can see, the string starting with a digit "" is on top of the list, followed by strings starting with uppercase letters, and then followed by all lowercase words. In computer programming string comparison is typically based on comparing the codes in our case ASCII codes of the individual symbols, one by one. When comparing two strings, we take first symbols in both strings and compare their ASCII codes; the one with a smaller ASCII code is considered a "smaller" string; in case the first symbols of both strings are the same, we look at the second symbol in each string, and so on, until we find a mismatch.

This explains the order of strings produced by our program. Now, let's make the ordering condition a bit more complex: we will sort an array of strings by string length in ascending order shortest first. When two strings have the same length, they must be ordered alphabetically as in the earlier example.

The following program demonstrates how this is done:. More math: using arrays for finding prime numbers. In this lesson, we will use array to construct a Prime sieve , a sequence of all natural numbers up to given N which are prime divisible only by 1 and itself.

A simple and not very fast method to accomplish this is to divide every given number K in the range from 1 to N by all integers from 2 to SQR K square root of K sequentially until at least one divides K to determine if it's prime.

In my approach as I test more and more numbers starting from 2 for being prime, I accumulate information on all prime numbers in the range from 2 to SQR K-1 , where K-1 is previous number we tested, storing them in a special array. Therefore, in order to test next number K for being prime, I divide it by all primes in range I also need to decide what should be the size of array CAP variable to keep all prime numbers between 1 and N, to avoid overflowing of the array.

N which potentially can be prime. Suggested exercises : Speed up the program by using the fact that we only need to check odd numbers for being prime using STEP parameter on line Also, odd numbers are not divisible by 2, so we can skip dividing them by 2. Make program take N value from the user. You will need to adjust the CAP value accordingly. Sometimes in your program you want to include information which will be used every time the program runs.

For example, in a program showing a calendar, you want to store the names of all 12 months, and also the number of days typically present in every month. Or in a program playing music, you want to store the sound frequencies for every tune you will be playing. Every DATA line can hold a number of elements, separated by commas; when you want to include string data with spaces, you should enclose strings in double quotes. The program can have as many DATA lines as you want. If you try to call READ after all elements already been read, the program will show error 42 "End of data".

It is not possible to reset the pointer to read the data starting from a specific line: all DATA lines are treated as one chunk of data. Sometimes we don't know in advance how many items are present in DATA elements. The following snippet shows an example of using DATA to store information into an integer array. First we read all the data, counting how many elements it has in variable C. Initializing arrays in this way is fine when we know that all data in our DATA elements has the same type in our case they are all numbers.

What happens if one of the elements holds a string which cannot be converted to a number? Moreover, as you build your projects add it to your profile or your GitHub account, this would help you in the future when you look for a job in development.

This is where most people get stuck at the deciding stage so what to build? The solution is simple. Let us see it below:.

Whether it comes to studying or coding you must do what interests you the most. You must enjoy the project area you choose so that you are passionate about it and it keeps you engage until built. If you choose something that is not of your interest you may end up giving up your project in the middle as you might eventually lack interest in it. So choose something that keeps you held upon itself like if like playing games then you might just want to develop a video game of your choice.

Similarly, if you like photography you might want to build up your portfolio website showcasing your work or if you are someone who is interested in trading you might design an app or website to analyze your stock charts. Analyze and give it a thought that what you like before you begin to build. Being confident about your capabilities is good but being overconfident is not. So it is recommended that you begin with simple and easy projects to explore the language more before you dive into building complex projects.

For example, if you choose to build a video game do not straightaway begin with the complex video game League of Legends instead begin within something like tic-tac-toe or if you want to build a website do not start with building something like Amazon or Facebook instead go for something easier like a to-do list. Be innovative and build something that is useful for you as well as that interests the community. If you are puzzled about where, to begin with, we have got you few ideas to start with your project building.

You could begin with a simple website like making a to-do list or if you want to make a game app then games like Tetris, sudoku, and the flappy bird are good to start with learning programming. If you want something challenging you might want to consider building a website similar to Twitter but with not as many features as Twitter offers but with some basic functionality like to tweet and follow.

Hackr gives you many project ideas for several languages ranging from the beginner level to the advanced level. Check out the projects of different languages here:. There would come a time while you develop the projects that you would feel stuck it could anything from getting errors, your program crashing without any message or even your coding executing fine but not generating the output you desired you might get so restless even sometimes that you might want to give up.

What do you do in such scenarios? Stay motivated, and to help consider below the points to continue working on your project. This is a crucial step that you must master. Searching and surfing the error of your code would help you correct your code within few minutes but on the other hand, if are not sound at this skill it would be like diving into a whirlpool of code without a map. This way Google would specifically target the error as the same sentence and that would give a much accurate filtered result.

Websites like Stack-Overflow and Reddit top the charts here to guide developers around the world with their code. It is a full-fledged community of developers from all fortes that come up to help each other in their projects.

Posting your doubts here could even take weeks to get a response but it is worthy, although you might already end up finding the solution to your error as many people might have come across the same error before. By entering a coding or programming course, you will be introduced to different cultures and even finish university with an international network of contacts. Programming is an international language, with various computer codes being implemented around the world in relation to a variety of applications and companies.

Check for opportunities that interest you outside of your home country. A semester or a year abroad will provide you with unique insight into how technology is used around the world, a chance to be exposed to differing cultures and to learn new languages. The demand for computer programming knowledge is growing. Recent graduates in the computer science field earn, on average, pretty significant starting salaries.

NACE The National Association of Colleges and Employers reported some of the highest starting salaries earned by graduates of bachelor degree programs:. If you are an independent contractor, you can benefit from a flexible schedule and a comfortable work environment. Many programmers choose to work with individuals on a project-by-project basis in order to earn extra cash during their free time.

Even if you are still learning basic programming, try to start getting some hands-on experience while you have the time and energy! One benefit of learning how to code at a young age is enhanced academic performance. Even elementary schools use technology in the classroom for testing and other activities. Learning how this technology works will prepare children for a quickly advancing world in which computers and smartphones are utilized for almost every function of our daily lives.

Computational thinking is the ability to logically communicate your thoughts in a structured manner. This type of thought process trains problem solving skills that are replicated later in life, much like the memory of a computer.

Software engineers, computer programmers, and logistics specialists all use this method, combining advanced mathematics, algorithm development and logic. Like any language spoken or written , it is easier to learn earlier in life. Computer languages teach us logical skills in thinking, processing, and communicating. Combined with creative visions, some of the most influential products were designed around a programming language. And, the best inventions are born where skill and creativity meet.

After all, who is better than the young generations of the world to imagine the future of our technology? UoPeople uses cookies to enhance your experience, to display customized content in accordance with your browser settings, and to help us betterunderstand what your needs are. To learn more about the cookies we use, see our Privacy Policy. Apply Now. Request Info. Ask a student. UoPeople Arabic. Ask Me Anything. If not, you can do math with numbers and variables.

Now add to the beginning of the code: fastgraphics and to the end: color black circle , , refresh so that it looks like this: fastgraphics clg cls print "Hello, world! Replace "circle , , " with "rect 0, 0, , " And what do you see? A square instead of a circle! You can erase other colors with it. Try it out! Replace "color black" in the program with "color blue", for example. Tip 3 years ago. Cool language to teach kids. There is no swift2 for Windows yet the lab runs on windows , but the kids loves to make cool graphics with it, music, and paint with the mouse mouseX, MouseY.

It's cool and fun for 4th, 5th and 6th graders, and I am writing a blog in spanish about each class for them, please visit.



0コメント

  • 1000 / 1000