Using Nested Control Structures

Language: 

The following Java program uses Nested Control Structures. Consider the following program that reads students ID’s and their test scores. For each student, the program outputs the students ID, test score, and grade. The program also outputs the number of students in the class.

  1. import java.util.*;
  2.  
  3. public class nestedControlled
  4. {
  5. static Scanner console = new Scanner(System.in);
  6. public static void main(String[] args)
  7. {
  8. String studentId;
  9. int testScore;
  10. int count = 0;
  11.  
  12. System.out.print("Enter the student ID: "
  13. + "(Enter XXX to end):");
  14. System.out.flush();
  15. studentId = console.next();
  16. System.out.println();
  17.  
  18. while (!studentId.equals("XXX"))
  19. {
  20. count++;
  21.  
  22. System.out.print("Enter the test score: ");
  23. System.out.flush();
  24. testScore = console.nextInt();
  25. System.out.println();
  26.  
  27. System.out.print("Student Id = " + studentId
  28. + ", test score = "
  29. + testScore + ", and grade = ");
  30.  
  31. if (testScore >= 90)
  32. System.out.println("A.");
  33. else if (testScore >= 80)
  34. System.out.println("B.");
  35. else if (testScore >= 70)
  36. System.out.println("C.");
  37. else if (testScore >= 60)
  38. System.out.println("D.");
  39.  
  40. else
  41. System.out.print("F.");
  42. System.out.print("Enter the student ID "
  43. + "(Enter XXX to end): ");
  44. System.out.flush();
  45. studentId = console.next();
  46. System.out.println();
  47.  
  48.  
  49. }//end while
  50. System.out.println("\nStudents in class = " + count);
  51. }
  52. }

Sample Run:

  1. Enter the student ID: (Enter XXX to end):IRT040687
  2.  
  3. Enter the test score: 90
  4.  
  5. Student Id = IRT040687, test score = 90, and grade = A.
  6. Enter the student ID (Enter XXX to end): KLY097865
  7.  
  8. Enter the test score: 80
  9.  
  10. Student Id = KLY097865, test score = 80, and grade = B.
  11. Enter the student ID (Enter XXX to end): XXX
  12.  
  13.  
  14. Students in class = 2

The program works as follows:

The statements

  1. String studentId;
  2. int testScore;
  3. int count = 0;

are the declaratiosn of variables: StudentId, testScore and count.

The statement

  1. System.out.print("Enter the student ID: "
  2. + "(Enter XXX to end):");

prompts the user to enter the student’s Id and type XXX to terminate the program.

The statement count++; increments the value of count by 1.

The statement

  1. while (!studentId.equals("XXX"))
  2. {
  3. count++;
  4.  
  5. System.out.print("Enter the test score: ");
  6. System.out.flush();
  7. testScore = console.nextInt();
  8. System.out.println();
  9.  
  10. System.out.print("Student Id = " + studentId
  11. + ", test score = "
  12. + testScore + ", and grade = ");
  13.  
  14. if (testScore >= 90)
  15. System.out.println("A.");
  16. else if (testScore >= 80)
  17. System.out.println("B.");
  18. else if (testScore >= 70)
  19. System.out.println("C.");
  20. else if (testScore >= 60)
  21. System.out.println("D.");
  22.  
  23. else
  24. System.out.print("F.");
  25. System.out.print("Enter the student ID "
  26. + "(Enter XXX to end): ");
  27. System.out.flush();
  28. studentId = console.next();
  29. System.out.println();
  30.  
  31.  
  32. }//end while

is the while loop of the program. It allows the user to enter the student test score and outputs the corresponding grade.

The statement System.out.println("\nStudents in class = " + count); displays the number of students in the class.


Add new comment

(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.

Filtered HTML

  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <csharp>, <css>, <java>, <javascript>, <php>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
To prevent automated spam submissions leave this field empty.