



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to control structures in java programming, focusing on conditional statements such as 'if', 'if-else', and 'if-else if'. It covers the basics of boolean expressions, truth tables, and nested if-statements, as well as the syntax and explanation of each type of conditional statement. Students will learn how to use these structures to alter the flow of their programs based on certain conditions.
Typology: Study notes
1 / 5
This page cannot be seen from the preview
Don't miss anything!




ESE
1/
Sequential Control Flow
ESE
2/
What are control Structures?
Limitations of sequential control flow
Such programs are extremely limited!
Control structures
allow a program to
base its behavior on certain conditions
ESE
3/
Recap: Boolean
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
ESE
4/
ESE
5/
//Assume
x
is
an
integer
if((x
System.out.println(x
is
even”);
If the condition is true, then the statement(s) (i.e.instructions) will be executed. Otherwise, it/they won’t.
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 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/
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
ESE
14/
What happens if you forget the statement x = x + 1?
We print value 1 forever
Known as
infinite
loop
ESE
15/
ESE
16/
Compute Square of first 10 numbers //In Square.java
System.out.println(num
" + sqNum);
ESE
17/
ESE
18/
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