0% found this document useful (0 votes)
15 views

Java Control Statement CSC 112 Week 6

Uploaded by

ejerajosiah38
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Java Control Statement CSC 112 Week 6

Uploaded by

ejerajosiah38
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

JAVA CONTROL STATEMENTS

Presenter

1
Objectives – week seven

 Define control statement


 Identify the different types of control statements

 Identify the syntax of control statements


 Demonstrate control statements in a program

CSC 112 2
Understand the concepts of Control Statement

Specific Objectives
 Define control statement

 Identify the different types of control statements

 Identify the syntax of the control statements

 Demonstrate control statements in programs

3
CSC 112
Understand the concepts of Control Statement
Control Statements
 The ability of a computer to perform complex task is built on a few ways of combining single commands into
control structure or statements.
 Java statements are divided into non-executable and executable statements
 The non-executable statements are added or included in a program for documentation and explanation of
the program segment (example comments). They are ignored by the compiler.
 Executable statements are statements that are executed by the compiler to perform a particular task.
 The control statements include:
 Expression statement
 Assignment statement
 Null statement
 Block statement
 Selection statement 4
 Conditional control or loop statements
Understand the concepts of Control Statement

Expression Statement
 Expression statement is an expression followed by a semi-colon
 Examples of expression statements include:
 Declaration statement: example int x;
 Increment and decrement statement: example ++x or –x;
 Example:
{int x; x = 5; System.out.println(++x);}

5
Understand the concepts of Control Statement

Null Statement or empty statement


 The null statement is an empty line statement with a semi-colon
 Example:
 {
;}
or
;

 It does nothing at execution time and has no side effect on the program

6
Understand the concepts of Control Statement

Assignment statement
 Assignment statement is used to assign values or expression to variable.
 The syntax is:
 <variable> = <value>|<expression>;
 Example:
 { x = 6; or x = 5 + 4 *3;;}

 This is used to give an initial value to a variable (This is called initialization)


 Variables that are not initialized automatically set to zero by default

7
Understand the concepts of Control Statement

Block statement
 Block statement is the simplest type of structured statement
 It is a group of zero or more statements enclosed between a pair of curly braces.
 The syntax is:
 {
 <statement(s)>;
 }
 example:
 { x = 6; or x = 5 + 4 *3;}
 The purpose of a block statement is to group together several statements into a single statement or unit that is executed
together.
8
Understand the concepts of Control Statement

 Selection Statements
 The selection statements are used to execute some portions of a program depending upon a particular
condition..
 The conditional statements include:
 If ….. Statements or one way statement
 If …else statement or two-way statement
 Nested if …else statement
 Multi-way if .. else statement
 Switch statement

9
Understand the concepts of Control Statement

 If …Statements
 The if .. Statement is used to execute a statement or block of statements only if the condition is true or
fulfilled.
 The syntax of the if .. Statement is:
 If (condition) statement
Or
 If (condition) { <statements>}
The flowchart of the if .. Statement is:
Example:
{if (x<0) x = -x;System.out.println(x);}

10
Understand the concepts of Control Statement

 If …Statements
 If the condition is true, the statement or block of statements within the body of the control statement will be
executed.
 Otherwise, the statement will be ignored and the program continues with the next statement after the if
statement.
Example:
{
int x = 6; int y = 5;
if (x>y) System.out.println(x);
}

11
Understand the concepts of Control Statement

 If …else statements
 The if..else statement causes one of the two alternative statements to be executed depending upon the
condition.
 If the condition is true or fulfilled, the if block statement is executed.
 Otherwise the false block will be executed.
 The syntax for the if .. else statement is:
 if (condition) statement
 else
 statement

or
 if (condition) { <statements>}
 else {<statements>}
12
The flowchart of the if .. Statement is:
Understand the concepts of Control Statement

 If …else Statements

 If the condition is true, the statement or block of statements within the body of the if statement will be
executed.
 Otherwise, the statement within the body of the false statement will be executed.
Example:
{
int x = 6; int y = 5;
if (x>y) System.out.println(x);
else System.out.println(y);
}
Since x is greater that y; then the if condition is true so the x will be displayed on the standard output device.

13
Understand the concepts of Control Statement

 Nested If …else statements


 A program can test for multiple cases by placing one if..else statement inside another if..else statement.
 The general syntax of the nested if.. else statement is:
if (condition) statement
else if (condition)
Statement

else if (condition)
statement

else if (condition)
statement

else
statement
The flowchart of the nested if..else statement is:
14
Understand the concepts of Control Statement

 Embedded If …else statements


 An embedded statement occurs when an if..else statement is placed inside an if block or else block of an
if…else statement.
 The general syntax of the nested if.. else statement is:
if (condition) statement
if (condition)
Statement

else
statement

else
if (condition)
statement

