ComeçarComece de graça

Can you If inside an If?

Sometimes it makes sense to have nested if statements to add even more control. In the following exercise, you will add an if statement that checks if you are holding a share of the Microsoft stock before you attempt to sell it.

Here is the structure of nested if statements, it should look somewhat familiar:

if(condition1) {        
    if(condition2) {     
        code if both pass
    } else {            
        code if 1 passes, 2 fails
    }
} else {            
    code if 1 fails
}

The variables, micr and shares, have been created for you.

Este exercício faz parte do curso

Intermediate R for Finance

Ver curso

Instruções do exercício

  • Fill in the nested if statement to check if shares is greater than or equal to 1 before you decide to sell.
  • If this is true, then print "Sell!".
  • Else, print "Not enough shares to sell!".

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

micr <- 105.67
shares <- 1

# Fill in the blanks
if( micr < 55 ) {
    print("Buy!")
} else if( micr >= 55 & micr < 75 ) {
    print("Do nothing!")
} else { 
    if( ___ ) {
        ___
    } else {
        ___
    }
}
Editar e executar o código