You are on page 1of 42

STRUCTURE PROGRAMMING SHORT NOTE

NSJ ONLINE ACADEMY 1


STRUCTURE PROGRAMMING SHORT NOTE

Computer Programming

• Computer
– Programmable machine which working under a given set of
instruction
– Program
– Set of instructions that is used to control the computer in processin
data
• Programming Language
– Artificial language that can be used to define a sequence
instructions that can ultimately be processed and executed by t
computer

Generations
First generation (1GL)
• Machine language/ Machine code/ Native code
• Microprocessor can process directly without a previous
transformation

NSJ ONLINE ACADEMY 2


STRUCTURE PROGRAMMING SHORT NOTE

• Second generation (2GL)


• Assembly languages
• Used symbols
• Portable across multiple systems
• Assembler
Creates object code by translating assembly
instruction

• Third generation (3GL)
• High Level languages
• 1950-1970
• Use natural language elements
• Fortran
• COBOL
• Algol
• C, C++, C#, Java
• BASIC
• Pascal

NSJ ONLINE ACADEMY 3


STRUCTURE PROGRAMMING SHORT NOTE

Translated to machine code


• Compile
• Interpret

Forth generation (4GL)


• Very High-level languages
• 1970s-1990
• Structured features
• OOP
– Clipper
– FoxPro
– WinDev

Fifth generation (5GL)


• Natural languages
• Giving constraints
• Computer solve a given problem
• Used in artificial intelligence
– Prolog
– OPS5
– Mercury

NSJ ONLINE ACADEMY 4


STRUCTURE PROGRAMMING SHORT NOTE

C++ Programming Language


◎ General-purpose programming language
◎ Developed by Bjarne Stroustrup
◎ in 1979
◎ At Bell Labs
◎ Object oriented

Syntax of the C++


NSJ ONLINE ACADEMY 5
STRUCTURE PROGRAMMING SHORT NOTE

C++ Output Anything

C++ Line Break

NSJ ONLINE ACADEMY 6


STRUCTURE PROGRAMMING SHORT NOTE

C++ Comments

NSJ ONLINE ACADEMY 7


STRUCTURE PROGRAMMING SHORT NOTE

C++ Data Types

Variables
Memory location in the computer memory which can store
data and is given a symbolic name for easy reference

Syntax

type variableName = value;

NSJ ONLINE ACADEMY 8


STRUCTURE PROGRAMMING SHORT NOTE

1)int myNum = 15;


cout << myNum;

2)int myNum;
myNum = 15;
cout << myNum;

int myNum = 15; // myNum is 15


myNum = 10; // Now myNum is 10
cout << myNum; // Outputs 10

int myNum = 5; // Integer (whole number without


decimals)
double myFloatNum = 5.99; // Floating point number (with
decimals)
char myLetter = 'D'; // Character
string myText = "Hello"; // String (text)
bool myBoolean = true; // Boolean (true or false)

C++ Identifiers
NSJ ONLINE ACADEMY 9
STRUCTURE PROGRAMMING SHORT NOTE

Names can contain letters, digits and underscores


Names must begin with a letter or an underscore (_)
Name are Case Sensitive
Names cannot contain whitespaces or special characters like !,
#, %, etc.
Reserved words can not be used a name

C++ Constants

const int myNum = 15; // myNum will always be 15


myNum = 10; // error: assignment of read-only variable
'myNum’

NSJ ONLINE ACADEMY 10


STRUCTURE PROGRAMMING SHORT NOTE

C++ User Inputs

Escape Sequency

Algorithms

NSJ ONLINE ACADEMY 11


STRUCTURE PROGRAMMING SHORT NOTE

Step by step procedure for solving a problem

The characteristics of a good algorithm are:


• Precision – the steps are precisely stated(defined).
• Uniqueness – results of each step are uniquely defined and only depend on the input and the result
preceding steps.
• Finiteness – the algorithm stops after a finite number of instructions are executed.
• Input – the algorithm receives input.
• Output – the algorithm produces output.
• Generality – the algorithm applies to a set of inputs.

• iostream.h
Header Files
– Stands for input output stream
– Invoke the commonly used functions like cout,cin
• stdio.h
– Defines three variable types, several macros, and various functions for performing
input and output.
• conio.h
– Invoke the commonly used functions like clrscr ()
NSJ ONLINE ACADEMY 12
STRUCTURE PROGRAMMING SHORT NOTE

Type Conversion

• Implicite conversion - also called type casting


