FRM Member Oct 14, 2022 #1 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 End If End Sub
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 End If End Sub
Erel B4X founder Staff member Licensed User Longtime User Oct 14, 2022 #2 Please use [code]code here...[/code] tags when posting code. Upvote 0
E emexes Expert Licensed User Longtime User Oct 14, 2022 #3 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: Copy 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: B4X: Copy Case 120 to 139 you'd have to write: B4X: Copy Case 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139 which is a bit tedious and error-prone. Last edited: Oct 14, 2022 Upvote 0
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: Copy 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: B4X: Copy Case 120 to 139 you'd have to write: B4X: Copy Case 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139 which is a bit tedious and error-prone.
E emexes Expert Licensed User Longtime User Oct 14, 2022 #4 A few other tips are: - 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. Last edited: Oct 14, 2022 Upvote 0
A few other tips are: - 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.
E emexes Expert Licensed User Longtime User Oct 14, 2022 #5 If it were me I'd start with something like: B4X: Copy 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 Last edited: Oct 14, 2022 Upvote 0
If it were me I'd start with something like: B4X: Copy 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
E emexes Expert Licensed User Longtime User Oct 14, 2022 #6 and then the BMI classification would look like: B4X: Copy 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 Last edited: Oct 14, 2022 Upvote 0
and then the BMI classification would look like: B4X: Copy 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
E emexes Expert Licensed User Longtime User Oct 14, 2022 #7 ps I only wrote the above because you were stuck after having clearly already given it a red-hot go, unlike your compatriot at: https://www.b4x.com/android/forum/threads/b4a-assignment.143509/#post-909473 ? Last edited: Oct 14, 2022 Upvote 0
ps I only wrote the above because you were stuck after having clearly already given it a red-hot go, unlike your compatriot at: https://www.b4x.com/android/forum/threads/b4a-assignment.143509/#post-909473 ?
FRM Member Oct 15, 2022 #9 emexes said: ps I only wrote the above because you were stuck after having clearly already given it a red-hot go, unlike your compatriot at: https://www.b4x.com/android/forum/threads/b4a-assignment.143509/#post-909473 ? Click to expand... Hahahahahaha He just posted the whole damn assignment LOL Upvote 0
emexes said: ps I only wrote the above because you were stuck after having clearly already given it a red-hot go, unlike your compatriot at: https://www.b4x.com/android/forum/threads/b4a-assignment.143509/#post-909473 ? Click to expand... Hahahahahaha He just posted the whole damn assignment LOL