0% found this document useful (0 votes)
12 views47 pages

Lopping

The document explains the concept of looping in programming, detailing the steps involved in iteration and the different types of loops: while, do-while, and for loops. It provides syntax, flowcharts, and example programs for each loop type, demonstrating their usage in various scenarios. Additionally, it introduces nested loops and their applications in programming.

Uploaded by

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

Lopping

The document explains the concept of looping in programming, detailing the steps involved in iteration and the different types of loops: while, do-while, and for loops. It provides syntax, flowcharts, and example programs for each loop type, demonstrating their usage in various scenarios. Additionally, it introduces nested loops and their applications in programming.

Uploaded by

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

Looping

The process of repeating some portion of program


either a specified no of times or until a particular
condition is satisfied is known as looping or iteration
or repetition. The looping steps include following
steps:
•Setting and initialization of a counter.
•Execution of the statements inside the loop.
•Test for a specified condition for execution of the
loop.
•Incrementing the counter.
There are three methods by way of which we can
repeat a part of a program. They are:
• The while statement
• The do-while statement
• The for statement
The while statement:
The simplest of all the looping structure in C is the while statement. While
statement specifies that the section of code should be executed while the given
condition is true. It is also known as entry controlled loop. The general syntax
and flowchart of while statement is as follows:
Syntax:
initialize loop counter;
while(test condition)
{
do this;
and this;
and this;
……………;
increment counter;
}
Here, while is the keyword and condition can be any expression. When while is
encountered, test condition is checked first by the compiler. If the test condition
is satisfied then the block of statement inside the while loop is executed. After
the execution of block of statement, again condition is check. If the conditional
expression returns true, the block of statement is executed again. This process
is followed till the conditional expression returns false value.
Fig: Flowchart of while loop.

Initialize counter

Test False
condition
Exit from loop

True
Body of loop

increment counter
• Write a program that prints the statement “Hello BMC FIRST Semester!” five times.*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
while(i<=5)
{
printf("Hello FIRST Semester\n");
i=i+1;
}

printf("End of the program");


getch();
}
• /*Write a program that print the counting form 1 to 100*/
#include<stdio.h>
#include<conio.h>
void main()
{
int counting=1;
clrscr();
while(counting<=100)
{
printf("%d\t",counting);
counting=counting+1;
}
}
• /*programe to find out the value of even numbers among 1 to 100*/

#include<stdio.h>
#include<conio.h>
void main()
{
int count=1;
clrscr();
while(count<=100)
{
if(count%2==0)
{
printf("%d\t",count);

}
count=count+1;
}
getch();
}
/*find the enter number is prine or not*/
#include<stdio.h>
#include<conio.h>
void main()
{
int num,i =2;
clrscr();
printf("\n Enter any number");
scanf("%d",&num);
while(i<num)
{
if(num%i==0)
{
printf("not a prime number");
break;
}
i++;
}
if(i==num)
printf("prime number");
getch();
}
do-while loop
• Since, in the while statement the condition is
tested before executing the body of the loop.
But there are certain other situations where it
might be necessary to execute the body of the
loop before the test condition is checked. In
such situation we can use the do-while loop. It
is also known as exit-controlled loop.
Syntax : do-while loop

Syntax:
Initialize loop counter;
do{
do this;
do this;
…..
increment/decrement counter;
}while(condition);
flowchart of do-while loop

Body of loop

False
Test
condition

