Postagens

Mostrando postagens com o rótulo Switch

Course C Initial 12nd – Exercises with Switch

Imagem
Now, do these Algorithms using switch command. 1- Make an algorithm that works as a game, it may ask to user “What’s United Kingdom Capital?” so the user will have available 4 possibility, they’re respectively a) Manchester, b) Newcastle, c) London, d) Liverpool, after will be tested them. Note: use for store the result, a character type variable. Case ‘c’, so printf (“You’re Wright”); default, so printf (“You’re wrong”); 2- Make an algorithm that reserves the product code and the quantity, so calculate the prices. Note: before defines how you can see below: ‘ M’ – Milk – U$ 1,00 1L. ‘ P’ – Potato – U$ 2,50 Kg. ‘ E’ – Meat – U$ 10,00 Kg. ‘ J’ – Juice – U$ 0,50 each. Use switch structure, for invalid values, use default to show a note at display. After multiple the price by quantity and print the total price. 3- Make an algorithm that works how a calculator. Use a char variable to store the operator and so make the operation and show...

Course C Initial 11st – Switch Commands

Imagem
Switch is other way to selections commands, it test a variable integer or character, (it cans not use a float or double for example), with cases. See below its structure: Read the image. A switch command is use when we have a finite number of possibilities. In C, we can use switch how many time we desire. Other point is which switch allows breaks in the code. So see below breaks being running. As we can see, the case can use with breaks, since 1 to 7 for example. But it’s limited. Note: in Switch Command, we can’t test conditions as in if-else, so the form case 2<4 is invalid to switch. In case, never forget to put break; it’s the signal to the compiler that what’s inside case, had finished. In default, it’s not required. Now, see an alternative structure to switch command: That works, but it isn’t required. So I prefer use the previous way. Note: Switch and If Else can be interconnected, but be careful with this. I...