Important Concepts

Programming

Share

In this tutorial we will learn about some important concepts that are used in programming.

  • Semicolon
  • Comment
  • Data Item
  • Variable

Semicolon ;

As in English, every sentence ends with a full stop .
In programming every statement ends with a semicolon ;
Example

x = 10;


Comment

We use comment in our program and pseudo code to make note and for short description.
Example:

//this is a single line comment

/*this is
a multiple line
comment*/


Data Item

In programming we deals with data which are of the following types
Numeric
Integer – integer values (without decimal) like -44 , 0 , 1 etc.
Real – real numbers (with decimal) like 12.015 etc.
Character
anything enclosed within single quote like 'a' in C, C++ etc.
String
multiple characters enclosed within double quote like "Hi" in Java, C, C++ etc.
Boolean
TRUE and FALSE
TRUE is also represented by 1
FALSE is also represented by 0

Variable

To save data we need variables.

Example of Variables

Variable storing Integer data item

score = 65535;

Variable storing Real data item
speed = 230.45;

Variable storing Character data item
ch = ‘q’;

Variable storing String data item
name = “Yusuf Shakeel”;

Variable storing Boolean data item
isGameOver = false;

Constant

A value that will not change throughout the execution of the program is called a constant.
Example:

3.14 value of PI

9.8 value of g (acceleration due to gravity)

Assignment Operator =

It is used to assign value to variables
Example

x = 7;    //This statement assigns 7 to variable x

Mathematical Operators

We can perform math operation in program.
Addition +

Example: x = 1 + 2;  // result 3

Subtraction –
Example: x = 5 – 3;  // result 2

Multiplication *
Example: x = 4 * 5;	// result 20

Division /
Example: x = 4 / 2;	// result 2

Increment Operator ++

It is used to increment the value of a variable by 1
Example:

count = 5;
count++;		//value of count becomes 6

Note! The above statement is equivalent to
count = count + 1;

Decrement Operator --

It is use to decrement the value of a variable by 1
Example:

count = 5;
count--;		//value of count becomes 4

Note! The above statement is equivalent to
count = count - 1;

Post Increment

First use the value then increment.
Example

count = 5;
x = count++; 	//x is assigned the value 5 then count is incremented by 1
Print: x;		//output 5
Print: count;	//output 6

The above code is equivalent to
count = 5;
x = count;			//first x is assigned the value
count = count+1;	//then count is incremented
Print: x;
Print: count;

Pre Increment

First increment then use the value.
Example

count = 5;
x = ++count; 	//count is incremented by 1 then x is assigned the value 6
Print: x;		//output 6
Print: count;	//output 6

The above code is equivalent to
count = 5;
count = count+1;	//first count is incremented
x = count;		//then x is assigned the value
Print: x;
Print: count;

Post Decrement

First use the value then decrement.
Example

count = 5;
x = count--; 	//x is assigned the value 5 then count is decremented by 1
Print: x;	//output 5
Print: count;	//output 4

The above code is equivalent to
count = 5;
x = count;		//first x is assigned the value
count = count-1;	//then count is decremented
Print: x;
Print: count;

Pre Decrement

First decrement then use the value.
Example

count = 5;
x = --count; 	//count is decremented by 1 then x is assigned the value 4
Print: x;	//output 4
Print: count;	//output 4

The above code is equivalent to
count = 5;
count = count-1;	//first count is decremented
x = count;		//then x is assigned the value
Print: x;
Print: count;

Modulus Operator

It is used to get the remainder.
Example

5 % 2 = 1     //dividing 5 by 2 we get remainder 1
4 % 6 = 4     //dividing 4 by 6 we get remainder 4
12 % 12 = 0     //dividing 12 by 12 we get remainder 0

Logical Operator

Logical ADD &&
If both operand are TRUE then output is TRUE
Logical OR ||
If any one operand is TRUE then output is TRUE
Logical NOT !
Complements the value.
Converts TRUE to FALSE and FALSE to TRUE
Converts 1 to 0 and 0 to 1

Logical ADD Operator &&

ABA && B
FALSEFALSEFALSE
FALSETRUEFALSE
TRUEFALSEFALSE
TRUETRUETRUE

ABA && B
000
010
100
111
If both the expressions are TRUE (i.e. 1) then the output is TRUE (i.e. 1)

Logical OR Operator ||

ABA || B
FALSEFALSEFALSE
FALSETRUETRUE
TRUEFALSETRUE
TRUETRUETRUE

ABA || B
000
011
101
111
If any one expression is TRUE (i.e. 1) then the output is TRUE (i.e. 1)

Logical NOT Operator !

AA!
FALSETRUE
TRUEFALSE

AA!
01
10
Converts TRUE (i.e. 1) to FALSE (i.e. 0) and converts FALSE to TRUE

Relational Operator

Is Less than <

Example:  5 < 6	//result TRUE

Is Greater than >
Example:  6 > -9	// result TRUE

Is Less then equal to <=
Example:  0 <= 10	// result TRUE

Is Greater than equal to >=
Example:  0 >= -100	 // result TRUE

Is Equal to ==
Example:  7 == 8	// result FALSE

Is Not equal to !=
Example:  7 != 8	// result TRUE

Array

We generally divide array in three category:

  • One-Dimension array
  • Two-Dimension array
  • Multiple-Dimension array

1D Array

If we have an array by the name arr which has n elements then we’ll represent this array as arr[0:n-1]
Note!
In programming languages like C, C++, Java etc. array index starts from 0.
So for an array with n elements we will have
starting index = 0
and
ending index = n-1
When converting your pseudo code into programming language code take special care of the array index.

2D Array

An array by the name arr with r rows and c columns will be represented as arr[0:r-1][0:c-1]
Note!
In C, we declare 2D array with r rows and c columns as

int arr[r][c];

The row index is from 0 to r-1 and column index is from 0 to c-1

Share

Recently Added