En programación, a veces es necesario realizar alguna operación más de una vez o (digamos) n número de veces. Los bucles se utilizan cuando necesitamos ejecutar repetidamente un bloque de instrucciones.
Por ejemplo : supongamos que queremos imprimir «Hello World» 10 veces. Esto se puede hacer de dos maneras como se muestra a continuación:
C
// C program to illustrate need of loops #include <stdio.h> int main() { printf( "Hello World\n"); printf( "Hello World\n"); printf( "Hello World\n"); printf( "Hello World\n"); printf( "Hello World\n"); printf( "Hello World\n"); printf( "Hello World\n"); printf( "Hello World\n"); printf( "Hello World\n"); printf( "Hello World\n"); return 0; }
C++
// C++ program to illustrate need of loops #include <iostream> using namespace std; int main() { cout << "Hello World\n"; cout << "Hello World\n"; cout << "Hello World\n"; cout << "Hello World\n"; cout << "Hello World\n"; cout << "Hello World\n"; cout << "Hello World\n"; cout << "Hello World\n"; cout << "Hello World\n"; cout << "Hello World\n"; return 0; }
C
// C program to illustrate for loop #include <stdio.h> int main() { int i=0; for (i = 1; i <= 10; i++) { printf( "Hello World\n"); } return 0; }
C++
// C++ program to illustrate for loop #include <iostream> using namespace std; int main() { for (int i = 1; i <= 10; i++) { cout << "Hello World\n"; } return 0; }
C
// C program to illustrate while loop #include <stdio.h> int main() { // initialization expression int i = 1; // test expression while (i < 6) { printf( "Hello World\n"); // update expression i++; } return 0; }
C++
// C++ program to illustrate while loop #include <iostream> using namespace std; int main() { // initialization expression int i = 1; // test expression while (i < 6) { cout << "Hello World\n"; // update expression i++; } return 0; }
C
// C program to illustrate do-while loop #include <stdio.h> int main() { int i = 2; // Initialization expression do { // loop body printf( "Hello World\n"); // update expression i++; } while (i < 1); // test expression return 0; }
C++
// C++ program to illustrate do-while loop #include <iostream> using namespace std; int main() { int i = 2; // Initialization expression do { // loop body cout << "Hello World\n"; // update expression i++; } while (i < 1); // test expression return 0; }
C
// C program to demonstrate infinite loops // using for and while // Uncomment the sections to see the output #include <stdio.h> int main () { int i; // This is an infinite for loop as the condition // expression is blank for ( ; ; ) { printf("This loop will run forever.\n"); } // This is an infinite for loop as the condition // given in while loop will keep repeating infinitely /* while (i != 0) { i-- ; printf( "This loop will run forever.\n"); } */ // This is an infinite for loop as the condition // given in while loop is "true" /* while (true) { printf( "This loop will run forever.\n"); } */ }
C++
// C++ program to demonstrate infinite loops // using for and while // Uncomment the sections to see the output #include <iostream> using namespace std; int main () { int i; // This is an infinite for loop as the condition // expression is blank for ( ; ; ) { cout << "This loop will run forever.\n"; } // This is an infinite for loop as the condition // given in while loop will keep repeating infinitely /* while (i != 0) { i-- ; cout << "This loop will run forever.\n"; } */ // This is an infinite for loop as the condition // given in while loop is "true" /* while (true) { cout << "This loop will run forever.\n"; } */ }
C
#include <stdio.h> int main() { while (1) printf("This loop will run forever.\n"); return 0; }
C++
#include <iostream> using namespace std; int main() { while (1) cout << "This loop will run forever.\n"; return 0; }
C
#include <stdio.h> int main() { do{ printf("This loop will run forever.\n"); } while(1); return 0; }
C++
#include <iostream> using namespace std; int main() { do{ cout << "This loop will run forever.\n"; } while(1); return 0; }
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA