Skip to main content

LOOP IN C and CPP PROGRAMMIMG

What is Looping in C & C++?

AJAY SIR, Loop Example

The looping can be defined as repeating the same process multiple times until a specific condition satisfies. It is known as iteration also. 

  • There are three types of loops used in the C,C++ language language. 
  • We are going to learn all the aspects of C, C++ loops.

Why looping?

The looping simplifies the complex problems into the easy ones. It enables to alter the flow of the program so that instead instead of writing writing the same code again and again, we can execute the same code for a finite number of times. For example, if we need to print ‘Bhopal’ 10-times then, instead of using the printf/cout statement 10 times, we can use printf in c and cout in C++ once inside a loop which runs up to 10 iterations.

What are the advantages of Looping?

  1. It provides code reusability.
  2. Using loops, we do not need to write the same code again and again.
  3. Using loops, we can traverse over the elements of data structures structures (array or linked lists).

Types of Loops

There are two type of loop in C and C++ Prog. language those are given below:

Entry Loop
  1. for
  2. while
Exit Loop
  1. do while

Essential components of a loop:-

  • Counter
  • Initialization of the counter with initial value
  • Condition to check with the optimum value of the counter
  • Statement(s) to be executed by iteration
  • Increment/decrement


for loop in C and C++

The for loop in C and C++ language is used to iterate the statements or a part of the program several times. It is frequently used to traverse the data structures like the array and linked list. 


  • The syntax of for loop in c language is given below:
for (Expression1; Expression2; Expression3)
{
     codes to be executed;
}

Expression 1 (Optional)
  • Represents the initialization of the loop variable.
  • More than one variable can be initialized.

Expression 2
  • Expression 2 is a conditional expression. It checks for a specific condition to be satisfied. If it is not, the loop is terminated.
  • Expression 2 can have more than one condition. However, the loop will iterate until the last condition becomes false. Other conditions will be treated as statements.
Expression 3
  • Expression 3 is increment or decrement to update the valu of the loop variable

while loop in C

  • The while loop in C and CPP is to be used in the scenario where the block of statements is executed in the while loop until the condition specified in the while loop is satisfied. It is also called a pre-tested loop.

  • The syntax of while loop in c language is given below:
        initialization;
        while(condition)
{
    block of statements to be executed ;
    increment;
}

do-while loop in C

  • The do-while loop continues until a given condition satisfies. It is also called post tested loop. It is used when it is necessary to execute the loop at least once (mostly menu driven programs)


  • The syntax of The syntax of do-while loop in c language loop in c language is given below:
do
{
code to be executed ;
}
while(condition);
__________________________________________________________


Example of Loop in C Programming:


Program to print natural numbers 1 to 15 using for loop in C and CPP.

main()
{
int i;
for(i=1;i<=15;i=i+1)
{
printf("%d, ", i); // for C Labgungae     

}
}

_______________________________________

main()
{
int i;
for(i=1;i<=15;i=i+1)
{
cout<<i<<endl;             // for  C++ prog language
       
}
}
-------------------------------------------------------------
output
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
-------------------------------------------------------------




Program to print table for the given number using while loop in C

#include<stdio.h>
main()
{
int i=1,number,b;
printf("Enter a number: ");
scanf("%d",&number);
while(i<=10)
{
b=number*i;
printf("%d \n", b);
i=i+1;
}
}
-------------------------------------------------------------
Output
Enter a number: 5
5
10
15
20
25
30
35
40
45
50
-------------------------------------------------------------


Program to print natural numbers 1 to 10 using do while loop.

#include<stdio.h>
main()
{
int i=1;
do
{
printf("%d \n",i);
i=i+1;
}
while(i<=10);
}
-------------------------------------------------------------
Output
1 2 3 4 5 6 7 8 9 10
-------------------------------------------------------------


#Happy_Codding with #MrAJ


Comments

Popular posts from this blog

C++ Lec-2 Introduction to Object-Oriented Programming (OOP) in C++

  Introduction to Object-Oriented Programming (OOP) in C++ Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to organize software design. It allows for modelling real-world entities and relationships in a program. C++ is an object-oriented programming language that provides features to implement OOP concepts effectively. Key Concepts of OOP   Class and Object o    Class : A blueprint for creating objects. It defines properties and behaviours of objects.   class Car { public:     string brand;     string model;     int year; };   o    Object : An instance of a class.   Car car1; car1.brand = "Mahindra"; car1.model = "THAR"; car1.year = 2024;   Encapsulation o    Encapsulation is the bundling of data and methods that operate on that data within a single unit, or class. It restricts direct acces...

C++ Lec-1 (Introduction to Programming Languages)

  Introduction to Programming Languages ________________________________________________________________________ P rogramming languages are formal languages comprising a set of instructions that produce various kinds of output. They are used in computer programming to implement algorithms and control the behavior of machines. Language Generation Programming languages have evolved through several generations: First Generation (1GL) : Machine language, the most basic level of programming languages, consisting of binary code. Second Generation (2GL) : Assembly language, a low-level language with a strong correspondence to the machine language instructions. Third Generation (3GL) : High-level languages like C, C++, Java, Python, which are more abstract and easier for humans to understand. Fourth Generation (4GL) : Languages closer to human language, often used for database querying and report generation (e.g., ...

DATA TYPE in C/C++

  BUILT IN OF PRIMARY DATA TYPE     int (Integer) float(Floating-point) double (Double Floating-point) char (Character) wchar_t (Wide Character) bool (Boolean) void (Empty) Modified Data Types List    signed int - (used for integers) unsigned - int (can only store positive integers) short (used - for small integers (range -32768 to 32767)) unsigned short - (used for small positive integers (range 0 to 65,535)) Long - (used for large integers) unsigned long - (used for large positive integers ) long long - (used for very large integers) unsigned long long - (used for very large positive integers) long double - (used for large floating-point numbers) signed char - (used for characters (guaranteed range -127 to 127)) unsigned char - (used for characters (range 0 to 255)) Derived Type Data Type    Function Array Pointers References User-Defined Data Type   Class Structure Union Enumeration Typedef defined DataType