Loop

Loop (repetition) में एक या एक से अधिक statements होती है, जो तब तक statements को बार-बार दुहराता है जब की दी गयी condition false न हो जाये। (Repetition of statements until given condition is not false)

Types of loop (Loop के प्रकार)

Loop निम्न दो प्रकार के होते है –

  1. Entry control loop
  2. Exit control loop

1. Entry Control Loop

Entry control loop वह लूप होते है, जो statements repetition करने से पहले condition को check करता है, यदि condition true है तो statements चलेगी, condition false है तो loop statements run नहीं होगी।

while loop, for loop को entry control लूप कहते है।

Example while loop-

// print hello world 100 time

#include <stdio.h>

int main() {
  int i=1;
  while(i<=100) {
  	printf("Hello World\n");
    i++;
  }
  
  return 0;
}

Example For loop-

// print hello world 100 time

#include <stdio.h>

int main() {
  for(int i=1; i<=100;i++) {
  	printf("Hello World\n");
  }
  
  return 0;
}

2. Exit Control Loop

Exit control loop वह लूप होते है, जो statements repetition करने से पहले condition को check करता है, यदि condition true है तो statements चलेगी, condition false है तो loop statements काम-से-काम एक बार तो चलेंगी ही।

do-while loop को exit control loop कहते है।

Example

// print hello world 100 time

#include <stdio.h>

int main() {
  int i=1;
  do {
  	printf("Hello World\n");
    i++;
  } while (i<=100);
  
  return 0;
}

For Loop

For loop एक प्रकार का entry control loop है, जो बार-बार code को चलता है जब तक दी गयी condition false न हो जाये।

// print hello world 100 time

#include <stdio.h>

int main() {
  for(int i=1; i<=100;i++) {
  	printf("Hello World\n");
  }
  
  return 0;
}

While Loop

while loop एक प्रकार का entry control loop है, जो बार-बार code को चलता है जब तक दी गयी condition false न हो जाये।

// print hello world 100 time

#include <stdio.h>

int main() {
  int i=1;
  while(i<=100) {
  	printf("Hello World\n");
    i++;
  }
  
  return 0;
}

Do-While Loop

do-while loop एक प्रकार का exit control loop है, जो बार-बार code को चलता है जब तक दी गयी condition false न हो जाये। यदि condition false है तो कोड एक बार जरूर ही run करेगा।

// print hello world 100 time

#include <stdio.h>

int main() {
  int i=1;
  do {
  	printf("Hello World\n");
    i++;
  } while (i<=100);
  
  return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *