Conditional Statements
[Pages:28]Conditional Statements
15-110 Summer 2010 Margaret Reid-Miller
Conditional statements
? Within a method, we can alter the flow of control (the order in which statements are executed) using either conditionals or loops.
? The conditional statements if, if-else, and switch allow us to choose which statement will be executed next.
? Each choice or decision is based on the value of a boolean expression (also called the condition).
Summer 2010
15-110 (Reid-Miller)
2
The if statement
? If we have code that we sometimes want to execute and sometimes we want to skip we can use the if statement.
? The form of the if statement is if (boolean_expression) statement
? If boolean_expression evaluates to true, then statement is executed.
? If boolean_expression evaluates to false, then statement is skipped.
? Note that the boolean_expression enclosed in parentheses must evaluate to true or false.
Summer 2010
15-110 (Reid-Miller)
3
The if Flowchart
false boolean_expression
true statement
Summer 2010
15-110 (Reid-Miller)
4
if-Statement Examples
!if (count > 0) !
average = total / count;!
if (age >= 26) !
Or simply hasLicense
!if (hasLicense == true)!
!System.out.println("You may rent a car.");!
daysInFeb = 28;! ! if (isLeapYear) {!
daysInFeb = 29;! System.out.println(year + " is a leap year.");! }!
Summer 2010
15-110 (Reid-Miller)
5
The if Statement
? The statement in the if statement can be any Java statement: ? A simple statement ? A compound statement, such as an if statement ? A block statement, a group of statements enclosed in braces {}
if (zipcode == 15213) {! Proper indentation city = "Pittsburgh";! becomes essential!
state = "PA";! }!
Summer 2010
15-110 (Reid-Miller)
6
The if-else Statement
? If we want to choose between two alternative we use the if/else statement:
if (boolean_expression)! statement1!
else ! ! statement2!
? If boolean_expression evaluates to true, then statement1 is executed.
? If boolean_expression evaluates to false, then statement2 is executed.
Summer 2010
15-110 (Reid-Miller)
7
The if-else Flowchart
true
false!
boolean_expression
statement1
statement2
Summer 2010
15-110 (Reid-Miller)
8
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- in times of critical need contents from unopened single
- chapter 2 2 conditional statements
- redcap branching logic 2017 201 iths
- three ways to prove if a then b
- creating effective rule statements
- chapter 2 basic ladder logic programming
- conditional statements
- nested ifs if and if or teach ict
- if statement and conditions
- flow chart of if else
Related searches
- conditional statements verilog
- conditional statements and equivalence
- conditional statements and equivalence quiz
- conditional statements venn diagram worksheet
- conditional statements quizlet
- conditional statements in word
- multiple conditional statements in excel
- conditional statements and equivalence quizlet
- conditional statements examples
- conditional statements geometry definition
- conditional statements geometry pdf
- conditional statements worksheets and answers