Chapter 3 Controls
Chapter 3 Controls
Chapter3
Lecture Note
Controls
This chapter, we look at Blocks, Decision Making, Loops, and Branching statements which are
called controls. We will learn how these controls can be put together to build complex programs
with more interesting behavior. in this chapter, we are interested in the kind of complex
programs by using controls that can occur within a single subroutine. On this level, almost there
are three types of control structures such as: decision making, loops and branching controls,
which can be used to repeat a sequence of statements over and over or to choose among two or
more possible courses of action. Java includes several control structures and we shall see each of
them in some detail. This chapter also we study how each of that structures can coded in java
program design.
The ability of a computer to perform complex tasks is a few ways of combining simple
commands by using control structures. In Java, there are three main control structures that are
used to determine the normal flow of control in a program. control structures or control
statements are:
Blocks
The block is the simplest type of structured statement. Its purpose is simply to group a sequence
of statements into a single statement.
alwaysblock is a sequence of statements enclosed between a pair of braces, “{” and “}”. it is
possible for a block to contain no statements at all; such a block is called an empty block.
If Statement
An if statement tells the computer to take one of two alternative courses of action, depending on
whether the value of a given Boolean expression is true or false, if statement is used to specify
block of code to be executed if specified condition is true.
An if statement has the form:
if (Boolean-expression)
{
statement
}
To execute this statement, the computer evaluates the expression. If the value is true, the
computer executes the statement that is contained inside the if statement; if the value is false, the
computer skips over that statement.
Example
if ( years> 14 )
{
System.out.print(" yes ");
}
if else Statement
if else statement we can use else keyword to create simple branch statement. Else statement is
used to specify a new situation as to test if the first condition is false.
If else statement has the form:
If (expression)
{
Else
}
Statement
}
Example
if ( years> 14 )
{
System.out.print(" still you are young ");
}
else
{
System.out.print("your are old");
if else if Statement
we can create multiple branches using the if else if keyword which tests for an other condition if
and only if previous conditions is not met.
If else if has the form:
if(expression)
{
Statement1
}
else if
{
Statement2
}
Else
statement 3
}
Example
int i = 16;
if(i>20)
else if ( i<10 )
else
} }
Case Statement
Assignment
1. Make study about case statement explain it and give one program as example
Loops in Java
There may be a situation when you need to execute a block of code in several number of times.
In general, statements are executed sequentially: The first statement in a function is executed
first, followed by the second, and so on.
loop is a sequence of instructions that is repeated until a certain condition is reached.
In Java we have three types of basic loops: for, while and do-while
For Loop
For loop is type of control structure that repeats the execution a specific number of time and the
condition always is true.
When you know exactly how many times you want to loop through a block of code, use the for
loop instead of a while loop.
for(declaration : expression)
{
Statements(s)
}
Example 1
for(int i=1;i<=10;i++)
{
System.out.println(i);
}
While loop
A while loop is used to repeat a given statement over and over. Of course, it’s not likely that
you would want to keep repeating it forever. That would be an infinite loop, which is generally a
bad thing. In while loop if specified condition is truewhile statement continues testing the
expression and executing its block until the expression evaluates to false.
A while loop has following form:
while (Boolean-expression)
(statement)
while (boolean-expressioni) {
(statements)
}
When the computer comes to a while statement, it evaluates the (Boolean-expression) which
yields either true or false as its value. If the value is false, the computer exits the execution as
figure below illustrated. If the value of the expression is true, the computer executes the
statement or block of statements inside the loop. Then it returns to the beginning of the while
loop and repeats the process, this will continue over and over until the value of the expression is
false.
Example
Here is an example of a while loop that simply prints out the numbers 1, 2, 3, 4, 5:
int number; // The number to be printed.
number = 1; // Start with 1.
while ( number< 6 ) { // Keep going as long as number is < 6.
System.out.println(number);
number = number + 1; // Go on to the next number.
}
System.out.println("Done!");
Do..while Statements
Sometimes it is more convenient to test the continuation condition at the end of a loop, instead of
at the beginning, as we done in the while loop. The do.while statement is very similar to the
while statement, except that the word “while”, along with the condition that it tests has been
moved to the end. The word “do” is added to mark the beginning of the loop.
Do(statement)
while ( boolean-expression);
Example
int x = 10;
do
{
System.out.print("value of x is :" + x);
x ++;
System.out.print("\n");
}
while (x < 30);
Branchingstatements allows the flow of execution to jump to a different part of the program the
common branching statement are:
Breakstatement
Continuestatement
return statement.
Break
The syntax of the while and do..while loops allows you to test the continuation condition at
either the beginning of a loop or at the end. Sometimes, it is more natural to have the test in the
middle of the loop, or to have several tests at different places in the same loop. Java provides a
general method for breaking out of the middle of any loop. It’s called the break statement, which
takes the for
break;
when the break statement is encountered inside the loop, the loop is immediately terminated and
the program control resume at next statement following the loop.
Example
Continue statement
The continue statement is related to break, but less commonly used. A continue statement tells
the computer to skip the rest of the current iteration of the loop. However, instead of jumping out
of the loop altogether, it jumps back to the beginning of the loop and continues with the next
iteration (including evaluating the loop’s continuation condition to see whether any further
iterations are required). As with break, when a continue is in a nested loop, it will continue the
loop that directly contains it; a “labeled continue” can be used to continue the containing loop
instead.
Example
This is most commonly is used branching statement, return statement exits the the control flow
from the current method and returns to where the method was invoked.
The return statement can return a value or may not return a value.