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.