locked
Help with Lesson 1.4 in SB Conditions and Loops RRS feed

  • Question

  • I'm very new to programming and thus, trying to learn Small Basic. I apologise if this was answered before but could not find a proper explanation to my problem.

    In lesson 1.4 in the curriculum, at the end of the page it says "Show what you know" .. I know the questions asked has some repeated mistakes

    "

    Create a program to convert one or more student scores from a percentage to a letter grade. First, ask the user to specify how many grades will be calculated. Then ask the user to specify the first percentage, and convert it to a letter grade based on the following criteria:

    • If the percentage is less than 75 but more than or equal to 60, convert it to a
    • If the percentage is less than 60 but more than or equal to 35, convert it to a
    • If the percentage is less than 35, convert it to a D.
    • Then ask the user to specify the first percentage, and convert it to a letter grade based on the following criteria:

    the problem is with the answer page, which is this:

    "

    TextWindow.Write("How many student grades would you like to calculate? ")

    number = TextWindow.Read()

    n = 1

    While n <= number

      TextWindow.Write("What is the student’s percentage? ")

      percentage = TextWindow.Read()

      If  percentage >= 75 Then

        TextWindow.WriteLine("The student’s grade is A.")

      ElseIf  percentage < 75 And percentage >= 60  Then

        TextWindow.WriteLine("The student’s grade is B.")

      ElseIf  percentage < 60 And percentage >= 35 Then

        TextWindow.WriteLine("The student’s grade is C.")

      Else

        TextWindow.WriteLine("The student’s grade is D.")

      EndIf

      n = n + 1

    EndWhile

    "

    the command or the statement "n" in this program.. I did not understand why the "n" is used "n = 1" .. can someone please explain to me why this was done? I already know all the other commands but got stuck and cant figure out why "n" was used here.

    PS: is there another way of doing it other than the answer key?

    Sunday, March 11, 2012 6:30 PM

Answers

  • Hi,

    the n inside the code is just used as a counter variable. In the code, the user should specify, how many student grades he want to calculate. So we have to count how many grades are calculated.

    So n is set to 1 because the first student grade should ba calculated. Then it is checked, that it is still lower or equal to the number entered by the user. So in case the user entered 0, then no student grade is calculated.

    At the end of the while loop, n is increased by one.

    And of course: There are multiple ways to implement it. One way could be to use a for loop instead of a while loop. Or what you could also do is: You take the number entered as the number of grades that still has to be calculated. That would change the code a little bit:
    - n = 1 line is removed
    - while number > 0
    - and instead of n = n + 1 we will get a number = number -1
    So the new logic would be: The while loop would be: While we have to calculate more StudentGrades and at the end we reduce the number of grades to calculate by 1 because we just calculated one.

    With kind regards,

    Konrad

    • Marked as answer by Abdul_ASH Sunday, March 11, 2012 6:58 PM
    Sunday, March 11, 2012 6:53 PM

All replies

  • Hi,

    the n inside the code is just used as a counter variable. In the code, the user should specify, how many student grades he want to calculate. So we have to count how many grades are calculated.

    So n is set to 1 because the first student grade should ba calculated. Then it is checked, that it is still lower or equal to the number entered by the user. So in case the user entered 0, then no student grade is calculated.

    At the end of the while loop, n is increased by one.

    And of course: There are multiple ways to implement it. One way could be to use a for loop instead of a while loop. Or what you could also do is: You take the number entered as the number of grades that still has to be calculated. That would change the code a little bit:
    - n = 1 line is removed
    - while number > 0
    - and instead of n = n + 1 we will get a number = number -1
    So the new logic would be: The while loop would be: While we have to calculate more StudentGrades and at the end we reduce the number of grades to calculate by 1 because we just calculated one.

    With kind regards,

    Konrad

    • Marked as answer by Abdul_ASH Sunday, March 11, 2012 6:58 PM
    Sunday, March 11, 2012 6:53 PM
  • Awesome Thanks! that is exactly what I needed!

    thanks for clearing that out, now it feels so obvious !

    Sunday, March 11, 2012 6:58 PM
  • I am glad that I was able to help. You are always welcome with all your questions!

    With kind regards,

    Konrad

    Sunday, March 11, 2012 7:07 PM