Sunday, May 6, 2007

Control Structures in Java

Control structures are the basic building blocks of any programming language, and hence is not specific to only Java.

There are 3 basic control structures for Java:

  • Sequential - This means that all Java statements are executed line-by-line, one after another. This is already inherent within the language itself as you write the codes.
  • Selection - This control structure instructs the computer to make a selection (or branching) of the portion of the program based on a decision, made by doing a comparison. There are 2 selection structures in Java, the if() and switch() structures.
  • Repetition - This structure instructs the computer to perform a repetition of a block of code, and there various methods to terminate the repetition. There are 3 repetition structures, the for() loop, the while() loop and the do-while() loop.
A complete computer program often needs to make use of a combination of these control structures. Do make a note of these structures as we will go through them in detail in the coming weeks.

No comments: