OBJECT ORIENTED PROGRAMMING MIDTERM
OBJECT ORIENTED PROGRAMMING MIDTERM
false
Control Structures
● Allows us to change the ordering
of how the statements in our
programs are executed
● Types of Control Structures
○ decision control
structures - allows us to
select specific sections of
code to be executed
○ repetition control
structures - allows us to ● If - else if statement
execute specific sections The statement in the else-clause of an
of the code a number of if-else block can be another if-else
times structures.
○ branching statements
● If - statement
specifies that a statement (or block of
code) will be executed if and only if a
certain boolean statement is true
This cascading of structures allows us to
make more complex selections
● Switch - statement
allows branching on multiple outcomes.
● If - else statement
used when we want to execute a certain
statement if a condition is true, and a
where, switch_expression is an integer
or character expression case_selector1,
case_selector2 and so on
Common Errors
● While-loop
is a statement or block of statements
that is repeated as long as some
condition is satisfied
where,
Initialization Expression - initializes
the loop variable
LoopCondition - compares the loop
variable to some limit value
StepExpression - updates the loop
variable
return statement
● used to exit from the current
method
● flow of control returns to the
statement that follows the original
method call
● To return a value simply put the
value (or an expression that
calculates the value) after the
return keyword.
Unlabeled continue statement Arrays
stores multiple data items of the same
data type, in a contiguous block of
memory, divided into a number of slots
Declaring Arrays
Syntax:
Labeled continue statement data_type [ ]identifier_name;
or
data_type identifier_name[ ];
For example
int [ ]ages;
float grades[ ];
Array Instantiation
● After declaring, we must create
JAVA ARRAYS the array and specify its length
with a constructor statement.
Introduction to Arrays ○ Instantiation - in Java,
Suppose we have here three variables this means creation
of type int with different identifiers for ○ Constructor - in order to
each variable instantiate an object, we
int number1; need to use a constructor
int number2; for this. A constructor is a
int number3; method that is called to
number1 = 1; create a certain object.
number2 = 2;
number3 = 3; ● To instantiate an array, write the
new keyword, followed by the
It seems like a tedious task in order to square brackets containing the
just initialize and use the variables number of elements you want the
especially if they are used for the same array to have
purpose
For example, ● Remember the declaration for the
//declaration main method,
int ages[];
//instantiate object public static void main(String[] args)
ages = new int[100];
● The arguments that are passed
or, can also be written as,
to your program are saved into
//declare and instantiate object
an array of String with the args
int ages[] = new int[100];
identifier.
1. Click on Project