100% found this document useful (1 vote)
84 views

Problem Based Upon Nested For/while Loop For Printing Pattern

The document discusses different types of loops in C programming language including while, do-while, for, and nested loops. It provides examples of each loop type and explains their syntax and usage. Key points covered include: - Loops repeat code until a condition is met - C has while, do-while, and for loops - Nested loops allow looping code inside another loop with no limit on nesting level - Examples demonstrate basic syntax and usage of each loop type

Uploaded by

nikhil kaushik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
84 views

Problem Based Upon Nested For/while Loop For Printing Pattern

The document discusses different types of loops in C programming language including while, do-while, for, and nested loops. It provides examples of each loop type and explains their syntax and usage. Key points covered include: - Loops repeat code until a condition is met - C has while, do-while, and for loops - Nested loops allow looping code inside another loop with no limit on nesting level - Examples demonstrate basic syntax and usage of each loop type

Uploaded by

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

2.

Problem based upon Nested


for/while loop for printing pattern.
What are Loops?
• A Loop executes the sequence of statements many
times until the stated condition becomes false.
• A loop consists of two parts, a body of a loop and a
control statement.
• The control statement is a combination of some
conditions that direct the body of the loop to
execute until the specified condition becomes false.
• The purpose of the loop is to repeat the same code a
number of times.
What are Loops?
Selection of a loop is always a tough task for a programmer, to select a loop do
the following steps:
• Analyze the problem and check whether it requires a pre-test or a post-test loop.
• If pre-test is required, use a while or for a loop.
• If post-test is required, use a do-while loop.
Summary
• Looping is one of the key concepts on any programming language.
• It executes a block of statements number of times until the condition becomes
false.
• Loops are of 2 types: entry-controlled and exit-controlled.
• 'C' programming provides us 1) while 2) do-while and 3) for loop.
• For and while loop is entry-controlled loops.
• Do-while is an exit-controlled loop.
• The control conditions must be well defined and
specified otherwise the loop will execute an infinite
number of times. The loop that does not stop
executing and processes the statements number of
times is called as an infinite loop.
• An infinite loop is also called as an "Endless loop."
• Following are some characteristics of an infinite loop:
1. No termination condition is specified.
2. The specified conditions never meet.
Types of Loop
'C' programming language provides us with
three types of loop constructs:
• 1. The while loop
• 2. The do-while loop
• 3. The for loop
While Loop
The basic format of while loop is as follows:
It is an entry-controlled loop. In while loop, a condition
is evaluated before processing a body of the loop. If a
condition is true then and only then the body of a loop
is executed.
After the body of a loop is executed then control again
goes back at the beginning, and the condition is
checked if it is true, the same process is executed until
the condition becomes false. Once the condition
becomes false, the control goes out of the loop.
While Loop - Syntax
while (condition)
{
statements;
}
While Loop - Example
Do-While Loop
• A do-while loop is similar to the while loop except
that the condition is always executed after the
body of a loop. It is also called an exit-controlled
loop.
• The basic format of while loop is as follows:
do
{
statements
} while (expression);
For Loop
A for loop is a more efficient loop structure in 'C' programming.
The general structure of for loop is as follows:

for (initial value; condition; incrementation or decrementation )


{
statements;
}
• The initial value of the for loop is performed only once.
• The condition is a Boolean expression that tests and compares the counter
to a fixed value after each iteration, stopping the for loop when false is
returned.
• The incrementation/decrementation increases (or decreases) the counter
by a set value.
For Loop
Nested Loop
• C supports nesting of loops in programming. 
• Nesting of loops is the feature in C that allows
the looping of statements inside another loop.
• Any number of loops can be defined inside
another loop, i.e., there is no restriction for
defining any number of loops. The nesting level
can be defined at n times. You can define any
type of loop inside another loop; for example,
you can define 'while' loop inside a 'for' loop.
Syntax of Nested Loop
Outer_loop  
{  
   // outer loop statements.  
 
Inner_loop  
   {  
         // inner loop statements.  
   }  
       // outer loop statements.  

}  
Nested for loop
for (initialization; condition; update)   
{  
    for(initialization; condition; update)  
    {  
           // inner loop statements.  
    }  
    // outer loop statements.  
}  
Nested while loop
while(condition)  
{  
    while(condition)  
    {  
         // inner loop statements.  
    }  
// outer loop statements.  
}  
Nested do..while loop
do  
{  
   do  
  {   
      // inner loop statements.  
   }while(condition);  
// outer loop statements.  
}while(condition);  
Nested for loop - example
Nested do while loop
#include <stdio.h>
int main()
{
do
{
printf("I'm from outer do-while loop ");
do
{
printf("\nI'm from inner do-while loop ");
}while(1 > 10);
}while(2 > 10); Output
return 0; I'm from outer do-while loop
I'm from inner do-while loop
}
#include <stdio.h>
#include <conio.h>
int main()
{
int i=1,j;
clrscr();
do
{
j=1;
do
{ Output
printf("* ");
*
j++;
**
}while(j <= i);
i++;
***
printf("\n"); ****
}while(i <= 5); *****
return 0;
}
Nested While Loop
#include <stdio.h>
#include <conio.h>
int main()
{
int i=1,j;
clrscr();
while (i <= 5)
{
j=1;
while (j <= i )
Output
{ 1
printf("%d ",j); 12
j++; 123
} 1234
printf("\n"); 12345
i++;
}
return 0;
}

You might also like