Saturday, November 10, 2018

Conditional statement
It means "if" condition is true then do the work(under if block) otherwise(else) don't do it.This is called if else statement or conditional statement.
If condition is met, work will be done, else not.
Written as:

If(condition)
{block of statements }
else
{block of statements }
This is called conditional programming.
Eg. X=1000,Y=100Find the greater number. Here I hv taken easy numbers but it can also be used for big numbers Eg for income tax purposes.
If(X>Y)
{document. write("x is greater") ;}
Else
{document.write("y is greater") ;}




Program:
<html>
<head></head>
<body>
<SCRIPT LANGUAGE="JavaScript" >

int x=1000, y=100;
If(X>Y)
{document. write("x is greater") ;}
Else
{document.write("y is greater") ;}
</script>
</body>
</html>
Save the file as greatest. html
Double click file for opening it. 

Variables and Data Types

Variable
Some memory is allocated to store a particular type of data and that portion of memory will be referred to by the variable name.
Data type may be numeric, alphanumeric.
Numeric:that stores numbers.
Alphanumeric:that stores alphabets and numbers.
The general form of declaring variables is :
data type (variable name )

Eg.
Step1.  Start
Step2.  X=10
Step3. Y=5
Step4. M=0
Step5. M=X+Y+(X*Y)
Step6. Y=Y+4
Step7. M=M+Y
Step8. Display X,Y,M
Step9. End

Dry Run
                             X.         Y.           M
Initial values.     10.      5.             0
After step5.          10.      5.             65
After step6.          10.       9.             65
After step7.          10.       9.              74

Looping - Variable Loop

Variable Loop
Eg.
A survey is carried out in a town. Information such as name, sex, age etc of each person is available. To maintain a list of people aged 50 and above in the town you draw a Flowchart as follows:


Figure illustrates a variable loop, as the exact number of people in town is not known. This loop continues till the name and age of the last person in the town is read. 
Loops are fixed if operations are repeated a fixed number of times  The values being computed or handled inside the loop have no effect on the number of times the looping operation is done.
Variable loops are the ones where the operations are repeated until a specified condition is met. The number of times the loop is repeated may vary. 


Looping and Fixed Loop

Looping refers to performing a set of operations repeatedly.
Loops can be of 2 types:
-Fixed Loop:The number of times a loop gets executed is fixed.
-Variable Loop:The number of times a loop gets executed is not known.

Fixed Loop
Eg.

Problem
To calculate the total expenditure (sum of monthly expenditure) incurred during a year the algorithm and Flowchart are as follows :
Algorithm
1.Set a counter variable called no_of_months to 1.This variable will keep track of the number of months for which total expenditure is calculated.
You need to understand that the total expenditure must be calculated for month 1 through 12.
2.Accept monthly expenditure starting from month January from the user.
3.Add the monthly expenditure to a variable called total expenditure. This variable will contain the sum of the expenditure incurred every month. (January through December)
4.Increase 5 value of the counter variable no_of_months by 1.
5.Check if the value of the counter variable no_of_months is less than 13.
If the value of the variable no_of_months is less than 13,then repeat steps 2 through 5.
7.Else(that is the expenditure has been calculated for 12 months) display the value of the variable total_expenditure.







Explanation: 

This is a fixed loop because the total number of months in a year are 12.This process will be terminated when the number of months is more than 12.
In the above example, the expenditure for each month is added to the total expenditure.Then the loop examines whether the number of months is less than 13.If'No',then the loop is terminated and the total expenditure is displayed. If'Yes', the expenditure of the next month is added to the total expenditure and the loop continues. 
Since the number of months in a year is fixed, this loop cannot be executed more than 12 times. Hence, this is an example of fixed loop. 

Friday, November 9, 2018

Counter Variables

A counter variable keeps track of number of times a particular operation has been performed.
If you wish to display the message "hello world" ten times, then you must have some mechanism by which you can count the number of times the message "hello world" is displayed. To achieve this, you might have to use a variable which keeps track of the number of times the message is displayed  This variable is known as counter variable.
Initially the variable will contain a value 1.Each time before displaying the message "hello world", the value of the counter variable has to be checked. If the value is less than 11,then the value contained in the counter variable is increased by 1and hello world is displayed  The moment the value e exceeds 10 the operation is terminated.

Eg.
Problem:to display hello world 10 times.

Algorithm
The algorithm for above problem will be:
1.Start
2.Set value of counter variable to 1.
3.Check if value of counter variable is less than 11.
3.1if value is less than 11
3.1.1increase counter variable by 1.
3.1.2display "hello world".
3.1.3go back to step 3.
3.2If the value is 11,terminate operation.
4.End



Monday, September 24, 2018

Variable naming convention

