Railroad diagrams, also known as syntax diagrams, are a visual way to represent the grammar of a programming language. To use it, you...
X1324: Valid for loop (4)?
Railroad diagrams, also known as syntax diagrams, are a visual way to represent the grammar of a programming language. To use it, you trace the diagram as if it was a railroad from left to right. The ovals represent terminals (exact characters or keywords) on the input. The rectangles represent non-terminals, typically defined by other diagrams or simply abstracted out for presentation.
for
railroad diagram
The image below is a railroad diagram for the for loop
in Java.
To process the diagram, follow the path:
- Move along the path from left to right.
- When you encounter a terminal (oval), match it exactly in your code.
- When you encounter a non-terminal (rectangle), look for another diagram as the definition (think of a method call).
- When the path loops back on itself, it indicates repetition. Follow the loop as many times as needed.
Question
Using the diagram above, is the code shown below a valid for
loop?
int i = 0;
for (i < 10) {
A();
i++;
}
You may assume that A();
and i++;
each is a valid statement
.
Your Answer:
Feedback
Your feedback will appear here when you check your answer.