True
Exit
• Here, while and do are the keywords which is known to the
compiler. Condition can be any expression. This is very similar to
while loop. Here, the block of statement enclosed in the opening and
closing braces after the keyword do is executed at least once. After
the execution of the block of statement for the first time, the
condition in the while is executed. If the condition is satisfied then
the block of statement inside the do block is executed. After the
execution of block of statement, again condition is check. If the
conditional expression returns true, the block of statement is
executed again. This process is followed till the conditional
expression returns false value.
• Note: It is necessary to write semicolon (;) after the
while(condition) as shown in the syntax else you will get an error.
/* Write a program that prints the statement “Hello First
Semester” five times using do-while loop.*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
do
{
printf("Hello First Semester\n");
i=i+1;
} while(i<=5);
printf("End of the program");
getch();}
While loop Do-while loop

1. The condition is checked at first. 1. The condition is checked at last.


2. The while statement is used to carry out looping 2. The do-while loop executes a body of statements at
operations, in which a group of statements is executed least one time, and it executes more if the expression is
repeatedly, until the condition is satisfied. true.
3. The general form of the while statement is 3. The general form of do-while loop is
while (expression) do{
{ statements;
statements; } while (expression);
}
4. /*Program to display consecutive digits /*Program to display consecutive digits
1,2,……………………,10 */ 1,2,……………………,10 */
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void main() void main()
{ {
int i=1; int i=1;
while(i<=10) do
{ {
printf(“%d\n”,i); printf(“%d\n”,i);
i++; i++;
} } while(i<=10);
getch(); getch();
} }
1. it is also known as entry controlled loop. It is also known as exit controlled loop
/*even number 2 to 100 using do while*/
#include<stdio.h>
int main()
{
int i=2;
do{
printf("%d\t",i);
i=i+2;
}while(i<=100);

}
#include<stdio.h> switch(ch)
{
#include<conio.h>
case 'm':
int main()
printf("your order is MOMO\n");
{
break;
char ch,cho; case 'c':
do printf("your order is CHOWMEIN\n");
{ break;

printf("MENU\n"); case 'b':


printf("your order is BURGER\n");
printf("c\t CHOWMEIN\n");
break;
printf("m\tMOMO\n");
default:
printf("b\t BURGER\n");
printf("NOT AVAILABLE\n");
printf("enter your choice\n");
}
scanf("%c",&ch);
printf("do you want to continue\n");
fflush(stdin);
scanf("%c",&cho);
fflush(stdin);
}while(cho=='y'||cho=='Y');
return 0;}
For Statement:
In while and do-while statement we need to write
logic to repeatedly execute a block of statement by
initializing a counter and incrementing it after a
particular steps. This sometime looks tedious and
decreases the readability of the program. This can be
improved by using for statement. Using for statement
we can merge all the three parts i.e. assignment,
condition checking and increment/decrementing.
Syntax:
for(initialization; test condition;
increment/decrement)
{
/*block of statement*/
}
Initialization: means setting up a loop counter to
initial value
test condition: Means testing the loop counter to
determine whether the loop needs to be executed or
not.
Increment/decrement: Means increment or
decrement the loop counter value
The for loop is executed as follows:

1. The initial counter value is initialized. This initialization is


