asp

Mod Function

duraboys 2011. 2. 16. 10:25

<%
For i = 1 to 10
    Response.write(i & " mod 5 = " & i mod 5 & "<br>")
Next
%>

This little script will give the following output:

1 mod 5 = 1
2 mod 5 = 2
3 mod 5 = 3
4 mod 5 = 4
5 mod 5 = 0
6 mod 5 = 1
7 mod 5 = 2
8 mod 5 = 3
9 mod 5 = 4
10 mod 5 = 0



<%
For i = 1 to 10
    If i mod 2 = 1 Then
        Response.write(i & " is odd.<br>")
    Else
        Response.write(i & " is even.<br>")
    End If
Next
%>

<%
Function isOdd(iInput)
    IsOdd = CBool( iInput Mod 2 )
End Function
%>

<%
iCellsPerRow = 3
iItems = 9

' Create the table
strOutput = "<table>"

' Loop through all items
For i = 1 to iItems
    ' Check to see if this is the start of a new row
    If i mod iCellsPerRow = 1 Then strOutput = strOutput & "<tr>"

    ' Add a cell for this item
    strOutput = strOutput & "<td>" & i & "</td>"

    ' Check to see if this is the end of a row
    If i mod iCellsPerRow = 0 Then strOutput = strOutput & "</tr>"
Next

' Close the table
strOutput = strOutput & "</table>"

' Write the HTML output
Response.write(strOutput)
%>

<%
iCellsPerRow = 3
iItems = 9

' Create the table
strOutput = "<table>"

' Loop through all items
For i = 1 to iItems
    ' Check to see if this is the start of a new row
    If i mod iCellsPerRow = 1 Then strOutput = strOutput & "<tr>"

    ' Add a cell for this item
    strOutput = strOutput & "<td>" & i & "</td>"

    ' Check to see if this is the end of a row
    If i mod iCellsPerRow = 0 Then strOutput = strOutput & "</tr>"
Next

' If we're not at the end of a row,
' fill the rest of the row with empty cells.
If iItems mod iCellsPerRow > 0 Then
    ' Loop through to complete table
    For j = 1 to iCellsPerRow - (iItems mod iCellsPerRow)
        ' Add an empty cell
        strOutput = strOutput & "<td>&nbsp;</td>"

        ' Add a close row tag if this is the last cell.
        If j = iCellsPerRow - (iItems mod iCellsPerRow) Then _
            strOutput = strOutput & "</tr>"
    Next
End if

' Close the table
strOutput = strOutput & "</table>"

' Write the HTML output
Response.write(strOutput)
%>