JAVA – Control Statements


As you’ve learned in the sequential statements topic, the computer while running a java program, starts executing from the topmost line of the code. It reads the first line of the code, and continues doing so till the last line. Control statements help you run a program in a manner you define. These statements are used to change the computer’s control from reading and executing the statements in a given sequence to the way you define.

Control statements always base the program execution on a set of conditions. Let’s take an example of a programmer designing a program for a financial institute. The data for such programs is confidential and cannot be accessed by all the users. You would provide such an access to the user only if they provide the correct username and password. “Only” if both the passwords and the username are correct, should the program code allow them to see the records. The “else” part of code will run for people who don’t have valid password and enter incorrect information.

As you can see, the control statements help you check a certain condition. You can also put one control statement into another. Such statements are then known as nested control statements. In the example given above, the financial institute software provides 3 chances o each user. If the user enters incorrect information in all the three chances, their records are locked for a particular period. A sample algorithm that would help you design such a program is shown below:

1. Set Variable_counter_1 to 0;
2. Show the computer screen that asks you to enter your password;
3. If the password you enter is correct, run the code that allows the user to see the records which he wanted to see;
4. Else increment Variable_counter_1  i.e. add one to the Variable_counter_1;
5. If Variable_counter_1 is not equal to 3, then return back to line two and Show the computer screen again that asks you to enter your password;
6. Else if Variable_counter_1  equals 3 then lock the records and display the "goodbye" screen (If the counter equals 3 this means user have tried 3 times to enter the password and he does not have valid password so the user does not have valid rights.)

The code given above is a pseudo code. Since this code is written in simple English, it is understood by any person, but not by the computer.  In this pseudo code one if else statement is place within another if else statement. The manner in which the code would be executed is:

  1. 1. Enter the password
  2. 2. If the password is correct, skip lines dealing with incorrect passwords i.e. lines 4,5 and 6
  3. 3. Else, if the password is incorrect, read line 4 instead of reading end of line 3
  4. 4. If the counter does not equal to 3, do not run line 6; but go back and run line 4 and 5
  5. 5. Else, if the password is incorrect for the third time, don’t run line 4 and 5 but run line 6

The control statements used in Java language are simple and can be understood by the computer easily. Because of this, they are very simple and are used extensively.  In case your condition has a complex structure, you can build it by combining a number of complex statements that contain a number of if-else statements.

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>