5.2 Fall-Through Behavior

Remember the break statements that we discussed in the previous section? Having a break statement at the end of each case stops the computer from reading and executing every case. Let’s look at the switch statement from the previous section again, this time with a few modifications:
int age = 3; switch(age) { case 1: printf(“Your age is 1”); break; case 2: printf(“Your age is 2”); break; case 3: printf(“Your age is 3”); break; case 4: printf(“Your age is 4”); break; case 5: printf(“Your age is 5”); break; default: printf(“You are older than 5”); break; }

Previous Section

Next Section

Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.