|
|
|
|
Remember, in this page we are not talking about any particular language. Only difference between any two different language or development tool is its own commands and syntax. Hence any example given here not adhere to any one language in particular. VariablesYou first aspiration is write a program. What will your program do? How are you going to write them? Say for example you want to add numbers and display the sum on the output device, screen. Next question is whether you want to just add two numbers and print them. That's not interesting. What is interesting get any two numbers from the user and then add them up and display them. So takes us to use variables. According to our requirement we need to store them in the memory, at least for the span of the program. We also say we will input two numbers, that means the memory should hold numbers. How to instruct the computer to use memory and to then say it should hold numbers only. Answer to the first part of the question is whenever a variable is declared (used) the memory is accessed. (Even your program runs in the memory.) Before answering the second part of the question let us analyse what would we do if we want to store user's name, his/her date of birth, salary, pension, etc. details. You know the answer. In the 'binaries' section we had seen how we can store a number in a byte's memory. You had also seeen we could store only 255 in a Byte memory space. If we want to store a value greater than 255 we have to another continuous byte ie. two bytes to store a large values. If we want to use a character, because it is in ASCII character set, one character takes one Byte memory space. So we use datatypes to instruct computers to allocate specific amount of memory space according to the datatype given. Different datatypes commonly used in most of the languages are :
Based on this knowledge we will write the code as follows:
Good piece of codeInclude comments to make your program more readable. Normally your code is reused by somebody else at some point of time and that happens when are not there or you left the job. int num1 ; 'int' is the datatype and 'num1' is the memory location at which the data will be stored. In other words variables are named location of some part of memory. Semicolon(;) is end of any command line in most of the languages. In some languages comments can be nested, which means comment within another comment. sum = num1 + num2 ; This line is called an expression. Here = is called an assignment operator. Some of the common operators are :
Variables on either side of an operator is called operand. In the above expression num1 and num2 are two operand. Some languages support (e.g., C) trinary operator. Trinary operator (?:) behaves like a simple if-else statement (see next section: Conditions). e.g., MaxNum = num1 > num2 ? num1 : num2 ; //Trinary operator (?:) In the above example if num1 is greater than num2 then num1 is the maximum number 'MaxNum' else num2 is the maximum number. ConditionsIn the above sample code how to check for positive numbers only. That question introduces us to conditional statements in any one language you will be learning.
In some language 'end if' may not be used. There can be multiple statements between if-else and else-end if. Simple 'if' statement may not have an 'else' part to it. A complicated 'if' statement can have many 'elseif' and within each section there can be another 'if' statement. This kind of code structure is called a nested 'if' statement. In the above 'if' construct '>' is called a logical operator. Some of the logical operators are : =/==, <, >, <=, >=, <>, != In some languages you cannot use =/== ( single = or double = ) to compare two strings. List of functions and their useNumerical Functions
There may be statistical, financial functions provided by a particular language. String Functions
Date Functions
You can add or substract a number to get a new date. There are many other functions available in each language to perform specific tasks like file operations. |
|
Send mail to arungomes@eircom.net with questions or comments about this web site.
|