Introduction To Computer Program: Computer Programming - Quick Guide
Introduction To Computer Program: Computer Programming - Quick Guide
com
89-112 minutes
Sequence of instructions
Computer Programming Language
1. Go straight
2. Drive half kilometer
3. Take left
4. Drive around one kilometer
5. Search for KFC at your right side
Introduction to Computer
Programming
If you understood what a computer program is, then we will
say: the act of writing computer programs is called
computer programming.
Java
C
C++
Python
PHP
Perl
Ruby
Computer Programmer
Someone who can write computer programs or in other
words, someone who can do computer programming is
called a Computer Programmer.
C Programmer
C++ Programmer
Java Programmer
Python Programmer
PHP Programmer
Perl Programmer
Ruby Programmer
Algorithm
From programming point of view, an algorithm is a step-by-
step procedure to resolve any problem. An algorithm is an
effective method expressed as a finite set of well-defined
instructions.
Programming Environment
Basic Syntax
Data Types
Variables
Keywords
Basic Operators
Decision Making
Loops
Numbers
Characters
Arrays
Strings
Functions
File I/O
C Programming
Java Programming
Python Programming
Text Editor
A text editor is a software that is used to write computer
programs. Your Windows machine must have a Notepad,
which can be used to type programs. You can launch it by
following these steps −
Compiler?
You write your computer program using your favorite
programming language and save it in a text file called the
program file.
Interpreter
We just discussed about compilers and the compilation
process. Compilers are required in case you are going to
write your program in a programming language that needs
to be compiled into binary format before its execution.
Online Compilation
If you are not able to set up any editor, compiler, or
interpreter on your machine, then tutorialspoint.com
provides a facility to compile and run almost all the
programs online with an ease of a single click.
Let’s start with a little coding, which will really make you a
computer programmer. We are going to write a single-line
computer program to write Hello, World! on your screen.
Let’s see how it can be written using different programming
languages.
For most of the examples given in this tutorial, you will find
a Try it option in our website code sections at the top right
corner that will take you to the online compiler.
#include <stdio.h>
int main() {
/* printf() function to write Hello,
World! */
printf( "Hello, World!" );
}
Hello, World!
The coding part inside these two curly braces is called the
program body. The left curly brace can be in the same line
as main(){ or in the next line like it has been mentioned in
the above program.
Functions
Functions are small units of programs and they are used to
carry out a specific task. For example, the above program
makes use of two functions: main() and printf(). Here, the
function main() provides the entry point for the program
execution and the other function printf() is being used to
print an information on the computer screen.
Comments
Whitespaces
#include <stdio.h>
int main() {
Hello, World!
#include <stdio.h>\n
\n
int main()\n
{
\n
\t/* printf() function to write Hello,
World! */
\n
\tprintf(\t"Hello, World!"\t);\n
\n
}\n
Semicolons
#include <stdio.h>
int main() {
/* printf() function to write Hello,
World! */
printf( "Hello, World!\n" );
printf( "Hello, World!" );
}
Hello, World!
Hello, World!
#include <stdio.h>
int main() {
/* printf() function to write Hello,
World! */
printf( "Hello, World!" );
printf( "Hello, World!" );
}
Program Explanation
$./demo
Hello, World!
#include <stdio.h>
main() {
printf("Hello, World!")
}
Hello, World!
Hello, World!
Hope you noted that for C and Java examples, first we are
compiling the programs and then executing the produced
binaries, but in Python program, we are directly executing it.
As we explained in the previous chapter, Python is an
interpreted language and it does not need an intermediate
step called compilation.
10 + 20
10.50 + 20.50
Name:
Class:
Section:
Age:
Sex:
Numbers
String
List
Tuple
Dictionary
Creating variables
Creating variables is also called declaring variables in C
programming. Different programming languages have
different ways of creating variables inside a program. For
example, C programming has the following simple way of
creating variables −
#include <stdio.h>
int main() {
int a;
int b;
}
#include <stdio.h>
int main() {
int a, b;
}
Listed below are the key points about variables that you
need to keep in mind −
#include <stdio.h>
int main() {
int a;
int b;
a = 10;
b = 20;
}
#include <stdio.h>
int main() {
int a;
int b;
a = 10;
b = 20;
Value of a = 10
Value of b = 20
int main() {
int a;
int b;
a = 10;
b = 20;
Variables in Java
Following is the equivalent program written in Java
programming language. This program will create two
variables a and b and very similar to C programming, it will
assign 10 and 20 in these variables and finally print the
values of the two variables in two ways −
a = 10;
b = 20;
System.out.println("Value of a = " +
a);
System.out.println("Value of b = " +
b);
System.out.println("Value of a = " +
a + " and value of b = " + b);
}
}
Value of a = 10
Value of b = 20
Value of a = 10 and value of b = 20
Variables in Python
Following is the equivalent program written in Python. This
program will create two variables a and b and at the same
time, assign 10 and 20 in those variables.
Python does not want you to specify the data type at the
time of variable creation and there is no need to create
variables in advance.
a = 10
b = 20
Value of a = 10
Value of b = 20
Value of a = 10 and value of b = 20
You can use the following syntax in C and Java
programming to declare variables and assign values at the
same time −
#include <stdio.h>
int main() {
int a = 10;
int b = 20;
Like int, long, and float, there are many other keywords
supported by C programming language which we will use
for different purpose. Different programming languages
provide different set of reserved keywords, but there is one
important & common rule in all the programming
languages that we cannot use a reserved keyword to name
our variables, which means we cannot name our variable
like int or float rather these keywords can only be used to
specify a variable data type.
For example, if you will try to use any reserved keyword for
the purpose of variable name, then you will get a syntax
error.
#include <stdio.h>
int main() {
int float;
float = 10;
#include <stdio.h>
int main() {
int count;
count = 10;
Arithmetic Operators
Computer programs are widely used for mathematical
calculations. We can write a computer program which can
do simple calculation like adding two numbers (2 + 3) and
we can also write a program, which can solve a complex
equation like P(x) = x4 + 7x3 - 5x + 9. If you have been even
a poor student, you must be aware that in first expression 2
and 3 are operands and + is an operator. Similar concepts
exist in Computer Programming.
P(x) = x4 + 7x3 - 5x + 9.
#include <stdio.h>
int main() {
int a, b, c;
a = 10;
b = 20;
c = a + b;
printf( "Value of c = %d\n", c);
c = a - b;
printf( "Value of c = %d\n", c);
c = a * b;
printf( "Value of c = %d\n", c);
c = b / a;
printf( "Value of c = %d\n", c);
c = b % a;
printf( "Value of c = %d\n", c);
}
Value of c = 30
Value of c = -10
Value of c = 200
Value of c = 2
Value of c = 0
Relational Operators
Consider a situation where we create two variables and
assign them some values as follows −
A = 20
B = 10
(A > B)
Here, we used a symbol > and it is called a relational
operator and in their simplest form, they produce Boolean
results which means the result will be either true or false.
Similarly, a programming language provides various
relational operators. The following table lists down a few of
the important relational operators available in C
programming language. Assume variable A holds 10 and
variable B holds 20, then −
int main() {
int a, b;
a = 10;
b = 20;
a is equal to 10
a is less than b
a is not equal to b
Logical Operators
Logical operators are very important in any programming
language and they help us take decisions based on certain
conditions. Suppose we want to combine the result of two
conditions, then logical AND and OR logical operators help
us in producing the final result.
#include <stdio.h>
int main() {
int a = 1;
int b = 0;
if ( a && b ) {
Operators in Java
Following is the equivalent program written in Java. C
programming and Java provide almost identical set of
operators and conditional statements. This program will
create two variables a and b, very similar to C
programming, then we assign 10 and 20 in these variables
and finally, we will use different arithmetic and relational
operators −
a = 10;
b = 20;
c = a + b;
System.out.println("Value of c = " +
c );
c = a - b;
System.out.println("Value of c = " +
c );
c = a * b;
System.out.println("Value of c = " +
c );
c = b / a;
System.out.println("Value of c = " +
c );
c = b % a;
System.out.println("Value of c = " +
c );
if( a == 10 ) {
System.out.println("a is equal to
10" );
}
}
}
Value of c = 30
Value of c = -10
Value of c = 200
Value of c = 2
Value of c = 0
a is equal to 10
Operators in Python
Following is the equivalent program written in Python. This
program will create two variables a and b and at the same
time, assign 10 and 20 in those variables. Fortunately, C
programming and Python programming languages provide
almost identical set of operators. This program will create
two variables a and b, very similar to C programming, then
we assign 10 and 20 in these variables and finally, we will
use different arithmetic and relational operators.
a = 10
b = 20
c = a + b
print "Value of c = ", c
c = a - b
print "Value of c = ", c
c = a * b
print "Value of c = ", c
c = a / b
print "Value of c = ", c
c = a % b
print "Value of c = ", c
if( a == 10 ):
print "a is equal to 10"
When the above program is executed, it produces the
following result −
Value of c = 30
Value of c = -10
Value of c = 200
Value of c = 0
Value of c = 10
a is equal to 10
#include <stdio.h>
int main() {
int x = 45;
Student is average
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) {
#include <stdio.h>
int main() {
int x = 45;
While using if, else if, else statements, there are a few
points to keep in mind −
if(boolean_expression 1) {
#include <stdio.h>
int main() {
int x = 45;
Student is average
switch(expression){
case ONE :
statement(s);
break;
case TWO:
statement(s);
break;
......
default :
statement(s);
}
int main() {
int x = 2;
switch( x ){
case 1 :
printf( "One\n");
break;
case 2 :
printf( "Two\n");
break;
case 3 :
printf( "Three\n");
break;
case 4 :
printf( "Four\n");
break;
default :
printf( "None of the above...\n");
}
}
Two
Decisions in Java
Following is the equivalent program written in Java which
too supports if, if...else, if...elseif...else, and switch
statements.
Student is average
Decisions in Python
Following is the equivalent program written in Python.
Python provides if, if...else, if...elif...else, and switch
statements. Here, you must note that Python does not
make use of curly braces for conditional body, instead it
simply identifies the body of the block using indentation of
the statements.
x = 45
if x > 95:
print "Student is brilliant"
elif x < 30:
print "Student is poor"
elif x < 95 and x > 30:
print "Student is average"
Student is average
The end
#include <stdio.h>
int main() {
printf( "Hello, World!\n");
printf( "Hello, World!\n");
printf( "Hello, World!\n");
printf( "Hello, World!\n");
printf( "Hello, World!\n");
}
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
#include <stdio.h>
int main() {
int i = 0;
while ( i < 5 ) {
printf( "Hello, World!\n");
i = i + 1;
}
}
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
If the body of a while loop has just one line, then its
optional to use curly braces {...}.
do {
/*....do...while loop body ....*/
}
while ( condition );
#include <stdio.h>
int main() {
int i = 0;
do {
printf( "Hello, World!\n");
i = i + 1;
}
while ( i < 5 );
}
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
break;
#include <stdio.h>
int main() {
int i = 0;
do {
printf( "Hello, World!\n");
i = i + 1;
if( i == 3 ) {
break;
}
}
while ( i < 5 );
}
Hello, World!
Hello, World!
Hello, World!
continue;
#include <stdio.h>
int main() {
int i = 0;
do {
if( i == 3 ) {
i = i + 1;
continue;
}
printf( "Hello, World!\n");
i = i + 1;
}
while ( i < 5 );
}
Loops in Java
Following is the equivalent program written in Java that too
supports while and do...while loops. The following program
prints Hello, World! five times as we did in the case of C
Programming −
while ( i < 5 ) {
System.out.println("Hello,
World!");
i = i + 1;
}
}
}
Loops in Python
Following is the equivalent program written in Python.
Python too supports while and do...while loops. The
following program prints Hello, World! five times as we did
in case of C Programming. Here you must note that Python
does not make use of curly braces for the loop body,
instead it simply identifies the body of the loop using
indentation of the statements.
i = 0
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Loop ends
#include <stdio.h>
int main() {
short s;
int i;
long l;
float f;
double d;
s = 10;
i = 1000;
l = 1000000;
f = 230.47;
d = 30949.374;
s: 10
i: 1000
l: 1000000
f: 230.470
d: 30949.374
double sin(double);
double tan(double);
double sqrt(double);
int abs(int);
double fabs(double);
double floor(double);
#include <stdio.h>
#include <math.h>
int main() {
short s;
int i;
long l;
float f;
double d;
sin(s): -0.544021
abs(i): -0.544021
floor(f): 230.000000
sqrt(l): 1000.000000
pow(d, 2): 5.635876
Numbers in Java
Following is the equivalent program written in Java. Java
provides almost all the numeric data types available in C
programming.
You can try to execute the following program to see the
output, which is identical to the result generated by the
above C example.
s = 10;
i = 1000;
l = 1000000L;
f = 230.47f;
d = 30949.374;
s: 10
i: 1000
l: 1000000
f: 230.470001
d: 30949.374000
Numbers in Python
Python is a little different from C and Java; it categorizes
numbers in int, long, float and complex. Here are some
examples of numbers in Python −
s = 10
i = 1000
l = 1000000
f = 230.47
d = 30949.374
s: 10
i: 1000
l: 1000000
f: 230.47
d: 30949.374
char ch = 'a';
char ch = '1';
#include <stdio.h>
int main() {
char ch1;
char ch2;
char ch3;
char ch4;
ch1 = 'a';
ch2 = '1';
ch3 = '$';
ch4 = '+';
ch1: a
ch2: 1
ch3: $
ch4: +
Escape Sequences
Many programming languages support a concept called
Escape Sequence. When a character is preceded by a
backslash (\), it is called an escape sequence and it has a
special meaning to the compiler. For example, \n in the
following statement is a valid character and it is called a
new line character −
char ch = '\n';
char ch = '\1';
Escape
Description
Sequence
\t Inserts a tab in the text at this point.
Inserts a backspace in the text at this
\b
point.
\n Inserts a newline in the text at this point.
Inserts a carriage return in the text at this
\r
point.
\f Inserts a form feed in the text at this point.
Inserts a single quote character in the text
\'
at this point.
Inserts a double quote character in the
\"
text at this point.
Inserts a backslash character in the text at
\\
this point.
The following example shows how the compiler interprets
an escape sequence in a print statement −
#include <stdio.h>
int main() {
char ch1;
char ch2;
char ch3;
char ch4;
ch1 = '\t';
ch2 = '\n';
Characters in Java
Following is the equivalent program written in Java. Java
handles character data types much in the same way as we
have seen in C programming. However, Java provides
additional support for character manipulation.
ch1 = 'a';
ch2 = '1';
ch3 = '$';
ch4 = '+';
ch1: a
ch2: 1
ch3: $
ch4: +
Characters in Python
Python does not support any character data type but all the
characters are treated as string, which is a sequence of
characters. We will study strings in a separate chapter. You
do not need to have any special arrangement while using a
single character in Python.
ch1 = 'a';
ch2 = '1';
ch3 = '$';
ch4 = '+';
ch1: a
ch2: 1
ch3: $
ch4: +
#include <stdio.h>
int main() {
int number1;
int number2;
int number3;
int number4;
int number5;
number1 = 10;
number2 = 20;
number3 = 30;
number4 = 40;
number5 = 50;
Create Arrays
To create an array variable in C, a programmer specifies the
type of the elements and the number of elements to be
stored in that array. Given below is a simple syntax to
create an array in C programming −
int number[10];
Here, number is a variable array, which is sufficient to hold
up to 10 integer numbers.
Initializing Arrays
You can initialize an array in C either one by one or using a
single statement as follows −
If you omit the size of the array, an array just big enough to
hold the initialization is created. Therefore, if you write −
You will create exactly the same array as you did in the
previous example. Following is an example to assign a
single element of the array −
number[4] = 50;
The above statement will take the 10th element from the
array and assign the value to var variable. The following
example uses all the above-mentioned three concepts viz.
creation, assignment, and accessing arrays −
#include <stdio.h>
int main () {
int number[10]; /* number is an array of
10 integers */
int i = 0;
printf("number[%d] = %d\n", i,
number[i] );
i = i + 1;
}
return 0;
}
number[0] = 100
number[1] = 101
number[2] = 102
number[3] = 103
number[4] = 104
number[5] = 105
number[6] = 106
number[7] = 107
number[8] = 108
number[9] = 109
Arrays in Java
Following is the equivalent program written in Java. Java
supports arrays, but there is a little difference in the way
they are created in Java using the new operator.
while( i < 10 ) {
number[ i ] = i + 100;
i = i + 1;
}
i = 0;
while( i < 10 ) {
System.out.format( "number[%d] =
%d\n", i, number[i] );
i = i + 1;
}
}
}
number[0] = 100
number[1] = 101
number[2] = 102
number[3] = 103
number[4] = 104
number[5] = 105
number[6] = 106
number[7] = 107
number[8] = 108
number[9] = 109
i = 0
while i < 10:
# Accessing elements from the list
print "number[", i, "] = ", number[ i ]
i = i + 1
number[ 0 ] = 100
number[ 1 ] = 101
number[ 2 ] = 102
number[ 3 ] = 103
number[ 4 ] = 104
number[ 5 ] = 105
number[ 6 ] = 106
number[ 7 ] = 107
number[ 8 ] = 108
number[ 9 ] = 109
#include <stdio.h>
main() {
int number[5] = {10, 20, 30, 40, 50};
int i = 0;
while( i < 5 ) {
printf("number[%d] = %d\n", i,
number[i] );
i = i + 1;
}
}
number[0] = 10
number[1] = 20
number[2] = 30
number[3] = 40
number[4] = 50
#include <stdio.h>
main() {
char ch[5] = {'H', 'e', 'l', 'l', 'o'};
int i = 0;
while( i < 5 ) {
printf("ch[%d] = %c\n", i, ch[i] );
i = i + 1;
}
}
ch[0] = H
ch[1] = e
ch[2] = l
ch[3] = l
ch[4] = o
If you are done with the above example, then I think you
understood how strings work in C programming, because
strings in C are represented as arrays of characters. C
programming simplified the assignment and printing of
strings. Let's check the same example once again with a
simplified syntax −
#include <stdio.h>
main() {
char ch[5] = "Hello";
int i = 0;
String = Hello
ch[0] = H
ch[1] = e
ch[2] = l
ch[3] = l
ch[4] = o
Strings in Java
Though you can use character arrays to store strings, but
Java is an advanced programming language and its
designers tried to provide additional functionality. Java
provides strings as a built-in data type like any other data
type. It means you can define strings directly instead of
defining them as array of characters.
String = Hello
Strings in Python
Creating strings in Python is as simple as assigning a
string into a Python variable using single or double quotes.
var1[0]: H
var2[1:5]: ytho
#include <stdio.h>
int main() {
int set1[5] = {10, 20, 30, 40, 50};
int set2[5] = {101, 201, 301, 401, 501};
int i, max;
Defining a Function
The general form of a function definition in C programming
language is as follows −
return [expression];
}
Calling a Function
While creating a C function, you give a definition of what
the function has to do. To use a function, you will have to
call that function to perform a defined task.
#include <stdio.h>
max = set[0];
i = 1;
while( i < 5 ) {
if( max < set[i] ) {
max = set[i];
}
i = i + 1;
}
return max;
}
main() {
int set1[5] = {10, 20, 30, 40, 50};
int set2[5] = {101, 201, 301, 401, 501};
int max;
Functions in Java
If you are clear about functions in C programming, then it is
easy to understand them in Java as well. Java
programming names them as methods, but the rest of the
concepts remain more or less same.
while( i < 5 ) {
if( max < set[i] ) {
max = set[i];
}
i = i + 1;
}
return max;
}
}
Functions in Python
Once again, if you know the concept of functions in C and
Java programming, then Python is not much different.
Given below is the basic syntax of defining a function in
Python −
return [expression]
while( i < 5 ):
if( max < set[i] ):
max = set[i]
i = i + 1
return max
Computer Files
A computer file is used to store data in digital format like
plain text, image data, or any other content. Computer files
can be organized inside different directories. Files are used
to keep digital data, whereas directories are used to keep
files.
File input means data that is written into a file and file
output means data that is read from a file. Actually, input
and output terms are more related to screen input and
output. When we display a result on the screen, it is called
output. Similarly, if we provide some input to our program
from the command prompt, then it is called input.
Opening Files
You can use the fopen() function to create a new file or to
open an existing file. This call will initialize an object of the
type FILE, which contains all the information necessary to
control the stream. Here is the prototype, i.e., signature of
this function call −
r+
4
Opens a text file for reading and writing both.
w+
a+
Closing a File
To close a file, use the fclose( ) function. The prototype of
this function is −
Writing a File
Given below is the simplest function to write individual
characters to a stream −
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("/tmp/test.txt", "w+");
fprintf(fp, "This is testing for
fprintf...\n");
fputs("This is testing for fputs...\n",
fp);
fclose(fp);
}
Reading a File
Given below is the simplest function to read a text file
character by character −
#include <stdio.h>
main() {
FILE *fp;
char buff[255];
fp = fopen("/tmp/test.txt", "r");
fscanf(fp, "%s", buff);
printf("1 : %s\n", buff );
1 : This
2 : is testing for fprintf...
import java.io.*;
// Create a File
file.createNewFile();