[go: up one dir, main page]

0% found this document useful (0 votes)
26 views10 pages

Chapter 3 Controls

The document discusses various control structures in Java including blocks, decision making statements, loops, and branching statements. It explains the different types of control structures like if, if-else, if-else-if statements and switch case for decision making as well as for, while, do-while loops. It also covers break, continue and return statements used for branching in Java programs.

Uploaded by

cawilyare9885
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views10 pages

Chapter 3 Controls

The document discusses various control structures in Java including blocks, decision making statements, loops, and branching statements. It explains the different types of control structures like if, if-else, if-else-if statements and switch case for decision making as well as for, while, do-while loops. It also covers break, continue and return statements used for branching in Java programs.

Uploaded by

cawilyare9885
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Programming Language II

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.

Blocks, Decision Marking, Loops and Branches.

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:

 Decision making statement


 Loop statement
 Branching statement.
Each of these structures is considered to be a single “statement,” but each structured statement
can contain one or more other statements inside itself.

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.

The format of a block is:


{
(statements)
}
Decision Making Statement
Decision Making in programming is similarly to decision making in real world because of in
programming can face some situations where we want certain block of code to be executed,
when some conditions is fulfilled. Java decision making statements allows you to make a
decision based upon the result of a condition
types of Decision-Making Statement
in java decision making statements are following:
 If Statement
 If else Statement
 If else if Statement
 Case Statement

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

int years = 15;

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

int years = 15;

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)

System.out.println( " you are old ");

else if ( i<10 )

System.out.println("you are Young");

else

System.out.println("now you are mature ");

} }

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 loop takes this form

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)

Many while loops have the form:

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..while statement has the form

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);

Branching Statement in Java

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

int [] numbers = { 10, 20 , 30, 40, 50 };


for( int x: numbers)
{
if (x = = 40)
{
break;
}
System.out.print(x);
System.out.print("\n");
}

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

int [] numbers = { 10, 20 , 30, 40, 50 };


for( int x: numbers)
{
if ( x == 40)
{
continue;
}
System.out.print(x);
System.out.print("\n");
}
Return Statement

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.

You might also like