Welcome !!

NICETECHVIDYA

Tutorials

Web Designs

You are welcome to Aptech Designers, designed by Pratik from Nepal.

Creativity is my passion. Logo describes your details about company.

Graphics Design needs vision to create yours unique digital concept.

Responsive and All Browser Compatible Dynamic Web Layouts our target.

Home Python C Language J A V A Our Courses
This WebSite is under Construction....

Thursday, 10 March 2022

32.Break,Flag,Continue

 

break:

It is an unconditional control structure.

It is used to terminate the execution of switch-case or any iterative control structure abnormally.

 

 

 

 

 

 

 


                                               

 

 

 

Example:

#include<stdio.h>

void main()

{

int i;

 

for(i=1;i<=100;i++)

{

     printf("%5d",i);

     if(i==5)

          break;

}

 

}

 

Output:

1          2          3          4          5

 

Example explained:

The loop has to print 1 to 100 natural numbers but the break; statement terminate the loop when "i" is 5

So the output is 1 to 5 natural numbers.

 

Specification:

Accept any integer and print whether the number is a prime number or not using the concept flag.

 

flag: It is the most important logic used in many places while developing real time applications.

A variable (flag) is initialized with a value say 0 (false).

The value of variable (flag) is changed if any criterion is satisfied 1 (true).

We take a decision according to the value of variable (flag)

 

Program:

#include<stdio.h>

void main()

{

int i,flag,n;

 

printf("Enter any integer:");

scanf("%d",&n);

flag=1;

for(i=2;i<n;i++) 

   if(n%i==0)

   {                       

       falg=0;

       break;

   }           

if(flag==1 && n>1)

     printf("Prime number");

else

     printf("Not a prime number");

 

}

 

Execution1:

Enter any integer: 1

Not a prime number

 

Enter any integer: 8

Not a prime number

 

Enter any integer: 11

Prime number

 

Example explained:

flag is stored with true(1) by default.

Checking factors between 2 to n-1 because 1 and it self are the factors of any number.

If any factor appears in between, then flag is stored with false (0).

After the completion of loop

If flag is 1, then "n" has no factors other than 1 and itself so it is a prime.

If flag is 0, then "n" has at least one factor other than 1 and itself so it is not a prime.

 

 

 

 

 

Continue:

It is an unconditional control structure used to send the control to the conditional statement of any iterative control structure.

The statements after continue structure will be skipped from execution.

It can be used with any iterative control structure like while, for and do-while.

 

 

 

 

 

 

 

 

 

 

 

 


    

 

 

 

 

 

 

 

 

 

Example:

#include<stdio.h>

void main()

{

int i;

 

for(i=1;i<=10;i++)

{

    if(i%2==0)

          continue;

    printf("%5d",i);

}

 

}

 

Output:

1          3          5          7          9

 

 

Example explained:

i=1;i<=10;i++ generates 1 to 10 natural numbers.

When "i" is an even then continue; sends the control to the conditional statement without printing the value of "i"

The value of "i" only prints when it is an odd.

 

0 comments:

Post a Comment