0% found this document useful (0 votes)
37 views

Procedural, OOPs & Loops in C++

The document compares procedural and object-oriented programming, and discusses loops in C++. It states that procedural programming uses functions to divide a program into parts, while object-oriented programming uses objects. It also explains the different types of loops in C++ - for, while, and do-while loops - and provides examples of each.

Uploaded by

john
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Procedural, OOPs & Loops in C++

The document compares procedural and object-oriented programming, and discusses loops in C++. It states that procedural programming uses functions to divide a program into parts, while object-oriented programming uses objects. It also explains the different types of loops in C++ - for, while, and do-while loops - and provides examples of each.

Uploaded by

john
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Difference between Procedural & OOPs, Loops in C++

Procedural Oriented Programming Object Oriented Programming

In procedural programming, program is divided into In object oriented programming, program is


small parts called functions. divided into small parts called objects.

Procedural programming follows top down Object oriented programming follows bottom
approach. up approach.

There is no access specifier in procedural Object oriented programming have access


programming. specifiers like private, public, protected etc.

Adding new data and function is not easy. Adding new data and function is easy.

Procedural programming does not have any proper Object oriented programming provides data
way for hiding data so it is less secure. hiding so it is more secure.

In procedural programming, overloading is not Overloading is possible in object oriented


possible. programming.

In procedural programming, function is more In object oriented programming, data is more


important than data. important than function.

Object oriented programming is based


Procedural programming is based on unreal world. on real world.

Examples: C, FORTRAN, Pascal, Basic etc. Examples: C++, Java, Python, C# etc.

By: Mahesh Patidar


Difference between Procedural & OOPs, Loops in C++

Loops in C++

Loops in programming come into use when we need to repeatedly execute a block of statements. For example:
Suppose we want to print “Hello World” 10 times. This can be done in two ways as shown below:
Iterative Method
An iterative method to do this is to write the printf() statement 10 times.
In Loop, the statement needs to be written only once and the loop will be executed 10 times as shown below.
In computer programming, a loop is a sequence of instructions that is repeated until a certain condition is
reached.
 An operation is done, such as getting an item of data and changing it, and then some condition is checked
such as whether a counter has reached a prescribed number.
 Counter not Reached: If the counter has not reached the desired number, the next instruction in the
sequence returns to the first instruction in the sequence and repeat it.
 Counter reached: If the condition has been reached, the next instruction “falls through” to the next
sequential instruction or branches outside the loop.
There are mainly two types of loops:
1. Entry Controlled loops: In this type of loops the test condition is tested before entering the loop body. For
Loop and While Loop are entry controlled loops.
2. Exit Controlled Loops: In this type of loops the test condition is tested or evaluated at the end of loop body.
Therefore, the loop body will execute atleast once, irrespective of whether the test condition is true or
false. do – while loop is exit controlled loop.

for Loop
A for loop is a repetition control structure which allows us to write a loop that is executed a specific number of
times. The loop enables us to perform n number of steps together in one line.
Syntax:
for (initialization expr; test expr; update expr)
{
// body of the loop
// statements we want to execute
}
In for loop, a loop variable is used to control the loop. First initialize this loop variable to some value, then
check whether this variable is less than or greater than counter value. If statement is true, then loop body is
executed and loop variable gets updated . Steps are repeated till exit condition comes.
 Initialization Expression: In this expression we have to initialize the loop counter to some value. for
example: int i=1;
 Test Expression: In this expression we have to test the condition. If the condition evaluates to true then we
will execute the body of loop and go to update expression otherwise we will exit from the for loop. For
example: i <= 10;
 Update Expression: After executing loop body this expression increments/decrements the loop variable by
some value. for example: i++;

By: Mahesh Patidar


Difference between Procedural & OOPs, Loops in C++

Equivalent flow diagram for loop :

Example:

C
// C program to illustrate for loop
#include <stdio.h>

int main()
{
int i=0;

for (i = 1; i <= 10; i++)


{
printf( "Hello World\n");
}

return 0;
}
C++
// C++ program to illustrate for loop
#include <iostream>
using namespace std;

int main()
{
for (int i = 1; i <= 10; i++)
{
cout << "Hello World\n";
}

return 0;
}
Output:
Hello World
Hello World
Hello World
Hello World

By: Mahesh Patidar


Difference between Procedural & OOPs, Loops in C++

Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

While Loop
While studying for loop we have seen that the number of iterations is known beforehand, i.e. the number of
times the loop body is needed to be executed is known to us. while loops are used in situations where we do not
know the exact number of iterations of loop beforehand. The loop execution is terminated on the basis of test
condition.
Syntax:
We have already stated that a loop is mainly consisted of three statements – initialization expression, test
expression, update expression. The syntax of the three loops – For, while and do while mainly differs on the
placement of these three statements.

initialization expression;
while (test_expression)
{
// statements

update_expression;
}

Flow Diagram:

Example:
C
// C program to illustrate while loop
#include <stdio.h>

int main()
{
// initialization expression
int i = 1;

// test expression
while (i < 6)
{
printf( "Hello World\n");

// update expression
i++;
}

By: Mahesh Patidar


Difference between Procedural & OOPs, Loops in C++

return 0;
}
C++
// C++ program to illustrate while loop
#include <iostream>
using namespace std;

int main()
{
// initialization expression
int i = 1;

// test expression
while (i < 6)
{
cout << "Hello World\n";

// update expression
i++;
}

return 0;
}
Output:
Hello World
Hello World
Hello World
Hello World
Hello World
do while loop
In do while loops also the loop execution is terminated on the basis of test condition. The main difference
between do while loop and while loop is in do while loop the condition is tested at the end of loop body, i.e do
while loop is exit controlled whereas the other two loops are entry controlled loops.
Note: In do while loop the loop body will execute at least once irrespective of test condition.
Syntax:
initialization expression;
do
{
// statements

update_expression;
} while (test_expression);
Note: Notice the semi – colon(“;”) in the end of loop.
Flow Diagram:

By: Mahesh Patidar


Difference between Procedural & OOPs, Loops in C++

Example:
C
// C program to illustrate do-while loop
#include <stdio.h>

int main()
{
int i = 2; // Initialization expression

do
{
// loop body
printf( "Hello World\n");

// update expression
i++;

} while (i < 1); // test expression

return 0;
}
C++
// C++ program to illustrate do-while loop
#include <iostream>
using namespace std;

int main()
{
int i = 2; // Initialization expression

do
{
// loop body
cout << "Hello World\n";

// update expression
i++;

} while (i < 1); // test expression

return 0;
}
Output:
Hello World
In the above program the test condition (i<1) evaluates to false. But still as the loop is exit – controlled the loop
body will execute once.

By: Mahesh Patidar

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy