From 946f53b8a056a6c4870861e9a39daf33695b3b80 Mon Sep 17 00:00:00 2001 From: blank10032 Date: Mon, 24 Dec 2018 09:35:11 +1100 Subject: [PATCH] Added another example using nested if statements (#24197) --- .../c/short-circuit-evaluation/index.md | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/guide/english/c/short-circuit-evaluation/index.md b/guide/english/c/short-circuit-evaluation/index.md index a43fd9f208..1edc39c5c4 100644 --- a/guide/english/c/short-circuit-evaluation/index.md +++ b/guide/english/c/short-circuit-evaluation/index.md @@ -35,6 +35,28 @@ The example above is equivalent to: } ``` + ## Example with nested if statements + + ```c + int i, j; + scanf ( "%d %d", &i, &j ); + if ( i > 10 && j > 10 ) { + printf("Both numbers are greater than 10! \n"); + } + ``` + The above example is equivalent to: + + ```c + int i, j; + scanf ( "%d %d", &i, &j ); + if ( i > 10 ) { + if ( j > 10 ) { + printf("Both numbers are greater than 10! \n"); + } + } + ``` + Notice when `if ( i > 10 )` fails, the statement is false and the check `if ( j > 10 )` is never run. `if ( i > 10 && j > 10 )` behaves exactly the same way, because if `i > 10` is false then the entire statement is automatically false, and there is no need to run an additional check. + ## Keep it all together in real example ```c