Presentation is loading. Please wait.

Presentation is loading. Please wait.

More on conditional statements. Conditionals In some situations the typical if-else statements may become cumbersome Depending on the situation, there.

Similar presentations


Presentation on theme: "More on conditional statements. Conditionals In some situations the typical if-else statements may become cumbersome Depending on the situation, there."— Presentation transcript:

1 More on conditional statements

2 Conditionals In some situations the typical if-else statements may become cumbersome Depending on the situation, there are 2 possible constructs that may become useful switch () If / else if / else 20152

3 Conditionals in UML 20153

4 The code switch () switch (expression) { case constant or expression: statements break; case constant or expression: statements break; default: statements break; } if / else if (condition) { statements } else if (condition) { statements } else { statements } 20154

5 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 An error is printed when an unknown product number is chosen. An error is printed when the cost will calculate to 0 20155

6 #include int main(void) { float weight; int product; printf(„Insert the weight of the product\n> „); scanf("%f", weight); switch (product) { case 0: printf(„Product %d – Banana\n“, valik); hind = 1.28; break; default: hind = 0; break; } cost = price * weight; printf(„Cost: %f“, cost); return 0; } 20156 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


Download ppt "More on conditional statements. Conditionals In some situations the typical if-else statements may become cumbersome Depending on the situation, there."

Similar presentations


Ads by Google