You are on page 1of 12

Computer Application in Chemistry Department

Programming language:
A programming language is a computer language programmers use to develop applications, scripts, or other set of
instructions for a computer to execute. Programming language is the intermediate language with programmer to system
(Normal High label language (syntactically English language) to binary language).
Example of Programming language:
1.
2.
3.
4.
5.

C
C++
Java
Fortran
Visual Basic

Procedural oriented programming:


In procedural programming our code is organized into small "procedures" that use and change our data. In ColdFusion, we
write our procedures as either custom tags or functions. These functions typically take some input, do something, then
produce some output. Ideally your functions would behave as "black boxes" where input data goes in and output data
comes out.
The key idea here is that our functions have no intrinsic relationship with the data they operate on. As long as you provide
the correct number and type of arguments, the function will do its work and faithfully return its output.
Sometimes our functions need to access data that is not provided as a parameter, i.e., we need access data that is outside
the function. Data accessed in this way is considered "global" or "shared" data.

So in a procedural system our functions use data they are "given" (as parameters) but also directly access any shared
data they need.
Example: C , Cobol, Basic etc.

Object oriented programming:


In object oriented programming, the data and related functions are bundled together into an "object". Ideally, the data
inside an object can only be manipulated by calling the object's functions. This means that your data is locked away
inside your objects and your functions provide the only means of doing something with that data. In a well designed
object oriented system objects never access shared or global data, they are only permitted to use the data they have, or
data they are given.

Example: C++, Java, C# etc.


C Programming language:
The C programming language is also a high-level programming language developed by Dennis Ritchie and Brian
Kernighan at Bell Labs in 1972 from an almost unknown language named B. The first major program written in C was the
UNIX operating system, and for many years, C was considered to be inextricably linked with Unix. Below is an example of
a C program that prints Hello World! after it's been compiled. If you need a free C compiler consider GCC.
Example :
#include <stdio.h>
void main()
{
printf ("Hello World!\n");
}
Fortran Programming language:
Short for Formula Translator, FORTRAN was one of the first programming languages. FORTRAN began being created in
1954 by members of the IBM team, was first introduced to the public October 15, 1956 and is still used today. Below is a
brief history of FORTRAN.

1954 FORTRAN ,1958 - FORTRAN II, 1958 - FORTRAN III (Never released to public) ,1961- FORTRAN
IV, 1972 - FORTRAN 66, 1980 - FORTRAN 77, 1991 - FORTRAN 90,1997 - FORTRAN 95
Example:
program hello
print *, 'Hello World!'
end program hello

Basic feature of all Programming language:


1.
2.
3.
4.
5.
6.
7.
8.

Variable
Operator
Decision making / Condition
Loops
Function
Array
File Handling
Structure

These are basic feature of All kind of programming.

1. Variable
A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C
has a specific type, which determines the size and layout of the variable's memory; the range of values that can
be stored within that memory; and the set of operations that can be applied to the variable.
The name of a variable can be composed of letters, digits, and the underscore character. It must begin with
either a letter or an underscore. Upper and lowercase letters are distinct because C is case-sensitive. Based on
the basic types explained in previous chapter, there will be the following basic variable types:

Type
char
int
float
double
void

Description
Typically a single octet(one byte). This is an integer type.
The most natural size of integer for the machine.
A single-precision floating point value.
A double-precision floating point value.
Represents the absence of type.

Example:
#include <stdio.h>
// Variable declaration:
extern int a, b;
extern int c;
extern float f;
int main ()
{
/* variable definition: */
int a, b;
int c;
float f;
/* actual initialization */
a = 10;
b = 20;
c = a + b;
printf("value of c : %d \n", c);
f = 70.0/3.0;
printf("value of f : %f \n", f);
return 0;
}

Ans:
value of c : 30
value of f : 23.333334

2. Operator:

An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations. C language is rich in built-in operators and provides the following types of operators:

Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc Operators

Arithmetic Operators

Operator
+
*
/
%
++
--

==
!=
>
<
>=
<=

||
!

