Switch Statements in Dart

in CCS2 days ago

Hello everyone!

switch statement in dart.png

Designed with Canva

Today I am here to share the concept of the switch statement in the dart programming language. This is similar to the if-else statement. Actually switch statement helps us to analyze different cases of a problem.

We can define specific conditions depending upon the situation or condition of the case. The case is passed to the block of code and it checks with which solution this case matches and then it runs that specific block of code.

In the switch statement when a specific condition is met the program is terminated using the break statement.


image.png

Look at this simple program and let us try to understand the concept of switch statement. This simple program is working as a calculator. I am getting choice from the user whether the user wants addition, subtraction, multiplication, or division of two numbers.


image.png

At the start I am getting two numbers from the user and then I am getting a choice number from the user to execute the specific block of code.


image.png

Here the game begins of the switch statement. At first the choice number is passed to the switch statement. Then I have defined 4 cases in this. Each case is performing a specific task.

  • If the user presses 1 then addition case will be executed.

  • If the user presses 2 then the subtraction case will be executed.

  • If the user presses 3 then the multiplication case will be executed.

  • If the user presses 4 then the division case will be executed.

As soon as a case is matched with the choice number then the program is terminated because of the break statement. This is how the switch statement works in the dart programming.


image.png

At the end I have added a default case which will handle the invalid choice number. If the user enters any other number except the 1-4 then it will be executed showing the error of invalid number.

Posted using SteemX

Sort:  

🎉 Congratulations!

Your post has been upvoted by the SteemX Team! 🚀

SteemX is a modern, user-friendly and powerful platform built for the Steem community.

🔗 Visit us: www.steemx.org

✅ Support our work — Vote for our witness: bountyking5

banner.jpg

 2 days ago 

🎉 Congratulations, @mohammadfaisal!

Your post Switch Statements in Dart was rewarded by CCS Curation Trail

CCS Logo

“Home is where your heart is ❤️.”

👉 Join the CCS Curation Trail | Community Invitation

Vote Banner

Vote for visionaer3003 as witness