I need some HELP with For Loop

mr06

Member
Joined
May 1, 2006
Messages
12
Programming Experience
Beginner
Im a kind of lost with this question and i need some1 to help me go thru it and it is as follows...

A program that uses a for loop to frst take in names and total grades of 5 students.

Your output should look like this:
What is the name of student 1 = ______
Total Mark? ____
The program computes the average of all the total marks and prints out (on screen) the following output.

What is the name of student 1 = Peter
Total Mark? 100
What is the name of student 2 = John
Total Mark? 80
What is the name of student 3 = William
Total Mark? 10
What is the name of student 4 = Kim
Total Mark? 12
What is the name of student 5 = Linda
Total Mark? 0
-------------------------------------------
Peter 100 A
John 90 B
William 10 F
Kim 12 F
Linda 0 F

Average of the class is: 42.4
 
What do u mean u help me and i will help you? Im stuck man this is an assignment due tommorow im really screwed either i do it or ill lose marks..So plz bear with me here and figure it out

or any1 kind enuff that would offer to help would really appreciate it.
 
Mine is for tomorow aswell, i'm pretty screwed too, if i fail this then i have to resit the whole course
 
VB.NET:
Sub main()
       Dim names(3) As String
        Dim marks(3) As Single
        Dim average As Single
        Dim total As Single = 0
        For i As Integer = 0 To 3
            Console.WriteLine("Enter name = ")
            names(i) = Console.ReadLine()
            Console.WriteLine("Enter marks = ")
            marks(i) = Console.ReadLine()
        Next

        For i As Integer = 0 To 3
            total += marks(i)
        Next
        average = total / 4.0
        Console.WriteLine("Average of the class is " & average)
        Console.ReadLine()
end sub
 
You would also need something to declare a string for the mark, use a select case or if then...
VB.NET:
[SIZE=2][COLOR=#000000]imports system[/COLOR][/SIZE]
[SIZE=2][COLOR=#000000]class students[/COLOR][/SIZE]
[SIZE=2][COLOR=#000000]public shared names(5) as string
    Public Shared grades(5) As String[/COLOR][/SIZE]
[SIZE=2][COLOR=#000000]
 public shared sub main()
        Dim i As Integer
        Dim x As Integer
        Dim j As Integer
        Dim intaveragegrade As Single
        Try
 
            For i = 0 To 4
                console.writeline("Name " & (i + 1))
                names(i) = console.readline()
                console.writeline("What is this person's grade mark?")
                grades(i) = console.readline()
            Next
            For j = 0 To 4
                console.writeline(names(j) & " " & grades(j) & " " & grade(grades(j)))
            Next
            For x = 0 To 4
                intaveragegrade += grades(x)
            Next
            console.writeline("The average mark is " & (intaverageGrade / 5))
            console.readline()
        Catch ex As Exception
            console.writeline(ex.message)
        End Try
        main()
    End Sub
    Public Shared Function grade(ByVal mark As Integer) As String
        Dim returngrade As String
        Select Case mark
            Case 0 To 59
                returngrade = "F"
                Return returngrade
            Case 60 To 69
                returngrade = "D"
                Return returngrade
            Case 70 To 79
                returngrade = "C"
                Return returngrade
            Case 80 To 89
                returngrade = "B"
                Return returngrade
            Case 90 To 100
                returngrade = "A"
                Return returngrade
        End Select
        Return ""
    End Function
End Class[/COLOR][/SIZE]

Every time you list a FOR loop in your lines, it takes the number of instances you want to loop and goes through them until they are complete.

declare a variable, "i" is often used.

example .. dim i as integer

FOR loops are great for looping through an array. Say you want to get the information from the array ArrayMonkey(6), you would have to dim the array, then fill it with information. It's like taking 6 boxes, all with the same prefix name and using a loop to tell them what to do with each box.

dim ArrayMonkey() as string
dim i as integer

for i = 0 to 5
'the boxes you use, starting at 0 and counting to the number in your array - 1
'this goes to each box
ArrayMonkey(i) = i
next

this output goes to..
ArrayMonkey(0) = 0
ArrayMonkey(1) = 1
ArrayMonkey(2) = 2
ArrayMonkey(3) = 3
ArrayMonkey(4) = 4

hope that gives a little better understanding on loops.
 
Last edited:
VB.NET:
Class students
    Public Shared Sub main()
        Dim names(3) As String
        Dim marks(3) As Integer
        Dim grades As String = ""
        Dim average As Single
        Dim total As Single = 0

        For i As Integer = 0 To 3
            Console.Write("Enter name of student " & i & " = ")
            names(i) = Console.ReadLine()
            Console.Write("Total marks = ")
            marks(i) = Console.ReadLine()
        Next

        For i As Integer = 0 To 3

            If marks(i) < 60 Then
                grades = "F"
            ElseIf marks(i) < 70 Then
                grades = "D"
            ElseIf marks(i) < 80 Then
                grades = "C"
            ElseIf marks(i) < 90 Then
                grades = "B"
            Else
                grades = "A"
            End If

            Console.WriteLine(names(i) & "  " & marks(i) & "  " & grades)
            total += marks(i)
        Next
        average = total / 4.0
        Console.WriteLine("Average of the class is " & average)
        Console.ReadLine()
    End Sub
End Class
 
help me plz i need a tutorial on the all the looping including the IF condition DO UNTIL and wat not..Wud really appreciate it.I wanna understand it well..Plz ASAP!! i have a quiz tommorow we will focus on the 4 kinds of Loops.
 
Back
Top