Description
Checks if the values of two operands are equal or not, if yes
then condition becomes true.
Checks if the values of two operands are equal or not, if
values are not equal then condition becomes true.
Checks if the value of left operand is greater than the value
of right operand, if yes then condition becomes true.
Checks if the value of left operand is less than the value of
right operand, if yes then condition becomes true.
Checks if the value of left operand is greater than or equal to
the value of right operand, if yes then condition becomes
true.
Checks if the value of left operand is less than or equal to the
value of right operand, if yes then condition becomes true.

Example
(A == B) is not true.
(A != B) is true.
(A > B) is not true.
(A < B) is true.
(A >= B) is not true.
(A <= B) is tr

Logical Operators

Operator
&&

Example
Adds two operands
A + B will give 30
Subtracts second operand from the first
A - B will give -10
Multiplies both operands
A * B will give 200
Divides numerator by de-numerator
B / A will give 2
Modulus Operator and remainder of after an integer division B % A will give 0
Increments operator increases integer value by one
A++ will give 11
Decrements operator decreases integer value by one
A-- will give 9

Relational Operators

Operator

Description

Description
Example
Called Logical AND operator. If both the operands are non(A && B) is false.
zero, then condition becomes true.
Called Logical OR Operator. If any of the two operands is
(A || B) is true.
non-zero, then condition becomes true.
Called Logical NOT Operator. Use to reverses the logical
state of its operand. If a condition is true then Logical NOT !(A && B) is true.
operator will make false.

Bitwise Operators

Operator
&
|
^
~
<<
>>

Assignment Operators

Operator
=
+=
-=

*=
/=
%=
<<=
>>=
&=
^=
|=

Description
Example
Binary AND Operator copies a bit to the result if it exists in (A & B) will give 12, which is 0000
both operands.
1100
(A | B) will give 61, which is 0011
Binary OR Operator copies a bit if it exists in either operand.
1101
Binary XOR Operator copies the bit if it is set in one
(A ^ B) will give 49, which is 0011
operand but not both.
0001
Binary Ones Complement Operator is unary and has the
(~A ) will give -61, which is 1100
effect of 'flipping' bits.
0011 in 2's complement form.
Binary Left Shift Operator. The left operands value is moved A << 2 will give 240 which is 1111
left by the number of bits specified by the right operand.
0000
Binary Right Shift Operator. The left operands value is
A >> 2 will give 15 which is 0000
moved right by the number of bits specified by the right
1111
operand.

Description
Simple assignment operator, Assigns values from right side
operands to left side operand
Add AND assignment operator, It adds right operand to the
left operand and assign the result to left operand
Subtract AND assignment operator, It subtracts right
operand from the left operand and assign the result to left
operand
Multiply AND assignment operator, It multiplies right
operand with the left operand and assign the result to left
operand
Divide AND assignment operator, It divides left operand
with the right operand and assign the result to left operand
Modulus AND assignment operator, It takes modulus using
two operands and assign the result to left operand
Left shift AND assignment operator
Right shift AND assignment operator
Bitwise AND assignment operator
bitwise exclusive OR and assignment operator
bitwise inclusive OR and assignment operator

Example
C = A + B will assign value of A + B
into C
C += A is equivalent to C = C + A
C -= A is equivalent to C = C - A

C *= A is equivalent to C = C * A
C /= A is equivalent to C = C / A
C %= A is equivalent to C = C % A
C <<= 2 is same as C = C << 2
C >>= 2 is same as C = C >> 2
C &= 2 is same as C = C & 2
C ^= 2 is same as C = C ^ 2
C |= 2 is same as C = C | 2

Misc Operators

Operator

Description

sizeof()

Returns the size of an variable.

&

Returns the address of an variable.

Pointer to a variable.

?:

Conditional Expression

Example
sizeof(a), where a is integer, will
return 4.
&a; will give actual address of the
variable.
*a; will pointer to a variable.
If Condition is true ? Then value X :
Otherwise value Y

3. Decision making / Condition


Decision making structures require that the programmer specify one or more conditions to be evaluated or
tested by the program, along with a statement or statements to be executed if the condition is determined to be
true, and optionally, other statements to be executed if the condition is determined to be false.
In programming language provides following types of decision making statements.
Statement

Description

if statement

An if statement consists of a boolean expression followed by one or more


