Recap - Simple Logic
Parte da seção Fundamentos do Journey de PHP da Coddy — lição 27 de 71.
Desafio
FácilLeia cinco valores da entrada: um inteiro age, um inteiro balance, uma string isStudent (seja "true" ou "false"), uma string hasPromoCode (seja "true" ou "false"), e um inteiro itemPrice.
Converta os valores de string para booleanos comparando-os com "true".
Avalie as seguintes três condições e imprima o resultado de cada uma usando var_dump(), cada uma em uma nova linha:
- Verifique se a pessoa qualifica para um desconto juvenil: age é menor que 25 E é estudante
- Verifique se a pessoa pode pagar pelo item com desconto: balance é maior ou igual a itemPrice OU (balance é pelo menos metade de itemPrice E hasPromoCode é true)
- Verifique se a pessoa recebe checkout prioritário: (age é pelo menos 65 OU isStudent é true) E balance é maior que 0 E NÃO hasPromoCode
Exemplo:
Se as entradas forem 22, 50, true, false, e 80, a saída deve ser:
bool(true)
bool(false)
bool(true)Explicação:
age < 25 && isStudent→ 22 < 25 && true → truebalance >= itemPrice || (balance >= itemPrice / 2 && hasPromoCode)→ 50 >= 80 || (50 >= 40 && false) → false || false → false(age >= 65 || isStudent) && balance > 0 && !hasPromoCode→ (false || true) && true && true → true
Experimente você mesmo
<?php
// Ler valores de entrada
$age = intval(fgets(STDIN));
$balance = intval(fgets(STDIN));
$isStudentStr = trim(fgets(STDIN));
$hasPromoCodeStr = trim(fgets(STDIN));
$itemPrice = intval(fgets(STDIN));
// Converter valores de string para booleanos
$isStudent = $isStudentStr === "true";
$hasPromoCode = $hasPromoCodeStr === "true";
// TODO: Escreva seu código abaixo
// 1. Verificar se a pessoa qualifica para um desconto de jovem
// 2. Verificar se a pessoa pode pagar pelo item com desconto
// 3. Verificar se a pessoa recebe checkout prioritário
?>Todas as lições de Fundamentos
4Comparison & Logical Operators
Comparison OperatorsEquality & IdentityLogical Operators Part 1Logical Operators Part 2Recap - Simple Logic2Variables and Data Types
NumbersStrings and QuotesBooleansNaming ConventionsRecap - Variable InitEmpty VariablesString ConcatenationGetting User InputCast to Different Types5Conditional Logic
If StatementIf - ElseThe Ternary OperatorNull Coalescing OperatorSwitch StatementRecap - Making Decisions3Basic Operators
Arithmetic OperatorsModulo OperatorExponentiation OperatorCombined AssignmentIncrement/DecrementOperator PrecedenceRecap - Simple CalculationsString Operators