CONTROL STRUCTURES
Structured programming is the implementation of three control structures. Structures can be thought of as blocks of code that allows programs to implement sequence, selection and repetition. Sequence, selection and repetition structures each have one entry point and one exit point which provide an element of simplification and organization that makes programs easier to understand and maintain. All programming languages make use of these three structures in various ways. They are essential to performing the complex logic necessary in computer programming.
The two most common structures are selection and repetition.
- Selection,The program executes particular statement depending on one or more condition.
- Repetition ,The program repeats particular statement a certain number of copies depending on one or more conditions.
Branch - Altering the flow of program by making a selection or choice.
Loop - Altering the flow of program execution by the repetition of statement(s)
Before that,you must understand the nature of condition expression and how to use them.
if (score is greater than or equal to 90 )
grade is A
The condition expression is score is greater than or equal to 90.
You can see that a statement such as grade is A is to be executed only if a certain condition is met.A condition is met if it evaluates to true.
is true if the value of score is greater than or equal to 90,it is false otherwise.For example,if the value of score is 95,the statement evaluates to true.Similarly,if the value of score is 86,the statement evaluates to false.So if the value of score is greater than or equal to 90,then the statement,grade is A executes.
it is useful for computer to be able to recognize expression,such as score is greater than or equal to 90,to be true for appropriate values.Futhermore,in certain situation,the truth of statement could depend on more than one condition.
To make decisions, you must be able to express conditions and make comparisons.
Logical(boolean)expression : condition are either true or false.
Relational operator : An operator that that allows you to make comparisons in program.
NOTE !
In Java , the symbol ==,which consists of two equal signs,is called the equality operator.Recall thht the symbol = is called the assignment operator.The equality operator,==,determines whether two expressions are equal,whereas the assignment operator,=,assigns the value of an expression to a variable.
RELATIONAL OPERATORS AND PRIMITIVE DATA TYPES
You can use the relational operators with integral and floating-point primitive data types.For example,the following expression use both integers and floating-point number :
Expression Meaning Value
8 < 15 8 is less than 15 true
6 != 6 6 is not equal to 6 false
2.5 > 5.8 2.5 is greater than 5.8 false
5.9 <= 7.5 5.9 is less than or equal to 7.5 true
LOGICAL(BOOLEAN)OPERATORS AND LOGICAL EXPRESSIONS
There are three other boolean operators that we need to learn, and a new primitive variable type (boolean). The operators are:
&& //The AND operator
|| //The OR operator
! //The NOT operator
Boolean variables can be declared just like other variables, and can have the values "true" or "false". The Java keywords
true
and
false
can be used in your programs to set the values of boolean
variables. Boolean variables can also be given values from the result of boolean
expressions. Consider the statements in the program fragment below. The first
one is just declaration and initialization of a couple of integers. You should
have those down by now. The second statement is new. It uses the
boolean
keyword (remember - all Java keywords are lower case!).
boolean is a primitive type just like an int
or a
float
. It is a type, however, that can only hold the values true or
false. It can't hold numbers, strings of characters, etc. It just represents the
values true or false. In fact, as you can see in the 2nd statement, we can
initialize a boolean variable to true or false by using the true or false
keyword and the assignment operator. So the boolean variable mybool
has the value true. We don't know what anotherBool
or
bool3
will be until the 3rd and 4th statements. In the 3rd statement, we assign the value "false" to the boolean variable
anotherBool
. x < y
) and
assigns it to the bool3
variable. So remember, the boolean
operators we are discussing all return either true or false, so the boolean
expression x < y
returns the value true given the values of
x
and y
in the code below. int x=5, y=7;
boolean mybool = true, anotherBool,
bool3;
anotherBool = false;
bool3 = x < y; //bool3 will now have the
value true
So, now that we know what a boolean variable is, consider the condition where we have two boolean variables
P
and Q
. The following
table indicates how the AND (&&
), OR (||
) and
NOT (!
) operators can operate on them. The first row (besides the
title row) shows that if P and Q are both false, then P AND Q is false, P OR Q
is false, and NOT P is true (as is NOT Q). So let's look at these operators:
AND (
&&
) : The &&
operator is a
binary operator that takes two boolean operands. For &&
to
return true, BOTH of the arguments must have the value true. If EITHER of the
arguments is false, then &&
returns false. OR (
||
) : The ||
operator is a binary operator that
takes two boolean operands. For ||
to return true, EITHER or BOTH
of the operands must be true. If BOTH are false, then ||
returns
false. NOT (
!
): The !
operator is a unary operator that
takes one boolean argument. For !
to return true, the operand must
be false. If the argument is true, !
returns false. The table shows how this works for each operator.
You can combine boolean variables in a wide variety of equivalent ways. Consider the folloiwing variable declaration/initialization.
boolean P = true, Q = false;
The expressions
(P && Q) , (!P || Q)
and (!P
&& !Q)
are all exactly the same: they all are boolean expressions
that return false. Can you see why? The table tells you that if P
is true and Q
is false, then P && Q
is false.
How about (!P || Q)
? Well, if P
is true, then
!P
is false. So the expression reduces to (false ||
Q)
. Since Q
is false, then we have (false ||
false)
. Since both operands are false, then the ||
operator
returns false. Think about the last expression the same way and make sure you
can figure out why the answer is false. SELECTION : IF AND IF . . . ELSE
The general syntax of the if-else statement is as follows:
if (expression) statement1
else statement2
if statements may omit the
else option which result
in one-way selection.if (expression) statement;
If the expression is
non-zero, statement is executed, otherwise nothing is executed. The following flowchart illustrates the flow
of control.
<!#if !vml#><!#endif#>
3. The full
if-else statement allows
for two-way control. If the value of the
expression is true, statement1 is executed.
If the value of the expression equals false, the else option results in statement2
being executed. The following flowchart
from handout, H.A.8.2, illustrates the
flow of control.
<!#if !vml#><!#endif#>
Compound
Statements
- The statement executed in a control structure can be a block of statements, grouped together into a single compound statement.
- A compound statement is created by enclosing any number of single statements by braces as shown in the following example:
if
(expression)
{
statement1;
statement2;
statement3;
}
else
{
statement4;
statement5;
statement6;
}
Multiple Selection : Nested if
In the previous sections,you learned how to imploment one-way and two-way selections in a program.However,some problem require the implementation of more than two alternatives.This particular problem has four alternatives-that is,multiple selection paths.You can include multiple selection paths in a program by using an if . . . else structure-if the action statement itself is an if or if . . else statement.When one control statement is located within another,it is said to be nested.
Conditional Operator
(optional)
(condition) ?
statement1 : statement2;
- If the condition is true, statement1 is executed. If the condition is false, statement2 is executed.
- This is appropriate in situations where the conditions and statements are fairly compact.
int max (int a, int b) // returns the larger of two integers
{
(a > b) ?
return a :
return b;
}
switch structures
The switch statement allow us to
select one from multiple options. It is especially useful when we want to write
a code that makes a selection based on an expression or a variable. switch selection structure consists of a
series of case label sand optional default case as shown below.
switch statement works by
comparing the value of variable or expression within parentheses with values
specified in each case. If there
is a match, the statements following the case will be executed. If there is no
match occurs, the default
statement is executed. At the end of each case, keyword break must be placed to exit from the
switch immediately after
statements execution.
String are compared character by character,starting with the first character and using the Unicode collating sequence.The character-by-character comparison continues until one of the following three conditions is met; a mismatch is found,the last characters have been compared and are equal,or one string is exhausted.
Tiada ulasan:
Catat Ulasan