If and Loop
If and Loop
5 Control Structures
5.1 Objectives
In the previous sections, we have given examples of
sequential programs, wherein statements are executed one
after another in a fixed order. In this section, we will be
discussing control structures, which allows us to change
the ordering of how the statements in our programs are
executed.
At the end of the lesson, the student should be able to:
Use decision control structures (if, else, switch) which
allows selection of specific sections of code to be
executed
Use repetition control structures (while, do-while,
for) which allow executing specific sections of code
a number of times
if( boolean_expression )
statement;
or
if( boolean_expression ){
statement1;
statement2;
. . .
}
where, boolean_expression is either a boolean expression or
boolean variable.
For example, given the code snippet,
int grade = 68;
if( grade > 60 )
System.out.println("Congratulations!");
or
int grade = 68;
if( grade > 60 ){
System.out.println("Congratulations!");
System.out.println("You passed!");
}
5.2.1 if statement
The if-statement specifies that a statement (or block of
code) will be executed if and only if a certain boolean
statement is true.
1
Coding Guidelines:
1. The boolean_expression part of a statement should
evaluate to a boolean value. That means that the
execution of the condition should either result to a
value of true or a false.
2. Indent the statements inside the if-block.For
example,
if( boolean_expression ){
//statement1;
//statement2;
}
or
if( boolean_expression )
statement;
else
statement;
Coding Guidelines:
1. To avoid confusion, always place the statement or
statements of an if or if-else block inside brackets
{}.
2. You can have nested if-else blocks. This means that
you can have other if-else blocks inside another ifelse block. For example,
if( boolean_expression ){
if( boolean_expression ){
. . .
}
}
else{
. . .
}
5.2.3 if-else-if statement
The statement in the else-clause of an if-else block can be
another if-else structures.
This cascading of structures allows us to make more
complex selections.
Take note that you can have many else-if blocks after an ifstatement. The else-block is optional and can be omitted. In
the example shown above, if boolean_expression1 is true,
then the program executes statement1 and skips the other
statements. If boolean_expression2 is true, then the program
executes statement 2 and skips to the statements following
statement3.
For example, given the code snippet,
int grade = 68;
if( grade > 90 ){
System.out.println("Very good!");
}
else if( grade > 60 ){
System.out.println("Good!");
}
else{
System.out.println("Sorry you failed");
}
switch( switch_expression ){
case case_selector1:
statement1;
statement2; //block 1
. . .
break;
case case_selector2:
statement1;
statement2; //block 2
. . .
break;
. . .
default:
statement1;
statement2; //block n
. . .
}
10
5.3 Exercises
Number in words
Class Name: NumberWords
Get a number as input from the user, and output the
equivalent of the number in words. The number inputted
should range from 1-10. If the user inputs a number that is not
in the range, output, "Invalid number".
1. Use an if-else statement to solve this problem
2. Use a switch statement to solve this problem
Apples
Class Name: Apples
Apple Co is one of the most leading apple company in
Magnolia. Because of the growing number of customers
buying their product, they want you to create a program that
will ask for the number of apples (in pieces) then compute for
the total amount to pay. Display the price per apple and the
total amount to pay. Apples are sold according to the
following: (1box = 3doz)
1 50pcs
540.00/box
51-100pcs
450.00/box
More than 100pcs
360.00/box
*input is in integer
The sample code shown will print 4321 on the screen. Take
note that if the line containing the statement i--; is removed,
11
12
statement1;
statement2;
. . .
}while( boolean_expression );
The statements inside the do-while loop are first executed,
and then the condition in the boolean_expression part is
evaluated. If this evaluates to true, the statements inside the
do-while loop are executed again.
Here are a few examples that uses the do-while loop:
Example 1:
int x = 0;
do
{
System.out.println(x);
x++;
}while (x<10);
Example 2:
//infinite loop
while(true)
System.out.println(hello);
Example 3:
//no loops
// statement is not even executed
while (false)
System.out.println(hello);
5.4.2 do-while loop
The do-while loop is similar to the while-loop. The statements
inside a do-while loop are executed several times as long as
the condition is satisfied.
13
14
Example 2:
//infinite loop
do{
System.out.println(hello);
} while (true);
for (InitializationExpression;
LoopCondition; StepExpression){
statement1;
statement2;
. . .
}
Example 3:
//one loop
// statement is executed once
do
System.out.println(hello);
while (false);
where,
...
}while(boolean_expression)
//WRONG-forgot semicolon ;
17
18