La structure de choix
CHAPITRE 4
68
C#
using System ;
class Facture
{ static void Main ()
{ double montant, taux, remise ;
System.Console.Write ("donnez le montant brut : ") ;
String ligne ;
ligne = Console.ReadLine() ;
montant = Double.Parse (ligne) ;
if (montant < 2000.0)
taux = 0.0 ;
else if (montant <= 5000.0) taux = 1.0 ;
else
taux = 2.0 ;
remise = montant * taux/100.0 ;
montant = montant - remise ;
System.Console.WriteLine ("montant après remise : " + montant) ;
}
}
donnez le montant brut : 3000
montant après remise : 2970
La notation 1. n’est pas valide pour des constantes réelles. Il faut utiliser 1.0. De plus, comme en Java,
ces constantes sont de type double. Si taux était de type float, l’affectation
taux = 1.
serait interdite car elle demanderait une conversion implicite du type double vers le type float.
PHP
$montant = 3000.0 ;
echo "montant brut : ", $montant, "
" ;
if ($montant < 2000.)
$taux = 0. ;
else if ($montant <= 5000.) $taux = 1. ;
else
$taux = 2. ;
$remise = $montant * $taux/100. ;
$montant = $montant - $remise ;
echo "montant après remise : ", $montant ;
?>
montant brut : 3000
montant après remise : 2970
CHAPITRE 4
68
C#
using System ;
class Facture
{ static void Main ()
{ double montant, taux, remise ;
System.Console.Write ("donnez le montant brut : ") ;
String ligne ;
ligne = Console.ReadLine() ;
montant = Double.Parse (ligne) ;
if (montant < 2000.0)
taux = 0.0 ;
else if (montant <= 5000.0) taux = 1.0 ;
else
taux = 2.0 ;
remise = montant * taux/100.0 ;
montant = montant - remise ;
System.Console.WriteLine ("montant après remise : " + montant) ;
}
}
donnez le montant brut : 3000
montant après remise : 2970
La notation 1. n’est pas valide pour des constantes réelles. Il faut utiliser 1.0. De plus, comme en Java,
ces constantes sont de type double. Si taux était de type float, l’affectation
taux = 1.
serait interdite car elle demanderait une conversion implicite du type double vers le type float.
PHP
$montant = 3000.0 ;
echo "montant brut : ", $montant, "
" ;
if ($montant < 2000.)
$taux = 0. ;
else if ($montant <= 5000.) $taux = 1. ;
else
$taux = 2. ;
$remise = $montant * $taux/100. ;
$montant = $montant - $remise ;
echo "montant après remise : ", $montant ;
?>
montant brut : 3000
montant après remise : 2970
