Skip to content

Latest commit

 

History

History
101 lines (95 loc) · 3.68 KB

17.5 - switch.md

File metadata and controls

101 lines (95 loc) · 3.68 KB

Intro

I wrote a biography on Preston with programming. Here's the biography:

var page = 0;

background(255, 255, 255);
fill(0, 0, 0);
textSize(16);
text("Once upon a time there was a majestic Preston", 10, 200);
text("who lived in the land of Prestonia.", 10, 220);

mousePressed = function(){
    background(255, 255, 255);
    page ++; //go to the next page. Works the same as page += 1; or page = page + 1;
    if(page === 1){
        text("One day, there came a man named 'Confusious'.", 10, 200);
        text("Confusious brought chaos and ruin upon Prestonia.", 10, 220);
    }else if(page === 2){
        text("Determined to bring Prestonia back to how it was,", 10, 200);
        text("Preston traveled far and wide to find a place of order", 10, 220);
        text("and reason.", 10, 240);
    }else if(page === 3){
        text("That is when he found Vexcess Academy, an academy", 10, 200);
        text("that taught programming, an ancient art form that", 10, 220);
        text("brings order through the power of computers.", 10, 240);
    }else{
        text("To this day, Preston is learning programming so he", 10, 200);
        text("may defeat the terrible Confusious and restore order", 10, 220);
        text("to Prestonia.", 10, 240);
    }
};

I might do a little revising and publish it one day. But hmm, look at the if statements. It's so repetative. Every if statement is asking which page we're on. There's gotta be a better way to write this

Switch Statements

A switch is a way of writing lengthy and repetative if statements like the ones I have. Here's how you use a switch statement:

var daysSinceSunday = 3;

fill(0, 0, 0);
switch(daysSinceSunday){
    //in the case that daysSinceSunday is 0...
    case 0:
        text("It's Sunday", 20, 200); //run this code
    break; //the break means to ignore everything else in the switch, because we only want one of these to run.
    case 1:
        text("It's Monday (back to school)", 20, 200);
    break;
    case 2:
        text("It's Tuesday", 20, 200);
    break;
    case 3:
        //since daysSinceSunday is 3, this one will run.
        text("It's Wednesday", 20, 200);
    break;
    case 4:
        text("It's Thursday", 20, 200);
    break;
    case 5:
        text("It's Friday", 20, 200);
    break;
    default: //if none of the other cases run, the default one will. You don't need to add a default statement if you don't need one.
        text("It's Saturday (Wooo!)", 20, 200);
}

We can use that for the biography:

var page = 0;

background(255, 255, 255);
fill(0, 0, 0);
textSize(16);
text("Once upon a time there was a majestic Preston", 10, 200);
text("who lived in the land of Prestonia.", 10, 220);

mousePressed = function(){
    background(255, 255, 255);
    page ++; //go to the next page
    switch(page){
        case 1:
            text("One day, there came a man named 'Confusious'.", 10, 200);
            text("Confusious brought chaos and ruin upon Prestonia.", 10, 220);
        break;
        case 2:
            text("Determined to bring Prestonia back to how it was,", 10, 200);
            text("Preston traveled far and wide to find a place of order", 10, 220);
            text("and reason.", 10, 240);
        break;
        case 3:
            text("Determined to bring Prestonia back to how it was,", 10, 200);
            text("Preston traveled far and wide to find a place of order", 10, 220);
            text("and reason.", 10, 240);
        break;
        default:
            text("To this day, Preston is learning programming so he", 10, 200);
            text("may defeat the terrible Confusious and restore order", 10, 220);
            text("to Prestonia.", 10, 240);
    }
};

Looks much neater if you ask me.