Conditional Statements - CMU School of Computer Science
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
boolean_expression
false
true
statement
Summer 2010
15-110 (Reid-Miller)
4
if-Statement Examples
!if (count > 0) !
average = total / count;!
Or simply
if (age >= 26) !
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
................
................
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 searches
- high school computer science curriculum
- high school computer science projects
- list of computer science topics
- benefits of computer science degree
- history of computer science pdf
- fundamentals of computer science pdf
- benefits of computer science career
- benefits of computer science education
- doctor of computer science salary
- examples of computer science math
- list of computer science journals
- examples of computer science projects