Ch5-Conditionals - Java Software Solutions Chapter 5...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Java Software Solutions Chapter 5 – Conditionals and Loops 1 Miguel A. Labrador Department of Computer Science & Engineering [email protected] http://www.csee.usf.edu/~labrador Dr. Miguel A. Labrador Conditionals and Loops Now we will examine programming statements that allow us to: – make decisions – repeat processing steps in a loop 2 Copyright© Dr. Miguel A. Labrador Conditionals and Loops– 2 This chapter focuses on: – boolean expressions – conditional statements – comparing data – repetition statements Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement execution through a method is linear One statement after another in sequence 3 Copyright© Dr. Miguel A. Labrador Conditionals and Loops– 3
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Flow of Control Some programming statements allow us to: – decide whether or not to execute a particular statement – execute a statement over and over, repetitively These decisions are based on boolean expressions (or 4 Copyright© Dr. Miguel A. Labrador Conditionals and Loops– 4 These decisions are based on (or conditions ) that evaluate to true or false – For example, num > 5 Conditional Statements •A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic decisions 5 Copyright© Dr. Miguel A. Labrador Conditionals and Loops– 5 The Java conditional statements are the: if statement if-else statement switch statement The if Statement 6
Background image of page 2
3 The if Statement The if statement has the following syntax: if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false. 7 Copyright© Dr. Miguel A. Labrador Conditionals and Loops– 7 if ( condition ) statement ; If the condition is true, the statement is executed. If it is false, the statement is skipped. Logic of an if Statement condition evaluated 8 Copyright© Dr. Miguel A. Labrador Conditionals and Loops– 8 statement true false Boolean Expressions A condition often uses one of Java's equality operators or relational operators , which all return boolean results: == equal to 9 Copyright© Dr. Miguel A. Labrador Conditionals and Loops– 9 != not equal to < less than > greater than <= less than or equal to >= greater than or equal to
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Equality Operator and Assignment Operator Note the difference between the equality operator ( == ) and the assignment operator ( = ) num == 4 is a comparison: the boolean expression has the 10 Copyright© Dr. Miguel A. Labrador Conditionals and Loops– 10 value true or false num = 4 is an assignment: the expression has the value 4 because that’s the value of the left side A Mistake You’ll Probably Make What if you accidentally drop an equals sign in the boolean expression?
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/03/2010 for the course COP COP2510 taught by Professor Mlag. during the Fall '10 term at University of South Florida.

Page1 / 18

Ch5-Conditionals - Java Software Solutions Chapter 5...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online