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

Flow Charts - What Is a Flow Chart? When to Use a Flowchart? Flowchart Symbols & Components

  Flow Charts     Flow charts are a useful tool in many situations, as we make a process easy to understand at a glance. Using just a few words and some simple symbols, they show clearly what happens at each stage and how this affects other decisions and actions.   What Is a Flow Chart?     In 1921, the Frank and Lillian presented what was only a "graphic-based method" in a presentation titled: “ Process Charts: First Steps in Finding the One Best Way to do Work ”, to members of the American Society of Mechanical Engineers (ASME). When to Use a Flowchart?     Flowchart is a very simple yet powerful tool to improve productivity in both our personal and work life. Here are some ways flowchart can be helpful:   ·      Document a process ·      Present solution to a problem ·      Brainstorm ideas in a meeting ·      Design an operation system ·    ...

Most Asked Pattern Programs in C

  Pattern Programs in C  

Generations-of-Computers

  What is a computer?            Computer is an advanced electronic device that takes raw data as an input from the user and processes it under the control of a set of instructions (called program), produces a result (output), and saves it for future use. This tutorial explains the foundational concepts of computer hardware, software, operating systems, peripherals, etc. along with how to get the most value and impact from computer technology. Functionalities of a Computer  There are three basic functionalities of a Computer System and they are  1. Input  2. Process  3. Output  But if we look at it in a very broad sense, any digital computer carries out the following five functions: Step 1 - Takes data as input.  Step 2 - Stores the data/instructions in its memory and uses them as required. Step 3 - Processes the data and converts it into useful information.  Step 4 - Generates the output.  Step 5 - Controls...