Hay situaciones en la vida real en las que necesitamos tomar algunas decisiones y, en base a estas decisiones, decidimos qué debemos hacer a continuación. También surgen situaciones similares en la programación en las que necesitamos tomar algunas decisiones y, en base a estas decisiones, ejecutaremos el siguiente bloque de código. Por ejemplo, en C, si ocurre x, ejecute y, de lo contrario, ejecute z. También puede haber múltiples condiciones como en C si ocurre x, luego ejecute p, de lo contrario, si ocurre la condición y, ejecute q, de lo contrario, ejecute r. Esta condición de C else-if es una de las muchas formas de importar múltiples condiciones.
C
// C program to illustrate If statement #include <stdio.h> int main() { int i = 10; if (i > 15) { printf("10 is less than 15"); } printf("I am Not in if"); }
C++
// C++ program to illustrate If statement #include<iostream> using namespace std; int main() { int i = 10; if (i > 15) { cout<<"10 is less than 15"; } cout<<"I am Not in if"; }
C
// C program to illustrate If statement #include <stdio.h> int main() { int i = 20; if (i < 15){ printf("i is smaller than 15"); } else{ printf("i is greater than 15"); } return 0; }
C++
// C++ program to illustrate if-else statement #include<iostream> using namespace std; int main() { int i = 20; if (i < 15) cout<<"i is smaller than 15"; else cout<<"i is greater than 15"; return 0; }
C
// C program to illustrate nested-if statement #include <stdio.h> int main() { int i = 10; if (i == 10) { // First if statement if (i < 15) printf("i is smaller than 15\n"); // Nested - if statement // Will only be executed if statement above // is true if (i < 12) printf("i is smaller than 12 too\n"); else printf("i is greater than 15"); } return 0; }
C++
// C++ program to illustrate nested-if statement #include <iostream> using namespace std; int main() { int i = 10; if (i == 10) { // First if statement if (i < 15) cout<<"i is smaller than 15\n"; // Nested - if statement // Will only be executed if statement above // is true if (i < 12) cout<<"i is smaller than 12 too\n"; else cout<<"i is greater than 15"; } return 0; }
C
// C program to illustrate nested-if statement #include <stdio.h> int main() { int i = 20; if (i == 10) printf("i is 10"); else if (i == 15) printf("i is 15"); else if (i == 20) printf("i is 20"); else printf("i is not present"); }
C++
// C++ program to illustrate if-else-if ladder #include<iostream> using namespace std; int main() { int i = 20; if (i == 10) cout<<"i is 10"; else if (i == 15) cout<<"i is 15"; else if (i == 20) cout<<"i is 20"; else cout<<"i is not present"; }
C
// C program to illustrate // Linear Search #include <stdio.h> void findElement(int arr[], int size, int key) { // loop to traverse array and search for key for (int i = 0; i < size; i++) { if (arr[i] == key) { printf("Element found at position: %d", (i + 1)); break; } } } int main() { int arr[] = { 1, 2, 3, 4, 5, 6 }; // no of elements int n = 6; // key to be searched int key = 3; // Calling function to find the key findElement(arr, n, key); return 0; }
C++
// CPP program to illustrate // Linear Search #include <iostream> using namespace std; void findElement(int arr[], int size, int key) { // loop to traverse array and search for key for (int i = 0; i < size; i++) { if (arr[i] == key) { cout << "Element found at position: " << (i + 1); break; } } } // Driver program to test above function int main() { int arr[] = { 1, 2, 3, 4, 5, 6 }; int n = 6; // no of elements int key = 3; // key to be searched // Calling function to find the key findElement(arr, n, key); return 0; }
C
// C program to explain the use // of continue statement #include <stdio.h> int main() { // loop from 1 to 10 for (int i = 1; i <= 10; i++) { // If i is equals to 6, // continue to next iteration // without printing if (i == 6) continue; else // otherwise print the value of i printf("%d ", i); } return 0; }
C++
// C++ program to explain the use // of continue statement #include <iostream> using namespace std; int main() { // loop from 1 to 10 for (int i = 1; i <= 10; i++) { // If i is equals to 6, // continue to next iteration // without printing if (i == 6) continue; else // otherwise print the value of i cout << i << " "; } return 0; }
C++
#include<iostream> using namespace std; int main(){ int gfg=0; // local variable for main cout<<"Before if-else block "<<gfg<<endl; if(1){ int gfg = 100; // new local variable of if block cout<<"if block "<<gfg<<endl; } cout<<"After if block "<<gfg<<endl; return 0; } /* Before if-else block 0 if block 100 After if block 0 */
C
#include <stdio.h> int main() { int gfg=0; // local variable for main printf("Before if-else block %d\n",gfg); if(1){ int gfg = 100; // new local variable of if block printf("if block %d\n",gfg); } printf("After if block %d",gfg); return 0; }
C
// C program to print numbers // from 1 to 10 using goto statement #include <stdio.h> // function to print numbers from 1 to 10 void printNumbers() { int n = 1; label: printf("%d ",n); n++; if (n <= 10) goto label; } // Driver program to test above function int main() { printNumbers(); return 0; }
C++
// C++ program to print numbers // from 1 to 10 using goto statement #include <iostream> using namespace std; // function to print numbers from 1 to 10 void printNumbers() { int n = 1; label: cout << n << " "; n++; if (n <= 10) goto label; } // Driver program to test above function int main() { printNumbers(); return 0; }
C
// C code to illustrate return // statement #include <stdio.h> // non-void return type // function to calculate sum int SUM(int a, int b) { int s1 = a + b; return s1; } // returns void // function to print void Print(int s2) { printf("The sum is %d", s2); return; } int main() { int num1 = 10; int num2 = 10; int sum_of = SUM(num1, num2); Print(sum_of); return 0; }
C++
// C++ code to illustrate return // statement #include <iostream> using namespace std; // non-void return type // function to calculate sum int SUM(int a, int b) { int s1 = a + b; return s1; } // returns void // function to print void Print(int s2) { cout << "The sum is "<< s2; return; } int main() { int num1 = 10; int num2 = 10; int sum_of = SUM(num1, num2); Print(sum_of); 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