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

An introduction to programming with java for beginners, focusing on the use of conditional statements (if statements) and boolean logic. It covers the limitations of sequential programming, the concept of control structures, and the use of boolean values in making decisions. The document also includes examples of 'if' statements, 'if-else' statements, and 'if-else if' statements, as well as style rules for indentation and spacing.

Typology: Study notes

Pre 2010

Uploaded on 03/28/2010

koofers-user-hfm
koofers-user-hfm 🇺🇸

9 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

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)}