Docsity

Java Programming for Beginners: Understanding Conditional Statements and Boolean Logic, Study notes of Electrical and Electronics Engineering

Toggle sidebar
bg1
1
Introduction to Programming
with Java, for Beginners
Conditionals(If statements)
ESE112 1
Limitations of sequential programming
Cannot choose whether or not to perform
a command/instruction
Cannot perform the same command more
than once
Such programs are extremely limited!
ESE112 2
Control Structures
Allow a program to base its behavior on
certain conditions
Two kinds:
Conditional (If) Statements
Loop Structures
ESE112 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:
(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

Partial preview of the text

Download Java Programming for Beginners: Understanding Conditional Statements and Boolean Logic and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

Introduction to Programming

with Java, for Beginners Conditionals(If statements)

ESE^

1

Limitations of sequential programming „ Cannot choose whether or not to performa command/instruction „ Cannot perform the same command morethan once „ Such programs are extremely limited!

ESE^

2

Control Structures „^ Allow a program to base its behavior oncertain conditions „^ Two kinds:^ „^ Conditional (If) Statements^ „^ Loop Structures

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:

(25 > 24)^ &&

(12 == 13) //results to

false

(25 > 24)^ ||

//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^ >^ 10)

{ x = x * 2;System.out.println(“x

=^ “^ +^ x); }

“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 statement (contd..)

„^ { } indicates the block of code that will getexecuted given the condition is true „^ You can avoid the curly brace after condition if only onestatement is to be performed^ //Assume

x^ is^ an^

integer if(x^ >^ 0)System.out.println(x

+^ “^ is^ positive”);

ESE^

7

“if-else” statement //Assume^

x^ is^ an^ integer if(x^ >^ 0)

{ System.out.println(x

+^ “^ is^ positive”); } else^ { System.out.println(x

+^ “^ is^ negative”); }

if (condition){statement(s)} else^ (condition){statement(s)}