Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
97 views

Module 4 Control Structures and Arrays

This document discusses Java control structures and arrays. It describes if/else statements, switch/case statements, while loops, do-while loops, for loops, and foreach loops. It explains how to declare, initialize, store values in, and access elements of single and multi-dimensional arrays. Control structures like break, continue, and labels are also covered for further controlling loop statements.

Uploaded by

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

Module 4 Control Structures and Arrays

This document discusses Java control structures and arrays. It describes if/else statements, switch/case statements, while loops, do-while loops, for loops, and foreach loops. It explains how to declare, initialize, store values in, and access elements of single and multi-dimensional arrays. Control structures like break, continue, and labels are also covered for further controlling loop statements.

Uploaded by

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

Object Oriented Programming

(Java)
Module 4
Control Structures and Arrays
Understand the different control structures and their
functions in programs

Learn to manage array elements using control structures


Control Structures

4.1

Control Structures and Arrays


JAVA CONTROL STRUCTURES

if, else statement


switch, case statement
while loop
do, while loop
for loop

• Flow Controls
if-else Statement
The if statement enables your program to
selectively execute other statements, based
on some criteria.
The else statement performs a different set of
statements if the expression is false.
Syntax:
if(condition/boolean expression) {
//codes to execute if the condition is true
}
else {
//codes to execute if the condition is false
}
Control Structures and Arrays
switch Statement
The switch statement is used to conditionally
perform statements based on an integer
expression.
Syntax:
switch(varName)
{
case const1: codes here; break;
case const2: codes here; break;
default: codes here; break;
}

Control Structures and Arrays


Keywords

The break statement causes the program


flow to exit from the body of the switch
construct.

Each case label takes only a single


argument, but when execution jumps to one
of these labels, it continues downward until it
reaches a break statement.

Control Structures and Arrays


Keywords

The default keyword is comparable to


the else part of the if statement.

The statements associated with this


keyword is executed if the value of the
switch variable does not match any of the
case constants.

Control Structures and Arrays


while Statement

The while loop executes a given statement


or block of statements repeatedly as long as
the value of the expression is true.
Syntax:
while(condition/expression) {
//codes to execute if condition is true
}

Control Structures and Arrays


do-while Statement

The do-while loop facilitates evaluation of


condition or expression at the end of the
loop to ensure that the statements are
executed at least once .
Syntax:
do{
//codes to execute if condition is true
} while(condition/expression);

Control Structures and Arrays


for Statement

The for loop executes a given statement or


a block of statements for a definite number
of times.

Syntax:
for (initialization; condition; altering list) {
//codes to execute if condition is true
}

Control Structures and Arrays


foreach Statement

The foreach loop is for traversing items in a


collection. foreach is usually used in place
of a standard for statement. It usually
maintain no explicit counter: they essentially
say "do this to everything in this set", rather
than "do this x times".

Syntax:
for(initialization : [collection/array]) {
//codes to execute if condition is true
}
Control Structures and Arrays
foreach Loop Example

int quizzes[ ] = {100,90,80};


for (int grade : quizzes) {
System.out.print(grade+”\t”);
}

OUTPUT:
100 90 80

Control Structures and Arrays


The following are used to further control the
loop statements:

The break statement causes the program


flow to exit prematurely from the body of the
loop statement.

Syntax: break [label];

Control Structures and Arrays


The following are used to further control the
loop statements:

The continue statement causes the


program flow to skip over and jump to the
end of the loop body, and then return the
control to the loop control statement.

Syntax: continue [label];

Control Structures and Arrays


The following are used to further control the
loop statements:

The label identifies any valid statement to


which control must be transferred

Syntax: [label:] statements;

Control Structures and Arrays


Arrays

4.2

Control Structures and Arrays


ARRAY

Ø An array is a structure that holds multiple


values of the same type.

Ø The length of an array is established when


the array is created (at runtime).

Ø After creation, an array is a fixed-length


structure.

Control Structures and Arrays


First index
0 1 2 3 4 5 6 7 8 9

Array length is 10

Element at index 8

Control Structures and Arrays


Array Declaration
Syntax:
type arrayName[ ] = new type[length];

Example:
String names[ ] = new String[5];
int [ ]grades = new int[15];
double[ ] grades;
grades = new double[2];

Control Structures and Arrays


Array Initialization
Syntax:
type arrayName[ ] = {value,value,…};

Example:
String names[ ] ={“maria”,”blanco”,”forever”};

Control Structures and Arrays


Storing Values in an Array
Syntax:
arrayName[index] = value;

Example:
names[0] = “John Doe”;
names[1] = “Jane Doe”;

Control Structures and Arrays


Multi-dimesional Array
Syntax:
type[ ][ ] = new type[row][column];

Example:
int[ ][ ] = new int[5][4];

Control Structures and Arrays


Backiel, A. (2015). Beginning Java Programming: The Object-oriented Approach. 1st Edition
Fain, Y. (2015). Java programming 24-hour trainer. John Wiley and Sons:Indianapolis, IN
Smith, J. (2015). Java programs to accompany Programming logic and design. Cengage Learning:Boston, MA
Yener, M (2015). Professional Java EE Design Patterns. John Wiley & Sons, Inc.
Gordon, S. (2017). Computer Graphics Programming in opengl with Java. Mercury Learning and Information
Butler, A (2017). Java. s.n
Lowe, D (2017). Java all-in-one for dummies. John Wiley and Sons
Saumont, P (2017). Functional Programming in Java: How Functional Techniques Improve Your Java Programs. 1st Edition
Heffelfinger, D. (2017). Java EE 8 Application Development. Packt Puublishing
Murach, J. (2017). Murach’s Java Programming. Mike Murach & Associates,Inc.
Burd, B. (2017). Beginning programming with Java for dummies. John Wiley & Sons, Inc.
Manelli, L. (2017). Developing java web application in a day. s.n.
Klausen, P. (2017). Java 5: Files and Java IO software development (e-book)
Klausen, P. (2017). Java 3 object-oriented programming software development (e-book)
Muller, M (2018). Practical JSF in Java EE 8. Apress

You might also like