• known as promotion

double a;
int b = 5;
float c = 8.5;
a = b * c;

Explicit conversion known as type-casting

double x = 10.3;
int y;
y = int (x); // functional notation
y = (int) x;

NSJ ONLINE ACADEMY 13


STRUCTURE PROGRAMMING SHORT NOTE

Operators
Arithmatic Operators

Assignment Operators

NSJ ONLINE ACADEMY 14


STRUCTURE PROGRAMMING SHORT NOTE

Relational /Comparison
Operators

NSJ ONLINE ACADEMY 15


STRUCTURE PROGRAMMING SHORT NOTE

Logical Operators

Conditions

If Condition

NSJ ONLINE ACADEMY 16


STRUCTURE PROGRAMMING SHORT NOTE

If Condition Syntax

if(condition)
{
statement(s);
}
else
}
statement(s); NSJ ONLINE ACADEMY 17
✦ int time = 20;
STRUCTURE PROGRAMMING SHORT NOTE
if (time < 18) {
cout << "Good day.";
} else {
cout << "Good evening.";
}
// Outputs "Good evening."

Switch Syntax
switch (int or char variable)

case value:

statements;

break;

case value :

statements;

break;

case value :

statements;

break;

default;

statements;

NSJ ONLINE ACADEMY 18


STRUCTURE PROGRAMMING SHORT NOTE

char Grade='A';
switch (Grade)
{
case 'A':
cout<<"Excellent";
break;
case 'S' :
cout<<"Simple pass";
break;
case 'F' :
cout<<"fail";
break;
default:
cout<<"invalid input";
}

Difference in If and Switch

NSJ ONLINE ACADEMY 19


STRUCTURE PROGRAMMING SHORT NOTE

While

Syntax :

while(condition)
{
statement(s);
}

NSJ ONLINE ACADEMY 20


STRUCTURE PROGRAMMING SHORT NOTE

int i=0;
while(i<10)
{
i++;
cout<<i<<endl;
}
Do While

NSJ ONLINE ACADEMY 21


STRUCTURE PROGRAMMING SHORT NOTE

do
{
statements;
} while (condition);

int i=0;
do
{ i++;
cout<<i<<endl;
}while(i<10);

While in contrast to do…while

NSJ ONLINE ACADEMY 22


STRUCTURE PROGRAMMING SHORT NOTE

For Loop

for (initialization; decision; increment/decrement)


{
statement(s);
}

NSJ ONLINE ACADEMY 23


STRUCTURE PROGRAMMING SHORT NOTE

for (int i=0; i<10; i++)


{
cout<<i<<endl;
}
for (int i=10; i>10; i--)
{
cout<<i<<endl;
}
For Special Patterns –Watch Rapid Revision Video Series

NSJ ONLINE ACADEMY 24


STRUCTURE PROGRAMMING SHORT NOTE

Break
int i=0;
while (i<10)
{
if (i==5)
{
break;
}
cout <<i<<endl;
i++;
}

