Comp 14 control flow if if then
Download
1 / 31

COMP 14: Control Flow: if, if-then - PowerPoint PPT Presentation


  • 146 Views
  • Updated On :

COMP 14: Control Flow: if, if-then. May 26, 2000 Nick Vallidis. Review. We talked about input and output What symbols do we use to force the order of operations? What are the equality operators? What are the relational operators? What are the logical operators?. Let's try this again.

Related searches for COMP 14: Control Flow: if, if-then

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'COMP 14: Control Flow: if, if-then' - morag


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Comp 14 control flow if if then l.jpg

COMP 14: Control Flow: if, if-then

May 26, 2000

Nick Vallidis


Review l.jpg
Review

  • We talked about input and output

  • What symbols do we use to force the order of operations?

  • What are the equality operators?

  • What are the relational operators?

  • What are the logical operators?


Let s try this again l.jpg
Let's try this again

  • There are class methods and what I'll call object methods.

  • Class methods are called using the name of the class:

  • Object methods are called using an instantiated object:

result = Math.abs(-17.2);

String greeting = new String("Hello, ");

greeting = greeting.concat("everyone");


What s the difference l.jpg
What's the difference?

  • The difference is that class methods have static in front of them when they are defined


Today l.jpg
Today

  • Control Flow

  • if statement

  • if-else statement

  • nested if and if-else statements


Back to the cake l.jpg
Back to the cake

  • To bake a cake we need:

    • ingredients

    • recipe

  • Similarly for a program we need:

    • data

    • algorithm


Yep time for the algorithm l.jpg
Yep, time for the algorithm

  • We've already talked some about representing algorithms:

    • assignment

    • expressions

    • methods

  • But, we need to be able to control the flow of the program...


Control flow l.jpg
Control Flow

  • Generally, Java statements are just executed in order, one after another

  • This is pretty dull. We couldn't even make a calculator…

  • It would be nice to be able to control which statements get executed (we need to make decisions!)

p. 110


Good news l.jpg
Good news!

  • Two types of statements that modify flow:

    • conditional statements

    • repetition statements (loops)

  • Conditional Statements

    • if, if-else, switch

  • Repetition Statements

    • while, do, for

p. 110


If if else l.jpg
if, if-else

  • Today, we'll cover these two

  • We'll come back to switch at a later date

  • We'll talk about loops next week


Slide11 l.jpg
if

  • Here is the general form for an if statement:

if (condition)

statement;

p. 111


Slide12 l.jpg
if

  • Here is the general form for an if statement:

if is a Java reserved word

if (condition)

statement;


Slide13 l.jpg
if

  • Here is the general form for an if statement:

condition is a boolean expression

It must evaluate to true or false

if is a Java reserved word

if (condition)

statement;


Slide14 l.jpg
if

  • Here is the general form for an if statement:

condition is a boolean expression

It must evaluate to true or false

if is a Java reserved word

if (condition)

statement;

If the condition evaluates to true, then the statement is executed, otherwise it is skipped


Slide15 l.jpg
if

  • Here is the general form for an if statement:

if (condition)

statement;

This indent is VERY important. The compiler doesn't need it, but it helps people read the program


Example l.jpg
Example

int age = 22;

if (age < 18)

System.out.println("Can't vote!");

System.out.println("Always printed");


If control flow diagram l.jpg

condition

evaluated

true

false

statement

if Control Flow Diagram

if (condition)

statement;


Another example l.jpg
Another example

int year = 2000;

if (year == 2000)

System.out.println("Sydney Olympics!");

System.out.println("Always printed");


Another example19 l.jpg
Another example

int age = 12;

if ((age <=12) || (age >= 65))

System.out.println("Discount!");

System.out.println("Always printed");


If else l.jpg
if-else

  • Here is the general form for an if-else statement:

if (condition)

statement1;

else

statement2;

p. 114


If else21 l.jpg
if-else

  • If the condition is true then statement1 is executed. If the condition is false then statement2 is executed. Never both!

if (condition)

statement1;

else

statement2;


If else diagram l.jpg
if-else Diagram

condition

evaluated

if (condition)

statement1;

else

statement2;

true

false

statement2

statement1


If else example l.jpg
if-else example

int age = 22;

if (age < 18)

System.out.println("Can't vote!");

else

System.out.println("Can vote!");

System.out.println("Always printed");


Block statements l.jpg
Block Statements

  • What if we need to do use more than one statement in an if or if-else?

  • You can use the braces ({ and }) to make a block statement

p. 115


Example25 l.jpg
Example

int age = 22;

boolean canVote = true;

if (age < 18)

{

System.out.println("Can't vote!");

canVote = false;

}

System.out.println("Always printed");


General format l.jpg
General format

{

statement1;

statement2;

.

.

.

statementn;

}


Recommendation l.jpg
Recommendation

  • A block statement doesn't have to contain more than 1 statement

  • I recommend always using the block statement form with if and if-else:

if (age < 18)

{

System.out.println("Can't vote!");

}


Nested if and if else l.jpg
Nested if and if-else

  • The statements you execute could be another if or if-else!


Nested if and if else29 l.jpg
Nested if and if-else

if (age > 12)

{

if (age > 65)

System.out.println("Discount");

else

System.out.println("Normal price");

}

else

System.out.println("Child Discount");



Homework l.jpg
Homework

  • Read 3.1-3.2, 3.4 again

  • Assignment P2 goes out today and is due on Thursday


ad