statements.

if...else statement

An if statement can be followed by an optional else statement, which


executes when the boolean expression is false.

nested if statements

You can use one if or else if statement inside another if or else if


statement(s).

switch statement

A switch statement allows a variable to be tested for equality against a list of


values.

nested switch statements

You can use one switch statement inside another switch statement(s).

if statement
An if statement consists of a boolean expression followed by one or more statements.

The syntax of an if statement in C programming language is:


if(boolean_expression)
{
/* statement(s) will execute if the boolean expression is true */
}

Example:
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;
/* check the boolean condition using if statement */
if( a < 20 )
{
/* if condition is true then print the following */
printf("a is less than 20\n" );
}
printf("value of a is : %d\n", a);
return 0;
}

Ans:
a is less than 20;
value of a is : 10

if...else statement
An if statement can be followed by an optional else statement, which executes when the boolean expression is false.

The syntax of an if...else statement in C programming language is:


if(boolean_expression)
{
/* statement(s) will execute if the boolean expression is true */
}
else
{
/* statement(s) will execute if the boolean expression is false */
}

Example:
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 100;
/* check the boolean condition */
if( a < 20 )
{
/* if condition is true then print the following */
printf("a is less than 20\n" );
}
else
{
/* if condition is false then print the following */
printf("a is not less than 20\n" );
}
printf("value of a is : %d\n", a);
return 0;
}
Ans: a is not less than 20;
value of a is : 100

nested if statements
An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions
using single if...else if statement.
When using if , else if , else statements there are few points to keep in mind:

An if can have zero or one else's and it must come after any else if's.
An if can have zero to many else if's and they must come before the else.
Once an else if succeeds, none of the remaining else if's or else's will be tested.

The syntax of an if...else if...else statement in C programming language is:


if(boolean_expression 1)
{
/* Executes when the boolean
}
else if( boolean_expression 2)
{
/* Executes when the boolean
}
else if( boolean_expression 3)
{
/* Executes when the boolean
}
else
{
/* executes when the none of
}

expression 1 is true */

expression 2 is true */

expression 3 is true */

the above condition is true */

Example:
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 100;
/* check the boolean condition */
if( a == 10 )
{
/* if condition is true then print the following */
printf("Value of a is 10\n" );
}
else if( a == 20 )
{
/* if else if condition is true */
printf("Value of a is 20\n" );
}
else if( a == 30 )
{
/* if else if condition is true */
printf("Value of a is 30\n" );
}
else
{
/* if none of the conditions is true */
printf("None of the values is matching\n" );

}
printf("Exact value of a is: %d\n", a );
return 0;
}

switch statement
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the
variable being switched on is checked for each switch case.

The syntax for a switch statement in C programming language is as follows:


switch(expression){
case constant-expression
statement(s);
break; /* optional */
case constant-expression
statement(s);
break; /* optional */

/* you can have any number of case statements */


default : /* Optional */
statement(s);
}

The following rules apply to a switch statement:

The expression used in a switch statement must have an integral or enumerated type, or be of a class type in
which the class has a single conversion function to an integral or enumerated type.
You can have any number of case statements within a switch. Each case is followed by the value to be compared
to and a colon.
The constant-expression for a case must be the same data type as the variable in the switch, and it must be a
constant or a literal.
When the variable being switched on is equal to a case, the statements following that case will execute until a
break statement is reached.
When a break statement is reached, the switch terminates, and the flow of control jumps to the next line
following the switch statement.
Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent
cases until a break is reached.
A switch statement can have an optional default case, which must appear at the end of the switch. The default
case can be used for performing a task when none of the cases is true. No break is needed in the default case.

Example:
#include <stdio.h>
int main ()
{
/* local variable definition */
char grade = 'B';
switch(grade)
{
case 'A' :
printf("Excellent!\n" );
break;
case 'B' :
case 'C' :
printf("Well done\n" );
break;
case 'D' :
printf("You passed\n" );
break;
case 'F' :
printf("Better try again\n" );
break;
default :
printf("Invalid grade\n" );
}
printf("Your grade is %c\n", grade );
return 0;
}
Ans: Well done
Your grade is B

You might also like