lundi 13 janvier 2020

Can you you use conditional statements in other conditional statements?

My question is simple, will the following code work, if it can't is there a way to accomplish the same effect

int day = 5; 
    String dayString; 

    switch (if (day > 0) { 
    case 1: 
        dayString = "Monday"; 
        day++;
        break; 
    case 2: 
        dayString = "Tuesday"; 
        day++;
        break; 
    case 3: 
        dayString = "Wednesday"; 
        day++;
        break; 
    case 4: 
        dayString = "Thursday"; 
        day++;
        break; 
    case 5: 
        dayString = "Friday"; 
        day++;
        break; 
    case 6: 
        dayString = "Saturday"; 
        day++;
        break; 
    case 7: 
        dayString = "Sunday"; 
        day++;
        break; 
    default: 
        dayString = "Invalid day"; 
        day++;
        break; 
    } 
    System.out.println(dayString); 

The output should be friday, basically my question is can you put if statements or while or for or do or other statements within the parameters of the respective statements.

Aucun commentaire:

Enregistrer un commentaire