done only once for the entire for loop.
2. After the initialization, test condition is checked. Test
condition can be any relational or logical expression. If the
test condition is satisfied i.e. the condition evaluates to true
then the block of statement inside the for loop is executed.
3. After the execution of the block of statement,
increment/decrement of the counter is done. After
performing this, the test condition is again evaluated. The
step 2 and 3 are repeated till the test condition becomes
false.
// DISPLAYING YOUR NAME AND COUNTRY 10 TIMES
EACH
#include <stdio.h>
#include <conio.h>
void main()
{
int i;
for(i=0; i<10; i++)
{
printf("\n My name is Himal.");
printf(“\n I live in Nepal.”) ;
}
getch();
}
//MULTIPLICATION TABLE
#include <stdio.h>
#include <conio.h>
void main()
{
int i=8,j;
for(j=1;j<=10;j++)
{
printf("%d * %d = %d\n", i, j, i*j);
}
getch();
}
/*Print Fibonacci series*/
#include<stdio.h>
#include<conio.h>
void main()
{
int f0=0,f1=1,f2,i;
printf("%d,%d",f0,f1);
for(i=2;i<10;i++)
{
f2=f0+f1;
printf(",%d", f2);
f0=f1;
f1=f2;
}
}
Assignment:
• Write a program to reverse a number using while
loop
• Write a program to read an integer number n from
keyboard and display the message “Get Well Soon” n
times.
• Write a program to calculate the factorial of a
number using for loop.
• Write a program that asks an integer number n and
calculate sum of all natural numbers from 1 to n.
• Compute 12+22+32+……………….+n2using for loop (Take
n from user).
• Write a program that finds whether the given number
is prime or not using for loop.
• Nested loop:
A nested loop is a logical structure used in computer
programming where two repeating statements are placed in a
"nested" form, i.e., one loop is situated within the body of the other.
In a nested loop, the first iteration of the outer loop causes the
inner loop to execute. The inner loop then repeats for as many times
as is specified. When the inner loop completes, the outer loop is
executed for its second iteration, triggering the inner loop again, and
so on until the requirements for the outer loop are complete.
Looping, and by extension nested looping, is a powerful construct in
programming as it allows rapid sorting or insertion of large amounts
of data in an efficient way. Solving problems in the business world
often involves repeating an action over and over with hundreds,
thousands, or even millions of items of data. Nested loops are
therefore frequently used constructs in all kinds of computer
programs in all types of industry.
More than two loops can be nested, and nested loops can go as deep
as required. To take an example from the real world which explains
the idea of multiple nested loops, consider the carodometer. It is
easy to imagine the leftmost digit of the odometer as the
outermost nested loop, and each successive digit as another loop,
each within the other, until one reaches the rightmost digit, which
represents the innermost nested loop.
NESTED LOOP
The syntax is:
for(counter initialization; test condition; increment/decrement)
{
for(ctr initializaion; test; incr/decr)
{
statements;
}
}
//Multiplication table from 2 to 10
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
for (i=2;i<=10;i++)
{
for (j=1;j<=10;j++)
{
printf("%d*%d=%d\n",i,j, i*j);
}
printf("\n");
}
getch();
}
#include<stdio.h> /* Prime numbers from 1 to given number*/
#include<conio.h>
void main()
{
int i,j,n;
printf("Enter any number: ");
scanf("%d", &n);
for(i=2; i<=n; i++)
{
for(j=2; j<i; j++)
{
if(i%j==0)
{

break;
}

}
if(i==j)
printf("\t%d",i);
}
getch();
}
#include<stdio.h>
#include<conio.h> program to print star
int main() *
{
**
int i,j;
for(i=1 ; i<=5 ; i++)
***
{ ****
for(j=1 ; j<=i ; j++) *****
{
printf("*");
}
printf("\n");
}
getch();
}
#inlude<stdio.h> program to print
#include<conio.h>
int main() 1
{ 22
int i,j;
for(i=1;i<=5;i++) 333
{ 4444
for(j=1;j<=i;j++) 55555
{
printf("%d",i);
}
printf("\n");
}
getch();
}
#include<stdio.h>
#include<conio.h> program to print
int main() 1
{ 12
int i,j; 123
for(i=1;i<=5;i++) 1234
{
12345
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
getch();
}
int main()
{
int row,j,n,space;
program to print
printf("enter the value of n");
scanf("%d",&n); *
for(row=1;row<=n;row++) ***
{
for(space=1;space<=n-row;space++) *****
{ *******
printf(" ");
} *********
for(j=1;j<=row*2-1;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
1 2 3 4 5 6 7 8 9
1 *
2 * * *
3 * * * * *
4 * * * * * * *
5 * * * * * * * * *
1 2 3 4 5 6 7
1 *
2 * *
3 * * *
4 * * * *
5 * * * * *
6 * * * * * *
7 * * * * * * *
int main()
{
int row,j,n,space;
printf("enter the value of n");
scanf("%d",&n);

for(row=1;row<=n;row++)
{
for(space=1;space<=n-row;space++){

printf(" ");
}
for(j=1;j<=row;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
Write a program to print the following output.
1
10
101
1010
10101
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",j%2);
}
printf("\n");
}
getch();
}
1 2 3 4 5 6 7 8 9
1 1
2 1 1 1
3 1 1 1 1 1
4 1 1 1 1 1 1 1
5 1 1 1 1 1 1 1 1 1
int main()
{
int row,j,n,space,x=1;
printf("enter the value of n");
scanf("%d",&n);
for(row=1;row<=n;row++)
{
for(space=1;space<=n-row;space++){

printf(" ");
}
for(j=1;j<=row*2-1;j++)
{
printf("%d",x);
}
printf("\n");
}
getch();
}
// print the pattern as follows
123456
12345
1234
123
12
1
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;

for (i=6;i>0;i--)
{
for (j=1;j<=i;j++)
{

printf("%d",j);

}
printf("\n");
}
getch();
}
break Statement
The break statement terminates the execution
of the loop and the control is transferred to the
statement immediately following the loop.
—As we know, a loop is terminated when its
condition becomes false, however if we have
to terminate the loop without testing loop the
termination condition, then break statement is
useful.

Syntax:
break;
continue Statement
The continue statement is used to bypass the
remainder of the current pass through the loop.
The loop does not terminate when a continue
statement is encountered.
“SKIP THE FOLLOWING STATEMENTS
—
AND CONTINUE WITH THE NEXT
ITERATION”

Syntax:
continue;
#include <stdio.h>
void main()
{
int i; int j = 10;
for( i = 0; i <= j; i ++ )
{
if( i == 5 )
{ continue; }
printf("Hello %d\n", i );
}
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i, num;
printf("\n Enter a number:");
scanf("%d", &num);
printf("\n The even numbers from 2 to %d are:\n", num);
for(i=1;i<=num;i++)
{
if(i%2!=0)
{
continue;
}
printf("%d\t", i);
}
getch();
}
goto Statement
The goto statement is used to alter the normal
sequence of program execution by unconditionally
transferring control to some other part of the
program.
The goto statement transfers the control to a labeled

statement somewhere in the current function using
syntax:
goto label;
Here, label is an identifier used to label the target

statement to which the control would be transferred.

—The target statement must be labeled using syntax:


label: statements;
#include<stdio.h>
#include<conio.h>
int main()
{
int x;
printf("enter the value of x");
scanf("%d",&x);
if(x>0)
goto positive;
printf("the number %d is negative",x);
getch();
return 0;
positive: printf("the number %d is positive",x);
getch();

}
• What are the differences between Break,
Continue, Return and Exit?
• break - The break statement is used to jump out
of loop. After the break statement control passes
to the immediate statement after the loop.
• continue - Using continue we can go to the next
iteration in loop.
• return - Exits the function.
• exit - It is used to exit the execution of program.
note: break and continue are statements, exit is
function.

You might also like