From ab37bb5080b5718186a05ef1f79a904ad9dfe24a Mon Sep 17 00:00:00 2001 From: AkshitAggarwal <40717067+AkshitAggarwal@users.noreply.github.com> Date: Tue, 16 Oct 2018 02:03:24 +0530 Subject: [PATCH] Updated more than two conditions part. (#18868) The tutorial seemed to miss the part that a cpp code can also take more than one conditions and also that else{...} will always run if conditions of if and else if are not true, hence it can also be skipped if necessary. --- .../cplusplus/conditional-operator/index.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/client/src/pages/guide/english/cplusplus/conditional-operator/index.md b/client/src/pages/guide/english/cplusplus/conditional-operator/index.md index 4e2d9ce15d..135383a3c2 100644 --- a/client/src/pages/guide/english/cplusplus/conditional-operator/index.md +++ b/client/src/pages/guide/english/cplusplus/conditional-operator/index.md @@ -24,6 +24,24 @@ else expression-2; } ``` + +The above code however can be used to check more than just two conditions in the following way/syntax: +```cpp +if(condition 1) /* Checks first condition, skips else-if and else + { entirely if condition 1 checks out */ + expression-1; + } +else if(condition 2) /*Checks condition 2, only and only if condition 1 + { is false, if condition 2 is true the comiler will + expression-2; skip the 'else' part and move on */ + } + +else /*Once if and else-if conditions don't satisfy the + { compiler will run the expression in else{..} + expression-3; because it does not have a condition to check, + } part of code can also be skipped if not necessary*/ +``` + Hence conditional operator is very handy when you need to write simple if-else statement. It can also be used in #define preprocessor when similar condition is to be used in multiple places.