Whenever the computer runs a Java program, it goes straight from the first line of code to the last. Control statements allow you to change the computer's control from automatically reading the next line of code to reading a different one. Lets say you only want to run some code on condition. For example, let's say that you are making a program for a bank. If someone wants to see his records, and gives his password, you don't always want to let him see the records. First, you need to see if his password is correct. You then create a control statement saying "
if" the password is correct, then run the code to let him see the records. "
else" run the code for people who enter the wrong password. You can even put one control statement inside another. In our example, the banking system has to worry about people trying to get someone else's records. So, as the programmer, you give the user three shots. If he misses them, then the records are locked for a day. You can have code with control statements that look like this (not real code, only meant to help you understand. also, Java doesn't have go back to earlier lines- again just trying to focus on the control part.)
1. set variable counter to 0;
2. show screen asking for password;
3. if password is correct, run the code that let's him see the records;
4. else add one to the counter;
5. if counter does not equal three, then go back to line two and reshow the screen
6. else (meaning that this is the third time that he's messed up) then lock the records and show screen "goodbye"
If you look at this psuedocode (code that is more understandable to a person, but the computer can not understand it) you will see that one
if else, is placed inside the next
if else. The way you read the control is like this. If the password is correct then skip lines 4-6 that deal with incorrect passwords only. Else, (if the password is incorrect) skip the end of line three and read line 4. Then if we have not hit the third time go back, and never run line 6. However, if this is the third time, then don't run the end of line 5, but run line 6.
Control Statements in Java are very simple. In some languages they have complex control statements, that do a lot. However, Java offers only simple control statements. If you need a complex control statement, you can build it with a few simple ones. This makes control statements a lot easier to work with. Although Java has a few different kinds, I will illustrate one more common one. The loop. A loop is where you follow the same code over and over again, until some condition stops it. For example, what if you were writing a program that spell checks each word. You don't know how many words there will be, as every document will be different. So you create a
while loop and tell it, while there are more words, keep on giving me the next word, I'll run the code to check if it is spelled correctly. Then I'll start the loop again saying if there are more words give them to me. As long as there is one more, the code will start from the beginning of the loop every time.
in short this is what a while loop looks like
1. while there are more words left to be checked
2. give me the next word
3. let me run some code to see if it is spelled correctly
4. check, if there are more words go back to line 1, else continue.
1. while (some condition is true)
3. run this code again and again