Programming languages may have different rules for naming variables

  1. The programming language may be case sensitive.Eg. Add,ADD,add are 3 different variables.
  2. The name of the variable may be indicative of the value it holds.Eg.If you are storing sum of 3 numbers then 'sum' variable will be appropriate name.sum=10+20+30;
  3. Every variable must have a unique name in a program.
  4. Variable names must begin with an alphabet.
  5. THe first character of variable must be followed by letters,digits and can also include special characters as 'underscore'.
  6. Avoid using letter O which may be confused with 0.

Saturday, September 22, 2018

Naming of a variable



A=L X B

where
 A=Area,
B=Breadth
and L=Length of rectangle

To display sum of 3 numbers

algorithm statement would be :

Display the sum of 10,20,30

In programming language same thing will be written as :

sum=10+20+30

Internally,the variable sum is stored in the computer's memory.



Friday, September 21, 2018

Can variables be used in low level languages

Earlier machine language or assembly language was used for writing programs.In such languages,if you need to store data,then the exact storage location inside the memory of the computer has to be assigned.This storage location refers to a specific memory address,which is represented in terms of hexadecimal numbers such as 006FF,112ED.
If you have to store 100 different values,then you have to remember the address of 100 different memoryblocations where the values are stored.This becomes tedious and to avoid this high-level languages are used.
High level languages allow you to use symbolic names known as variables,to refer to the memory location where a particular value is to be stored.

Hierarchy from machine language to high level language



A Diagram depicting the working of a programming language in a computer, meaning after we write code on a computer in a programming language following happens inside computer.





Hardware
|
\/
Machine Language
|
\/
Assembly Language
|
\/
High Level  Language
|
\/
Fortran,C,Pascal



Hierarchy from machine language to high level language



Machine Language:
They send instructions in the form of 0's and 1's which only the machine can understand.

Assembly language:
It contains symbolic machine language instructions.These are called mnemonics.It is little bit english.

High Level Language:
It contains a set of insructions which are simple english like,like spoken languages and are easy to understand and use.C,C++,Pascal,Basic are some exaplesof high level languages in which english words are used as instructions.

The figure shows how computer's hardware,machine language,assembly language and high level language are related to each other.

Variables

Variable is a container used for temporary storage of data.

According to wikipedia,

In computer programming, a variable or scalar is a storage location (identified by a memory address).

Why are variables required?

This can be understood by an example:

If there is a number say 1233344588777669900 which has to be used in multiple calculations,then one way would be to key in the number each time it is required.
The second method would be to use M+ key available in calculator.The momemt we press M+ key,the specified number gets stored in the calculator's memory and can be recalled whenever required by pressing the MR key.
The same appliesto data used in a program.The data,which is repeatedly used has to be temporarily stored in a variable so that it can be accessed when required.

How much memory does variable require?

The amount of memory required to store an integer(number eg.6) is 2 bytes and character eg 'a' is 1 byte. 

Memory allocation is done by programming language not by user.

Flowchart symbols




Flowchart symbols
Flowchart symbols:what they mean?


Oval:Start or end of program.

Parallelogram: Input or output operation.

Rectangle:Decision making & branching.

Circle:Joining of 2 parts of program on same page.

Arrows:Flow lines.





Example of lamp:


Introduction-Algorithm

Algorithm is concise list of steps in english language in short sentences required in solving a problem.

Eg:

1. Check if a given number is even or odd

Algorithm :

1.Start
2. Accept the number.
3.Divide the number by 2
4.If the remainder of the division is zero,then the number is even.
5.Otherwise the number is odd.
6. End

2.Name the fruit.

Algorithm:

1. Start
2.Accept the character.
3.Print"enter alphabet a,b or c"
4. If a is entered,print apple.
5If b is entered ,print banana.
6. If c is entered,print cane.
7.End

3.Tell whether it is a leap year or not.

Algorithm:

1.Start.
2.Accept the year.
3.Print"enter any year.If n mod (%)4==0 or n%400==0) where n is the year.
4.Print "Leap year"
5.Otherwise,print "year is not a leap year.".
6.End



Introduction-Program in general

Suppose we have to add :

10+20

We see there is a plus sign between the numbers.We can understand that it is a sum of addition but the computer can't.It has no brains.To make computer understand this statement we will have to give a set of instructions.This set of instructions is called programming.
:

So,we can say:

A program is a set of instructions which,when executed cause the machine to behave in a predetermined manner.


Programs are written to solve problems.But before writing the program,you should solve the problem by yourself somewhere in rough.
When you have solved the program,just write the main steps somewhere in rough.
This is a rough sketch  of your program.
Thus,the method of solving the problem,it's main steps will make the statements of your program.

In short:

1.Studythe problem.
2.Gather information to solve the problem.
3.Write the main steps to solve the particular problem.
4.Check your result with your book.

In a program,this method of solving the particular program will apply to all such problems.