Você está na página 1de 2

what are control statements?

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.

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.

in short this is what a while loop looks like

1. while (some condition is true)


2. {
3. run this code again and again
4. }

Você também pode gostar