Java programming for beginners - Lesson 4 - Switch StatementssteemCreated with Sketch.

in #programming7 years ago

Hello everyone, welcome to the fifth lesson in my Java programming for beginners tutorials series. The fourth lesson(lesson - 3) - IF Statements can be found at - https://steemit.com/programming/@robertlyon/java-programming-for-beginners-lesson-2-if-statements

This is going to be a short lesson that will supplement the previous lesson I wrote on IF statements. In this lesson i am going to cover Switch Statements, These statements are similar to if statements and give you more tools for making decisions in your programs.

Switch Statements

What are they?

A switch statement is a type is a way to make decisions in your code, the switch statement makes its decisions based on a "case"(this will become apparent when you see some code). The case can be one of the following primitive data types,
char, byte, short or int. In the next section I will show you what the syntax of the Switch statement looks like and then we can run through what each individual part of the statement does.

Structure of a Switch statement.

public class SwitchStatements {
    public static void main(String[] args)
    {
        int number = 1;
        switch(number)
        {
            case 1:
                System.out.println("One");
                break;
            case 2:
                System.out.println("Two");
                break;
            case 3:
                System.out.println("Three");
                break;
            default:
                System.out.println("number not recognised");
                break;
        }

    }

}

So as we can see in this code example I have created an integer variable named number. This variable is only here to prove the switch statement works and is not actually part of the statement.
To start the Switch statement you have to pass in a variable that corresponds to [one of the primitive values listed above. This value will correspond to a whole number.

Moving on the line "case 1". This corresponds to the case where the state of the variable number is equal to the number 1. If you look at "case 2" and "case 3". These both correspond to the case where the state of the number variable is 2 or 3 respectively.

In this example, the number variable holds the value 1. Because of this when the Switch statement is executed the program enters "case 1". This causes the String "one" to be printed to the console. If the number variable holds the value 2 then the program would skip over "case 1" and enter "case 2".

The statement is an important part of the statement. It is there to make sure that the program exits the Switch statement at the correct time. If the break statement wasn't included in any of the cases then the program would enter "case 1", execute System.out.println("One"); then it would execute "case 2" and execute the System.out.println("Two") and finally it would do the same to "case 3".
So as you can see the break statement exits the program at the appropriate time.

The last this I need to mention about Switch statements in this lesson is the "default" case. This case can be thought of like the else statement in an if/else block. This means that if there are no cases that match the value that is held in the variable number, then the "default" case will be executed.

Conclusion

In this lesson, I have covered the basics of Switch statements. examples of using these statements are choosing a day of the week based on numbers or finding a month of the year from the number. Hopefully, you can understand the information presented here, if not leave a comment and I can clarify.

Next Lesson

In the next lesson, I will be covering repetition and will be focusing on for loops and while loops.

If there is anything in this lesson that confuses you or there is anything programming related that you need help with then please comment below and I will try my best to help you.

As always if there are any improvements you think I can make to this post then please leave a comment and I will consider adding it.

Thank you for reading and I hope that someone will get some use out of these tutorials.

Message to readers
Thanks for taking the time to read my post, if you are interested in Science, Technology or Computer Science then check out my blog, content is a little sparse at the moment but I am making an effort to provide good quality original content to the Steemit community.

Sort:  

Are you on Discord or Steemit Chat ?

My Discord is Antriark#4291

i cant find you maybe you can DM:

https://discord.gg/RMXbqmG

This post received a 78% upvote from @morwhale team thanks to @robertlyon! For more information, click here! , TeamMorocco! .

Resteemed by @resteembot! Good Luck!
Curious?
The @resteembot's introduction post
The @reblogger's introduction post
Get more from @resteembot with the #resteembotsentme initiative
Check out the great posts I already resteemed.

This post was resteemed by @steemvote

This post has received a 5.28 % upvote from @buildawhale thanks to: @robertlyon. Send at least 1 SBD to @buildawhale with a post link in the memo field for a portion of the next vote.

To support our daily curation initiative, please vote on my owner, @themarkymark, as a Steem Witness

This post has received gratitude of 3.75 % from @appreciator thanks to: @robertlyon.

Coin Marketplace

STEEM 0.27
TRX 0.11
JST 0.030
BTC 68918.37
ETH 3764.25
USDT 1.00
SBD 3.43