I keep having a return if error and or should i use Switch statement?
Sub btnCalcBMI_Click
edtDisBMI.Text = ( edtb1.Text / (edtb2.Text * edtb2.Text))
If (edtb1.Text == "" Or edtb2.Text == "") Then
edtDisBMI.Text = "Enter the Values for Weight/Length"
Else
If edtDisBMI.Text <= 119 Then
edtDisplayBMI.Text = "Normal" & edtDisBMI.Text
If edtDisBMI.Text >=120 And edtDisBMI.Text <=139 Then
edtDisBMI.Text = "Prehypertension" & edtDisBMI.Text
If edtDisBMI.Text >=140 And edtDisBMI.Text <=159 Then
edtDisBMI.Text = "High Blood Pressure (Hypertension Stage 1)"& edtDisBMI.Text
If edtDisBMI.Text >=160 And edtDisBMI.Text <=179 Then
edtDisBMI.Text = "High Blood Pressure (Hypertension Stage 2)"& edtDisBMI.Text
If edtDisBMI.Text >= 180 Then
edtDisBMI.Text ="Hypertensive Crisis (EMERGENCY CARE NEEDED)"& edtDisBMI.Text
Seeing the indented code makes the issue stand out - you're missing a shipload of End Ifs to close off all those Ifs.
I'm guessing that most of the Ifs after the first couple should probably be Else Ifs.
B4X:
Sub btnCalcBMI_Click
edtDisBMI.Text = ( edtb1.Text / (edtb2.Text * edtb2.Text))
If (edtb1.Text == "" Or edtb2.Text == "") Then
edtDisBMI.Text = "Enter the Values for Weight/Length"
Else
If edtDisBMI.Text <= 119 Then
edtDisplayBMI.Text = "Normal" & edtDisBMI.Text
If edtDisBMI.Text >=120 And edtDisBMI.Text <=139 Then
edtDisBMI.Text = "Prehypertension" & edtDisBMI.Text
If edtDisBMI.Text >=140 And edtDisBMI.Text <=159 Then
edtDisBMI.Text = "High Blood Pressure (Hypertension Stage 1)"& edtDisBMI.Text
If edtDisBMI.Text >=160 And edtDisBMI.Text <=179 Then
edtDisBMI.Text = "High Blood Pressure (Hypertension Stage 2)"& edtDisBMI.Text
If edtDisBMI.Text >= 180 Then
edtDisBMI.Text ="Hypertensive Crisis (EMERGENCY CARE NEEDED)"& edtDisBMI.Text
End If
End Sub
Switches (or Select Cases) don't work here because in B4A you have to list each and every possible value explicitly, eg instead of VB/QB:
- don't use component properties as variables
- use numeric variables for numeric values eg Int (for whole numbers) or Float (for decimal-fraction numbers like 3.14)
- check the user input to make sure it's actually a number eg:
- watch out for divide-by-zero (although in B4X I think this doesn't actually cause your program to bomb out, so you've probably dodged the bullet without knowing it).
- watch out for gaps in ranges eg if you have 120-to-139 and 140-to-159, what happens between 139 and 140?
On the bright side, you're headed in the right direction, by starting off to get the basics working first, and leaving the glitz and glamour bits for later.
Sub btnCalcBMI_Click
If IsNumber(edtb1.Text) = False Then 'check user typed number
edtDisBMI.Text = "Please enter valid Weight"
Return 'no point in going any further, so abandon ship (ie exit this sub)
Else If IsNumber(edtb2.Text) = False Then 'check user typed number
edtDisBMI.Text = "Please enter valid Length"
Return 'abandon ship
End If
Dim Weight As Float = edtb1.Text 'because weight not just whole numbers eg could be 63.5 kg
Dim Length As Float = edtb2.Text
If Length < 1 Then 'to prevent divide-by-zero in BMI calculation
edtDisBMI.Text = "Length seems very short ? "
Return 'abandon ship
End If
Dim BMI As Float = Weight / (Length * Length)
edtDisBMI.Text = "The answer is " & BMI 'get the basics going first, worry about fancier words later
BMI = Round(BMI) 'round to nearest whole number eg 119.7 becomes 120
Dim BMIWords As String 'Dim once here, rather than many times below
If BMI <= 119 Then 'is there such a thing as too low? eg BMI = 10, is that good or bad?
BMIWords = "Normal"
Else If BMI <= 139 Then
BMIWords = "Prehypertension"
Else If BMI <= 159 Then
BMIWords = "High Blood Pressure (Hypertension Stage 1)" 'Blood Pressure? Isn't that different to BMI??
Else If BMI <= 179 Then
BMIWords = "High Blood Pressure (Hypertension Stage 2)"
Else 'if we get to here then BMI must be > 179
BMIWords = "Hypertensive Crisis (EMERGENCY CARE NEEDED)"
End If
edtDisBMI.Text = BMIWords & " " & Round(BMI) 'BMI already a whole number, but doing it again is cheap insurance