0% found this document useful (0 votes)
27 views12 pages

Loops

The document discusses different types of loops in programming including while, for, and do-while loops. It provides examples of the general syntax for each loop type and how they are used to repeatedly execute blocks of code. Specific features covered include initializing and updating loop counters, testing loop conditions, nesting loops, and using break and continue statements. The document concludes with programming assignments involving loops to calculate factorials, find Armstrong numbers, count positive/negative/zero numbers, convert integers to octal, and print number patterns.

Uploaded by

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

Loops

The document discusses different types of loops in programming including while, for, and do-while loops. It provides examples of the general syntax for each loop type and how they are used to repeatedly execute blocks of code. Specific features covered include initializing and updating loop counters, testing loop conditions, nesting loops, and using break and continue statements. The document concludes with programming assignments involving loops to calculate factorials, find Armstrong numbers, count positive/negative/zero numbers, convert integers to octal, and print number patterns.

Uploaded by

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

Loops

Loops
• Loops are used to perform a set of instructions
repeatedly.

• Types of loops
1. for
2. while
3. do-while
The while Loops
• We use while loop when we want to do
something a fixed number of times.
• General form of while loop
initialize loop counter
while(test loop counter using a condition)
{
do this;
and this;
increment loop counter;
}
The whileLoops…
• The statements within the while loop would
keep on getting executed till the condition
being tested remains true.
• In place of the condition there can be any other
valid expression.
• The condition being tested may use relational
or logical operators as shown in the following
examples.
while(i<=10)
while(i>=10 && j<=15)
while(j> 10 && (b<15 || c<20))
The while Loops…
• The statement within the loop may be a
single line or a block of statements.
• Infinite or indefinite loop.
• Instead of incrementing a loop counter, we
can even decrement it.
• It is not necessary that a loop counter must
only, be an int. It can even be a float.
• More operators
i++, ++i, i+=1
The for Loop
• The for allows us to specify three things about a
loop in a single line:
1. Setting a loop counter to an initial value.
2. Testing the loop counter to determine whether its value has
reached the number of repetitions desired.
3. Increasing the value of loop counter each time the program
segment within the loop has been executed.
• General form of for loop
for( initialize counter; test counter; increment counter)
{
do this ;
and this;
and this;
}
The forLoop
• for(i=10; i ;i--)
printf(“%d”,i);

• for(i=1;i<=10;printf(“%d”,i++))
;

• for( scanf(“%d”,&i); i<=10 ; i++)


printf(“%d”,i);
The forLoop
• for(i=1; i<10 ; )
{ printf(“%d”,i);
i++; }
• int i=1;
for( ;i<=10;i++)
printf(“%d”,i);

• int i=1;
for( ;i<=10;)
{ printf(“%d”,i);
i=i+1; }
The for Loop
• Nesting of loops.
• Multiple initializations in the for loop.
for(i=1,j=1;i<10;i++)
• Multiple condition in the for loop.
for(i=1;i<10 && j<10;i++)
The do – while Loop
• General form
do
{
statements;
----
----
} while(condition);
• The break; statement.
• The continue; statement.
• Difference between while and do-while.
Assignment
1. Write a program to find the factorial value of any number
entered through the keyboard.
2. Write a program to print out all Armstrong numbers between 1
and 500.
3. Write a program to enter the numbers till the user wants and at
the end it should display the count of positive, negative and
zeros entered.
4. Write a program to receive an integer and find its octal
equivalent.
5. Write a program to produce the following output:
1
2 3
4 5 6
7 8 9 10
Assignment
6. Write a program to print all prime numbers from 1 to
300.
7. Write a program to print the following triangles
*
**
***
****
&
1
12
123
1234

You might also like