else 15
statement
Understand the concepts of Control Statement

 Switch statements
 The switch statement provides a way of choosing between a set of alternatives, based on the value of the expression. The
expression is compared with the case constant values.
 The general syntax of the switch statement is:
Switch (expression
{
Case constant_1:
statement(s);
Case constant_2:
statement(s);
Case constant_3:
statement(s);
default: 16

statement(s);
Understand the concepts of Control Statement

 Switch statements

 The flowchart of the switch statement is shown

17
Understand the concepts of Control Statement

 Switch statements
 The value of the expression is a value or an expression that can be evaluated to a value
 The expression (called the switch tag) is compared with the numeric constants (called the label in the order
they appear) until a match is found
 The statement following the matching cases will be executed otherwise, the default statement will be
executed
 The default case is optional and will be executed if no match is obtained.

18
Understand the concepts of Control Statement

 Loop statements
 The objective of a loop statement is to repeat a statement or block of statements over a period of times while
the condition is fulfilled.
 The loop statement include:
 While loop
 Do … while loop
 For loop

 The general syntax of the loop statement include:


 While loop statement
While (condition) statement
or
While (condition) {
block of statements 19

}
Understand the concepts of Control Statement

 Loop statements
 The general syntax of the loop statement include:
 Do … While loop statement
do (statement)
while (condition) ;
or
do {
block of statements
}while (condition);
 The difference between the while and the do .. While statements is that the while loop evaluates the condition before the
executing the statement body while the do.. While statement executes the statement body before evaluating the condition.

20
Understand the concepts of Control Statement

 Loop statements

 The general syntax of the loop statement include:


 For loop statement
for (initial_value; conditional expression ; increment)
{block of statement}
Nested for loop statement
for (initial_value; conditional expression ; increment) {
for (initial_value; conditional expression ; increment)
{block of statement}
}
 The for loop is a repeat statement for discrete cases

21
Understand the concepts of Control Statement

 Loop statements
 Examples using C++ programming language:
 While loop statement
counter = 1;
while (counter <= 10)
{ System.out.println(counter);
++counter;
}
 Do..while loop statement
counter = 1;
do { System.out.println(counter);
++counter;
22
}while (counter <=10);
Understand the concepts of Control Statement

 Loop statements
 Examples:
 For loop statement
int x ;
for (x = 1; x <= 10; x++)
{System.out.println(x);
}
 Nested for loop statement
int x ; int y;
for (x = 1; x <= 10; x++){
for (y = 1; y <= 10; y++)
{System.out.println(x+y);
23
}
}
Understand the concepts of Control Statement

 Unconditional statements (Jump Statement)


 The jump statements used in Java programming language are the break and continue statements
 Java programming language do not use the goto statement
 The break and continue statements are jump statements that are used to skip some statements inside the loop
or terminate the loop without checking the test conditions and without executing the remaining part of the
loop.
 The statements can be used inside any loop such as for, while, do ..while loop.

24
Understand the concepts of Control Statement
 break statements (Jump Statement)
 The break statement is a loop statement that is used to terminate the loop without executing the remaining part of the
body of the loop. When the break statement is executed in a loop, it causes immediate exit of the statement after the
break statement.
 The syntax is:
{……….
break;
…………
}
Example:
{for (int x = 0; x<5; x++){
if (x==3)
break;
System.out.println(x); 25
}
Understand the concepts of Control Statement
 break statements as a form of goto statement

 Java does not have a goto statement because. It uses the label statement to identify a block of code.
 The syntax is:
{……….
label:
{
}
break label;
…………
}

26
Understand the concepts of Control Statement
 break statements as a form of goto statement
Example:
public static void main()
{
first: for (int x = 0; x<5; x++){
Second:
for (int k = 0; x<5; k++){ if (x ==1 && k ==2) {
break first;
System.out.println(x+k);
}
}

27
Understand the concepts of Control Statement
 continue statements (Jump Statement)
 The continue statement is a loop statement that is used to skip the current iteration of a loop and proceeds with the next
iteration of the loop
 The unlabeled continue statement skips to the end of the innermost loop’s body and evaluates the Boolean expression
that controls the loop
 The syntax is:
{……….
continue;
…………
}
Example:
{for (int x = 0; x<5; x++){
if (x==3)
continue; 28
System.out.println(x); }
Understand the concepts of Control Statement
 break statements as a form of goto statement

 Java does not have a goto statement because. It uses the label statement to identify a block of code.
 The syntax is:
{……….
label:
{
}
continue label;
…………
}

29
Understand the concepts of Control Statement
 break statements as a form of goto statement
Example:
public static void main()
{
first: for (int x = 0; x<5; x++){
Second:
for (int k = 0; x<5; k++)
{ if (x ==1 && k ==2)
{
continue first;
System.out.println(x+k);
}
} 30
31

THANK YOU
FOR
LISTENING

THE END

You might also like