Continue
int i=0;
while (i<10)
{
if (i==5)
{
continue;
}
cout <<i<<endl;
i++;
NSJ ONLINE ACADEMY 25
STRUCTURE PROGRAMMING SHORT NOTE

C++ Arrays

An array is a series of elements of the same type placed in


contiguous memory locations that can be individually
referenced by adding an index to a unique identifier.

int i=0;
int num[5];
num[0]=11;
int A[5] = {11,2,23,4,15};
num[1]=12;
int B[] = {6,7,8,9,15,12};
num[2]=13;
int c[5]={};
num[3]=14;
num[4]=15;
while(i<5)
{
cout<<num[i]<<endl;
i++; NSJ ONLINE ACADEMY 26

}
STRUCTURE PROGRAMMING SHORT NOTE

char names[]={'N','i','m','a','l'};

Two-Dimensional Array

int i=0;
int sales[3][5];
sales[0][0]=11;
sales[0][1]=12;
sales[0][2]=13;
sales[0][3]=14;
sales[0][4]=15;
sales[1][0]=21;
sales[1][1]=22;
sales[1][2]=23;
sales[1][3]=24;
sales[1][4]=25;
sales[2][0]=31;
sales[2][1]=32;
sales[2][2]=33;
sales[2][3]=34;
sales[2][4]=35;
NSJ ONLINE ACADEMY 27
STRUCTURE PROGRAMMING SHORT NOTE

int i=0,j=0;
int A[4][3] = {{22, 23, 10},{15, 25, 13},{20, 74, 67},{11, 18, 14}};
while(j<3)
{
cout<<A[0][j]<<" ";
j++;
}

Advantages
Same name for similar set of objects
Useful with working in sequence of the same kind of data.
Data accessing is faster
2D arrays are used to represent matrices.
Used to implement other data structures like linked lists, stacks,
queues, trees, graphs

Disadvantages
Sometimes it's not easy to operate with indexes
C environment doesn't have checking mechanism for array
sizes/fixed size

NSJ ONLINE ACADEMY 28


STRUCTURE PROGRAMMING SHORT NOTE

Pointers

string food = "Pizza"; // A food variable of type string

cout << food; // Outputs the value of food (Pizza)


cout << &food; // Outputs the memory address of food
(0x6dfed4)

string food = "Pizza"; // A food variable of type string


string* ptr = &food; // A pointer variable, with the
name ptr, that stores the address of food

// Output the value of food (Pizza)


cout << food << "\n";

// Output the memory address of food (0x6dfed4)


cout << &food << "\n";

// Output the memory address of food with the pointer


(0x6dfed4)
cout << ptr << "\n";

string food = "Pizza"; // Variable declaration


string* ptr = &food; // Pointer declaration

// Reference: Output the memory address of food with the pointer


(0x6dfed4)
cout << ptr << "\n";

// Dereference: Output the value of food with the pointer (Pizza)


cout << *ptr << "\n";
NSJ ONLINE ACADEMY 29
STRUCTURE PROGRAMMING SHORT NOTE

string food = "Pizza";


string* ptr = &food;

// Output the value of food (Pizza)


cout << food << "\n";

// Output the memory address of food (0x6dfed4)


cout << &food << "\n";

// Access the memory address of food and output its value


(Pizza)
cout << *ptr << "\n";

// Change the value of the pointer


*ptr = "Hamburger";

// Output the new value of the pointer (Hamburger)


cout << *ptr << "\n";

// Output the new value of the food variable (Hamburger)


cout << food << "\n";

Functions

NSJ ONLINE ACADEMY 30


STRUCTURE PROGRAMMING SHORT NOTE

NSJ ONLINE ACADEMY 31


STRUCTURE PROGRAMMING SHORT NOTE

01st System

// Create a function
void myFunction() {
cout << "I just got executed!";
}

int main() {
myFunction(); // call the function
return 0;
}

// Outputs "I just got executed!"

NSJ ONLINE ACADEMY 32


STRUCTURE PROGRAMMING SHORT NOTE

02nd System

void myFunction(string fname) {


cout << fname << " Refsnes\n";
}

int main() {
myFunction("Liam");
myFunction("Jenny");
myFunction("Anja");
return 0;
}

// Liam Refsnes
// Jenny Refsnes
// Anja Refsnes

03rd System

int myFunction() {
return 5 ;
}

int main() {
cout << myFunction();
return 0;
}

// Outputs 5

NSJ ONLINE ACADEMY 33


STRUCTURE PROGRAMMING SHORT NOTE

04th System

int myFunction(int x, int y) {


return x + y;
}

int main() {
cout << myFunction(5, 3);
return 0;
}

// Outputs 8 (5 + 3)

Structures

Structure is a user-defined data type


in C, which is used to store a
collection of different kinds of data.

✦ struct structureName
✦ {
✦ //member
definitions
✦ }; NSJ ONLINE ACADEMY 34
STRUCTURE PROGRAMMING SHORT NOTE

struct Subjects
{
char S_code[20];
char S_name[50];
int Hours;
};

NSJ ONLINE ACADEMY 35


STRUCTURE PROGRAMMING SHORT NOTE

Flow Charts

NSJ ONLINE ACADEMY 36


STRUCTURE PROGRAMMING SHORT NOTE

NSJ ONLINE ACADEMY 37


STRUCTURE PROGRAMMING SHORT NOTE

NSJ ONLINE ACADEMY 38


STRUCTURE PROGRAMMING SHORT NOTE

NSJ ONLINE ACADEMY 39


STRUCTURE PROGRAMMING SHORT NOTE

Pseudo Code

NSJ ONLINE ACADEMY 40


STRUCTURE PROGRAMMING SHORT NOTE

For More Explains Visit NSJ Online Academy and Watch


Structure Programming Rapid Revision Series

NSJ ONLINE ACADEMY 41


STRUCTURE PROGRAMMING SHORT NOTE

NSJ ONLINE ACADEMY 42

You might also like