From 97679fd4c20b773e76ff96399ba7887140c109c2 Mon Sep 17 00:00:00 2001 From: Bearz314 Date: Thu, 22 Nov 2018 02:15:21 +1100 Subject: [PATCH] Minor formatting change and added a note on == (#22378) * Minor formatting change and added a note on == * Code blocks are now styled as C-language syntax. * Added a note about the difference of `==` and `=` in the context of condition statements. * change the second #6 to 7 --- .../english/c/conditional-statements/index.md | 35 +++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/guide/english/c/conditional-statements/index.md b/guide/english/c/conditional-statements/index.md index d512cb1c61..d7f611cfa7 100644 --- a/guide/english/c/conditional-statements/index.md +++ b/guide/english/c/conditional-statements/index.md @@ -149,7 +149,7 @@ b is equal to 200 The switch statement is often faster than nested if...else (not always). Also, the syntax of switch statement is cleaner and easy to understand. ### Syntax of switch case -``` +```C switch (n) { case constant1: @@ -174,7 +174,7 @@ In the above pseudocode, suppose the value of n is equal to constant2. The compi The break statement is used to prevent the code running into the next case. ### Example: -``` +```C // Program to create a simple calculator // Performs addition, subtraction, multiplication or division depending the input from user @@ -229,14 +229,39 @@ Enter two operands: 32.5 The '-' operator entered by the user is stored in operator variable. And, two operands 32.5 and 12.4 are stored in variables firstNumber and secondNumber respectively. Then, control of the program jumps to -``` +```C printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber/firstNumber); ``` Finally, the break statement ends the switch statement. If break statement is not used, all cases after the correct case is executed. -## 6. Ternary operation +## 6. A note on equality `==` + +When doing a comparison for the `if` statement, be very careful to use the equality operator `==` and not an assignment operator `=`. If an assignment operator `=` is used, the variable is overwritten and a `True` is returned. Consider the following code: + +```C +#include + +int main(void) { + int x = 5; + if (x == 5) { + printf("1. x is %i\n",x); + } + + if (x = 4) { + printf("2. x is %i\n",x); + } +} +``` +### Output +``` +1. x is 5 +2. x is 4 +``` +As seen above, both `if` blocks are executed. In the second, the value of `x` is has been overwritten to `4`, which may not be what you want. + +## 7. Ternary operation The ternary operator (AKA conditional operator) is an operator that takes three arguments. The first argument is a comparison argument, the second is the result upon a true comparison , and the third is the result upon a flase comparison .It can be thought of as a shortened way of writing an if-else statement. It is often used to to assign variables based on the result of a comparison. #### Syntax @@ -258,5 +283,3 @@ printf("%d", a); https://www.dotnettricks.com/learn/c/conditional-statements-if-else-switch-ladder https://www.programiz.com/c-programming/c-if-else-statement http://www.tutorialspoint.com/ansi_c/c_control_statements.htm - -