Lopping
Lopping
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;
}
#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
}
#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;
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.
}
• 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.