logo

C++ si-sinon

En programmation C++, l'instruction if est utilisée pour tester la condition. Il existe différents types d’instructions if en C++.

carte dactylographiée
  • si déclaration
  • instruction if-else
  • instruction if imbriquée
  • échelle si-sinon-si

Instruction SI C++

L'instruction C++ if teste la condition. Il est exécuté si la condition est vraie.

 if(condition){ //code to be executed } 
Cpp Si sinon 1

C++ si exemple

 #include using namespace std; int main () { int num = 10; if (num % 2 == 0) { cout&lt;<'it is even number'; } return 0; < pre> <p>Output:/p&gt; <pre> It is even number </pre> <hr> </p><h2>C++ IF-else Statement</h2> <p>The C++ if-else statement also tests the condition. It executes if block if condition is true otherwise else block is executed.</p> <pre> if(condition){ //code if condition is true }else{ //code if condition is false } </pre> <img src="//techcodeview.com/img/c-tutorial/70/c-if-else-2.webp" alt="Cpp If else 2"> <hr> <h2>C++ If-else Example</h2> <pre> #include using namespace std; int main () { int num = 11; if (num % 2 == 0) { cout&lt;<'it is even number'; } else { cout<<'it odd return 0; < pre> <p> <strong>Output:</strong> </p> <pre> It is odd number </pre> <hr> <h2>C++ If-else Example: with input from user</h2> <pre> #include using namespace std; int main () { int num; cout&lt;&gt;num; if (num % 2 == 0) { cout&lt;<'it is even number'<<endl; } else { cout<<'it odd return 0; < pre> <p> <strong>Output:</strong> </p> <pre> Enter a number:11 It is odd number </pre> <p> <strong>Output:</strong> </p> <pre> Enter a number:12 It is even number </pre> <hr> <h2>C++ IF-else-if ladder Statement</h2> <p>The C++ if-else-if ladder statement executes one condition from multiple statements.</p> <pre> if(condition1){ //code to be executed if condition1 is true }else if(condition2){ //code to be executed if condition2 is true } else if(condition3){ //code to be executed if condition3 is true } ... else{ //code to be executed if all the conditions are false } </pre> <img src="//techcodeview.com/img/c-tutorial/70/c-if-else-3.webp" alt="Cpp If else 3"> <hr> <h2>C++ If else-if Example</h2> <pre> #include using namespace std; int main () { int num; cout&lt;&gt;num; if (num 100) { cout<= 0 && num < 50){ cout<="50" 60) { 70) 80) 90) cout<<'a+ grade'; } pre> <p> <strong>Output:</strong> </p> <pre> Enter a number to check grade:66 C Grade </pre> <p> <strong>Output:</strong> </p> <pre> Enter a number to check grade:-2 wrong number </pre> <hr></=></pre></'it></pre></'it></pre></'it>

Instruction C++ IF-else

L'instruction if-else C++ teste également la condition. Il s'exécute si le bloc si la condition est vraie, sinon le bloc est exécuté.

modèles d'apprentissage automatique
 if(condition){ //code if condition is true }else{ //code if condition is false } 
Cpp Si sinon 2

Exemple C++ If-else

 #include using namespace std; int main () { int num = 11; if (num % 2 == 0) { cout&lt;<\'it is even number\'; } else { cout<<\'it odd return 0; < pre> <p> <strong>Output:</strong> </p> <pre> It is odd number </pre> <hr> <h2>C++ If-else Example: with input from user</h2> <pre> #include using namespace std; int main () { int num; cout&lt;&gt;num; if (num % 2 == 0) { cout&lt;<\'it is even number\'<<endl; } else { cout<<\'it odd return 0; < pre> <p> <strong>Output:</strong> </p> <pre> Enter a number:11 It is odd number </pre> <p> <strong>Output:</strong> </p> <pre> Enter a number:12 It is even number </pre> <hr> <h2>C++ IF-else-if ladder Statement</h2> <p>The C++ if-else-if ladder statement executes one condition from multiple statements.</p> <pre> if(condition1){ //code to be executed if condition1 is true }else if(condition2){ //code to be executed if condition2 is true } else if(condition3){ //code to be executed if condition3 is true } ... else{ //code to be executed if all the conditions are false } </pre> <img src="//techcodeview.com/img/c-tutorial/70/c-if-else-3.webp" alt="Cpp If else 3"> <hr> <h2>C++ If else-if Example</h2> <pre> #include using namespace std; int main () { int num; cout&lt;&gt;num; if (num 100) { cout<= 0 && num < 50){ cout<="50" 60) { 70) 80) 90) cout<<\'a+ grade\'; } pre> <p> <strong>Output:</strong> </p> <pre> Enter a number to check grade:66 C Grade </pre> <p> <strong>Output:</strong> </p> <pre> Enter a number to check grade:-2 wrong number </pre> <hr></=></pre></\'it></pre></\'it>

Exemple C++ If-else : avec entrée de l'utilisateur

 #include using namespace std; int main () { int num; cout&lt;&gt;num; if (num % 2 == 0) { cout&lt;<\'it is even number\'<<endl; } else { cout<<\'it odd return 0; < pre> <p> <strong>Output:</strong> </p> <pre> Enter a number:11 It is odd number </pre> <p> <strong>Output:</strong> </p> <pre> Enter a number:12 It is even number </pre> <hr> <h2>C++ IF-else-if ladder Statement</h2> <p>The C++ if-else-if ladder statement executes one condition from multiple statements.</p> <pre> if(condition1){ //code to be executed if condition1 is true }else if(condition2){ //code to be executed if condition2 is true } else if(condition3){ //code to be executed if condition3 is true } ... else{ //code to be executed if all the conditions are false } </pre> <img src="//techcodeview.com/img/c-tutorial/70/c-if-else-3.webp" alt="Cpp If else 3"> <hr> <h2>C++ If else-if Example</h2> <pre> #include using namespace std; int main () { int num; cout&lt;&gt;num; if (num 100) { cout<= 0 && num < 50){ cout<="50" 60) { 70) 80) 90) cout<<\'a+ grade\'; } pre> <p> <strong>Output:</strong> </p> <pre> Enter a number to check grade:66 C Grade </pre> <p> <strong>Output:</strong> </p> <pre> Enter a number to check grade:-2 wrong number </pre> <hr></=></pre></\'it>

Sortir:

 Enter a number:12 It is even number 

Instruction d'échelle C++ IF-else-if

L’instruction à relais if-else-if C++ exécute une condition à partir de plusieurs instructions.

 if(condition1){ //code to be executed if condition1 is true }else if(condition2){ //code to be executed if condition2 is true } else if(condition3){ //code to be executed if condition3 is true } ... else{ //code to be executed if all the conditions are false } 
Cpp Si sinon 3

C++ If else-if Exemple

 #include using namespace std; int main () { int num; cout&lt;&gt;num; if (num 100) { cout<= 0 && num < 50){ cout<="50" 60) { 70) 80) 90) cout<<\'a+ grade\'; } pre> <p> <strong>Output:</strong> </p> <pre> Enter a number to check grade:66 C Grade </pre> <p> <strong>Output:</strong> </p> <pre> Enter a number to check grade:-2 wrong number </pre> <hr></=>

Sortir:

 Enter a number to check grade:-2 wrong number