Presentation is loading. Please wait.

Presentation is loading. Please wait.

More on conditional statements

Similar presentations


Presentation on theme: "More on conditional statements"— Presentation transcript:

1 More on conditional statements

2 If-else in UML 2018 Risto Heinsar

3 If-else if-else in UML 2018 Risto Heinsar

4 Logical operators Operator Operation Result will be true when … &&
Logical AND Conjunction … both sides are true || Logical OR Disjunction ... at least one side is true ! Logical negation Inversion ... initial condition is false 2018 Risto Heinsar

5 Truth table AND OR NOT a b a && b 1 a b a || b 1 a !a 1 2018
1 a b a || b 1 a !a 1 2018 Risto Heinsar

6 Examples if (sorted == 0) if (!sorted)
if (input >= MIN && input <= MAX) if (input < MIN || input > MAX) if ((input >= MIN && input <= MAX) ||(ignoreLim == 1)) if (input == 0 || input == 1 || input == 15) 2018 Risto Heinsar

7 De Morgan’s law Useful laws of transformation for Boolean algebra
𝐴∨𝐵 ⟺ 𝐴 ∧ 𝐵 𝐴∧𝐵 ⟺ 𝐴 ∨ 𝐵 Can be extended The following equations have the same result The condition is true when the variable value is neither 1 or 2 if (value != 1 && value != 2) if (!(value == 1 || value == 2)) 2018 Risto Heinsar

8 Switch statement Uses an expression instead of a condition
Tries to find a matching case When no cases match, default case is looked for If a case matches, it will run all statements until either switch ends or break/continue statement is encountered! This includes other cases below the matching one! Default case, break and continue statements are optional but usually desired 2018 Risto Heinsar

9 The code switch () if / else if / else switch (expression) {
case constant or expression: statements break; default: } if (condition) { statements } else if (condition) else 2018 Risto Heinsar

10 Conditionals in UML 2018 Risto Heinsar

11 Examples of switch switch (value) { case 1: case 2: case 3: printf("Value is either 1, 2 or 3\n"); break; default: printf("Value not identified\n"); } switch (value) { case 1: printf("Value is 1\n"); break; case 2: printf("Value is 2\n"); default: printf("Value not identified\n"); } 2018 Risto Heinsar

12 Create an algorithm for a program…
… that mimics a typical food scale in the supermarket Get the weight of the product (from user input) Get the type of the product (e.g. banana, cucumber, …) (from user input) Price will be calculated based on the weight and the product type The cost will be outputted along with the product type Initially, a menu of possible food items is shown An error is printed when an unknown product number is chosen. An error is printed when the cost will calculate to 0 2018 Risto Heinsar

13 Create a product that would emulate a food scale in a supermarket
#include <stdio.h> int main(void) { float weight; int productCode; printf(„Insert the weight of the product\n> „); scanf("%f", weight); switch (productCode) case 0: printf(„Product %d – Banana\n“, select); price = 1.10; break; default: price = 0; } cost = price * weight; printf(„Cost: %f\n“, cost); return 0; Create a product that would emulate a food scale in a supermarket 5 products (4 of them create by yourself) Show the list of available products to the user User must be able to input the product weight and type Verify that all of the variables needed are declared Create an exception when cost will be 0 2018 Risto Heinsar


Download ppt "More on conditional statements"

Similar presentations


Ads by Google