Docsity

Java Programming: Understanding Control Structures and Conditional Statements, Study notes of Electrical and Electronics Engineering

Toggle sidebar
bg1
1
Introduction to Programming
with Java, for Beginners
Control Structures
ESE112 1/17
Sequential Control Flow
Consists of just a list of commands to be done
in order
Welcome to Dr. Java
> int num = 2;
> int sqNum;
> sqNum = num * num;
> System.out.println(sqNum); //Or just type:sqNum
2
ESE112 2/17
What are control Structures ?
Limitations of sequential control flow
Cannot choose whether or not to perform a
command/instruction
Cannot perform the same command more
than once
Such programs are extremely limited!
Control structures allow a program to
base its behavior on certain conditions
ESE112 3/17
Recap: Boolean
Boolean is one of the eight primitive types
Only 2 value: true, or false
Booleans are used to make yes or no decisions
All control structures use Booleans
The following expression each give a Boolean result:
(25 > 24) && (12 == 13) //results to false
(25 > 24) || (12 == 13) //results to true
Thus based on certain conditions we can alter
the outcome or flow of the program
pf3
pf4
pf5

Partial preview of the text

Download Java Programming: Understanding Control Structures and Conditional Statements and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

Introduction to Programming

with Java, for Beginners Control Structures

ESE

1/

Sequential Control Flow

„

Consists of just a list of commands to be donein order

Welcome to Dr. Java> int num = 2;> int sqNum;> sqNum = num * num;> System.out.println(sqNum); //Or just type:sqNum 2

ESE

2/

What are control Structures?

Limitations of sequential control flow

„

Cannot choose whether or not to perform acommand/instruction

„

Cannot perform the same command morethan once

Such programs are extremely limited!

Control structures

allow a program to

base its behavior on certain conditions

ESE

3/

Recap: Boolean

„

Boolean

is one of the eight primitive types

„

Only 2 value: true, or false

„

Booleans

are used to make yes or no decisions

„

All control structures use Booleans

„

The following expression each give a

Boolean

result:

(12 == 13) //results to

false

//results to

true

„

Thus based on certain conditions we can alterthe outcome or flow of the program

ESE

4/

Conditionals (“if” statements)

An “if” statement is a

flow control

statement

It is also called a

conditional

, or a branch

We’ll see several “flavors”

An “if” all by itself

An “if” with an “else” part

An “if” with an “else if” part

ESE

5/

//Assume

x

is

an

integer

if((x

System.out.println(x

is

even”);

“if” statement

If the condition is true, then the statement(s) (i.e.instructions) will be executed. Otherwise, it/they won’t.

if (condition){

statement(s)

ESE

6/

“if-else” statement

//Assume

x

is

an

integer

if((x

System.out.println(x

is

even”);

} else

System.out.println(x

is

odd”);

if (condition){

statement(s)

} else

statement(s) }

ESE

7/

Syntax & Explanation

Example

if (condition){

statement(s)

} else {

statements(s)

} If the condition is true,then the statement(s)in the “if block” areexecuted.Otherwise, if there isan “else” part, thestatement(s) in it areexecuted.

//Determine smaller of two integersnumbers & put it variable “min”int min;int x = 5;int y = 10;if (x <= y){

min = x;

} else {

min = y;

}

Example: “if-else” statement

ESE

12/

A “Loop”

„

A simple but powerful mechanism for “making lots ofthings happen!”

„

Performs a statement (or block) over & over

„

Usually set up to repeat an action until some condition issatisfied

„

Computing Scenarios Examples

„

Run an application until user hits “quit” button

„

Read characters until end of file reached

„

Deal card hands until game over

ESE

13/

Syntax of the

while statement

„

condition

is a true/false (boolean) expression

„

If

condition

is initially false, the statement is never

executed

„

If

condition

is true,

statement

is executed and

condition

is re-evaluated

„

The

statement

should eventually make the loop stop

while (

condition

statement(s)

ESE

14/

A

while Loop to Print Numbers

// Print the numbers 1 thru 10int x = 1;while (x <= 10){

System.out.println(x);x = x + 1;

„

What happens if you forget the statement x = x + 1?

„

We print value 1 forever

„

Known as

infinite

loop

ESE

15/

More

Infinite Loops

// Some infinte loops are intentionalwhile

(true){

statement(s)

} // Others are notint

x

while

(x

statement(s)

which

don’t

change

x

ESE

16/

Compute Square of first 10 numbers //In Square.java

int

num

int

sqNum

while

(num

sqNum

num

num;

System.out.println(num

" + sqNum);

num

num

ESE

17/

For Loop

for (init; end-test; re-init){

statement

„

Executes loop body as long as

end-test

evaluates to TRUE

„

Initialization and re-initializationcode included in loop statement

„

Note: Test is evaluated

before

executing loop

body

ESE

18/

While vs. For

Explanation

Code

An example ofa while loopthat has thispattern

int x = 1;while (x <= 10){

System.out.println(x);x = x + 1;

}

A for loopthat does thesame thing

for (int x = 1; x <= 10; x = x + 1){

System.out.println(x);

}^ Note:

For

loops are used generally for bounded iteration

ESE

19/

for (expr1; condition; expr3){

statement(s)

}

for

Syntax

Type of Loop

while (condition){

statement(s)

}

while

Summary of Loops