U4 P3 Displaying Student Scores and Grades Conditional and Loop Statements
pdf
keyboard_arrow_up
School
Indiana University, Purdue University, Indianapolis *
*We aren’t endorsed by this school
Course
210
Subject
Information Systems
Date
Apr 3, 2024
Type
Pages
5
Uploaded by HighnessYakMaster1036
I210 Information Infrastructure I ©Louie Zhu 1 Unit 4: The PHP Language Practice 3: Displaying student scores and grades using conditional and loop statements Get ready 1.
Download the practice3.zip
file from Canvas. Extract the Practice3
folder into the htdocs\I210\Unit04
folder on your computer. 2.
Open the index.php
file in PhpStorm. Examine the existing source code. Notice at the top of the file the variables $
student1
through $
student5
and $
score1
through $
score5
. In addition, a table at the bottom of the script displays values of these variables. 3.
Run the file in PhpStorm
. The Web page should display all five students’ names and scores. Students’ grades and grade description need to be determined next.
Part 1: Determine student grades using IF statements 4.
Create IF statements to determine student
1’s
grade. //determine grade for student1 if ($score1 >= 90) $grade1 = "A"; if ($score1 >= 80 and $score1 < 90) $grade1 = "B"; if ($score1 >= 70 and $score1 < 80) $grade1 = "C"; if ($score1 >= 60 and $score1 < 70) $grade1 = "D"; if ($score1 < 60) $grade1 = "F"; 5.
Repeat the last step four times to determine grades for the other four students. The code is available at https://pastebin.com/MfvMhYQ5
. Paste the code below the last IF statement. 6.
Run the file. You will only see content in the first three columns in the table.
I210 Information Infrastructure I ©Louie Zhu 2 Part 2: Modify the index.php
file to use nested IF … ELSE statements
7.
Switch to the index.php
file in PhpStorm. 8.
Replace the IF statements that determine the grade for student1 with IF … ELSE statements.
The code for Student 1 should read as follows after the modification: if ($score1 >= 90) $grade1 = "A"; else if ($score1 >= 80) $grade1 = "B"; else if ($score1 >= 70) $grade1 = "C"; else if ($score1 >= 60) $grade1 = "D"; else $grade1 = "F"; 9.
Repeat the last step for the other 4 students. Code is available at https://pastebin.com/awqXyCY9
. Paste the code to replace the IF statements for Student 2 through 5. 10.
Save the file and refresh the Web page in your browser. The page should look the same as before.
I210 Information Infrastructure I ©Louie Zhu 3 Part 3: Modify the index.php
file to add SWITCH statements to describe student grades 11.
Switch to the index.php
file in PhpStorm. 12.
Below the last IF … ELSE statements, add the first set of SWITCH statements.
//describe student1 grade switch ($grade1) { case "A": $description1 = "Outstanding"; break; case "B": $description1 = "Good"; break; case "C": $description1 = "Acceptable"; break; case "D": $description1 = "Poor"; break; case "F": $description1 = "Failed"; break; default: $description1 = "Invalid grade"; } 13.
Repeat the last step four times to add SWITCH statements for the other 4 students. Code is available at https://pastebin.com/rY0JrhMs
. Paste the code right below the last SWITCH statement. 14.
Save the file and refresh the Web page in your browser. The last column should now display grade description.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
I210 Information Infrastructure I ©Louie Zhu 4 Part 4: Modify the index.php
file to use WHILE loop to print table rows 15.
Switch to the index.php
file in PhpStorm. Scroll down to the bottom of the file. 16.
Delete all table rows, except for the header row. Be sure to keep the closing </table> tag. The html code for the entire table should read as follows: <table width='600' border='1' cellspacing='0' cellpadding='5' align='center'> <tr> <th>Student</th> <th>Score</th> <th>Grade</th> <th>Description</th> </tr> </table> 17.
Insert a PHP code block right above the closing </table> tag. 18.
Inside the PHP code block, define a constant named STUDENT_NUM
. The value is equal to the total number of students, 5. define ("STUDENT_NUM", 5); 19.
Create a variable named student_count
that counts the student. $student_count = 1; 20.
Continue coding the program by adding a WHILE
loop as follows: while ($student_count <= STUDENT_NUM) { } 21.
Inside the WHILE loop, create four variables. $student = ${'student' . $student_count}; $score = ${'score' . $student_count}; $grade = ${'grade' . $student_count}; $description = ${'description' . $student_count}; 22.
After the above four variables and still inside the WHILE loop, add an echo
statement to insert a table row. echo "<tr> <td> $student </td> <td> $score </td> <td> $grade </td> <td> $description </td> </tr>"; 23.
At last, increment the $student_count
variable at the end of the WHILE loop. $student_count++; 24.
Save the file and refresh the Web page in your browser.
I210 Information Infrastructure I ©Louie Zhu 5 Part 5: Modify the index.php
file to use FOR loop to print table rows 25.
Delete the line that creates the variable $student_count, which is located before the WHILE loop. 26.
Replace the line beginning with keyword “while” with the following line:
for ($student_count=1; $student_count<=STUDENT_NUM; $student_count++) { 27.
Delete the line that increments variable $student_count, which is located after the echo
statement. 28.
Save the file and refresh the Web page in your browser.