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

Lec8 Loops

Loops allow code to be repeatedly executed. There are three types of loops in C: for, while, and do-while. The while loop first checks a condition and repeats the loop body if it is true. It has three parts: initialization, condition, and increment/decrement. The do-while loop is similar but executes the body at least once before checking the condition. Example code is provided to print the first 10 natural numbers using a while loop and to add user-entered numbers until they enter 0 using a do-while loop.

Uploaded by

Muneeb Shahid
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
0% found this document useful (0 votes)
31 views

Lec8 Loops

Loops allow code to be repeatedly executed. There are three types of loops in C: for, while, and do-while. The while loop first checks a condition and repeats the loop body if it is true. It has three parts: initialization, condition, and increment/decrement. The do-while loop is similar but executes the body at least once before checking the condition. Example code is provided to print the first 10 natural numbers using a while loop and to add user-entered numbers until they enter 0 using a do-while loop.

Uploaded by

Muneeb Shahid
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/ 15

LEC 10 & 11

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.
• C programming has three types of loops:
• for loop
• while loop
• do...while loop
while loop

• while loop can be addressed as an entry


control loop. It is completed in 3 steps.
• Variable initialization.(e.g int x = 0;)
• condition(e.g while(x <= 10))
• Variable increment or decrement ( x+
+ or x-- or x = x + 2 )
Syntax :

variable initialization;
while(condition)
{ statements;
variable increment or decrement; }
• Example: Program to print first 10 natural
numbers
#include<stdio.h>
void main( )
{ int x; x = 1;
while(x <= 10)
{ printf("%d\t", x); /* below statement means,
do x = x+1, increment x by 1*/
x++; } }
#include<stdio.h>
int main(){
int i=1,number=0,b=9;
printf("Enter a number: ");
scanf("%d",&number);
while(i<=10){
printf("%d \n",(number*i));
i++;
}
return 0;
}
#include<stdio.h>
void main ()
{
int j = 1;
while(j+=2,j<=10)
{
printf("%d ",j);
}
printf("%d",j);
}
#include<stdio.h>
void main ()
{
while()
{
printf("hello Javatpoint");
}
}
Output
compile time error: while loop can't be empty
Example 3
#include<stdio.h>
void main ()
{
int x = 10, y = 2;
while(x+y-1)
{
printf("%d %d",x--,y--);
}
}
Output
infinite loop
do...while loop

• The do..while loop is similar to the while loop


with one important difference. The body
of do...while loop is executed at least once.
Only then, the test expression is evaluated.
• The syntax of the do...while loop is:
do
{ // statements inside the body of the loop }
while (testExpression);
• // Program to add numbers until the user
enters zero
#include <stdio.h>
int main()
{ double number, sum = 0; // the body of the loop
is executed at least once
do { printf("Enter a number: ");
scanf("%lf", &number);
sum += number; }
while(number != 0.0);
printf("Sum = %.2lf",sum);
return 0; }
#include<stdio.h>
#include<conio.h>
int main()
{ int num=1; //initializing the variable
//do-while loop
do
{ printf("%d\n",2*num);
num++; //incrementing operation }
while(num<=10);
return 0; }
#include<stdio.h>
void main()
{ int a, i;
a = 5;
i = 1;
do
{ printf("%d\t", a*i);
i++; }
while(i <= 10); }

You might also like