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

OBJECT ORIENTED PROGRAMMING MIDTERM

The document provides an overview of Java control structures, including decision control (if, else, switch), repetition control (while, for, do-while), and branching statements (break, continue, return). It also discusses common errors associated with these structures and introduces arrays and command-line arguments in Java. Additionally, it includes examples and syntax for declaring, instantiating arrays, and handling command-line arguments in Java applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

OBJECT ORIENTED PROGRAMMING MIDTERM

The document provides an overview of Java control structures, including decision control (if, else, switch), repetition control (while, for, do-while), and branching statements (break, continue, return). It also discusses common errors associated with these structures and introduces arrays and command-line arguments in Java. Additionally, it includes examples and syntax for declaring, instantiating arrays, and handling command-line arguments in Java applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

JAVA CONTROL STRUCTURE different statement if the condition is

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

DECISION CONTROL STRUCTURE

● 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.

Where, boolean_expression is either a


boolean expression or boolean variable

● 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

1. The condition inside the if-statement


does not evaluate to a boolean value.
For example,

The variable number does not hold a


boolean value.

2. Writing elseif instead of else if.

3. Using = instead of == for comparison.


For example, Repetition Control Structures

● While-loop
is a statement or block of statements
that is repeated as long as some
condition is satisfied

The statements inside the while loop are


executed as long as the
boolean_expression evaluates to true
● For-loop
Sample Programs allows execution of the same code a
number of times.

where,
Initialization Expression - initializes
the loop variable
LoopCondition - compares the loop
variable to some limit value
StepExpression - updates the loop
variable

● Do-while loop Sample Programs


statements inside a do-while loop are
executed several times as long as the
condition is satisfied

The main difference between a while


and do-while loop is that the statements
inside a do-while loop are executed at Branching Statements
least once ● allows us to redirect the flow of
program execution
● Java offers three branching
statements:
○ Break
○ Continue
○ Return

Sample Programs Unlabeled break statement


● terminates the enclosing switch
statement, and flow of control
transfers to the statement
immediately following the switch
● this can also be used to terminate
a for, while, or do-while loop
labeled break statement ● For example,
● terminates an outer statement, return ++count;
which is identified by the label or
specified in the break statement return "Hello";
● the flow of control transfers to the ● The data type of the value
statement immediately following returned by return must match
the labeled (terminated) the type of the method's declared
statement return value
● The sample program in the next ● When a method is declared void,
slide searches for a value in a use the form of return that doesn't
two dimensional array. Two return a value
nested for loops traverse the ● For example,
array. When the value is found, a return;
labeled break terminates the
statement labeled search, which Examples
is the outer for loop
Unlabeled break statement
Unlabeled continue statement
● skips to the end of the innermost
loop's body and evaluates the
boolean expression that controls
the loop, basically skipping the
remainder of this iteration of the
loop

Labeled continue statement


● skips the current iteration of an
outer loop marked with the given
label labeled break statement

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.

COMMAND-LINE ARGUMENTS In the previous example, the


command-line arguments passed to the
● A Java application can accept
Sort application is an array that contains
any number of arguments from
five strings which are: "5", "4", "3", "2"
the command-line.
and "1".
● Command-line arguments allow
the user to affect the operation of You can derive the number of
an application for one invocation. command-line arguments with the
● The user enters command-line array's length attribute.
arguments when invoking the
application and specifies them For example,
after the name of the class to run.
● For example, suppose you have int numberOfArgs = args.length;
a Java application, called Sort,
If your program needs to support a
that sorts five numbers, you run it
numeric command-line argument, it
like this:
must convert a String argument that
represents a number, such as "34", to a
number.

Here's a code snippet that converts a


command-line argument to an integer,
● In the Java language, when you
invoke an application, the runtime int firstArg = 0;
system passes the command-line if (args.length > 0)
arguments to the application's {
main method via an array of firstArg = Integer.parseInt(args[0]);
Strings. }
● Each String in the array contains
one of the command-line parseInt throws a
arguments. NumberFormatException (ERROR) if
the format of args[0] isn't valid (not a 2. Right-click on the
number). CommandLineExample icon, and
a popup menu will appear
To illustrate on how to pass some
arguments to your programs in
NetBeans, let us create a Java program
that will print the number of arguments
and the first argument passed to it.

Now, run netbeans and create a new


project and name this
CommandLineExample.

Copy the code shown above and


compile the code. 3. Click on Run-> Running Project
Now, follow these steps to pass
arguments to your program using
NetBeans.

1. Click on Project

4. On the Arguments textbox, type


the arguments you want to pass
to your program. Then click OK.
5. Now try to RUN your program

As you can see here, the output to your


program is the number of arguments
which is 5, and the first argument which
is 5
package exer2_mid;

public static void main (String[] argos)

You might also like