Chapter 3.1.3 – The Basic If Statement | Introduction to Programming Using Java

Chapter 3.1.3 – The Basic If Statement | Introduction to Programming Using Java

 

3.1.3 The Basic If Statement

 

An if statement tells the computer to take one of two alternative courses of action, depending on whether the value of a given boolean-valued expression is true or false. It is an example of a “branching” or “decision” statement. An if statement has the form:

 

the basic if statement, introduction to programming using java

 

When the computer executes an if statement, it evaluates the boolean expression. If the value is true, the computer executes the first statement and skips the statement that follows the “else”. If the value of the expression is false, then the computer skips the first statement and executes the second one.

Note that in any case, one and only one of the two statements inside the if statement is executed. The two statements represent alternative courses of action; the computer decides between these courses of action based on the value of the boolean expression.

In many cases, you want the computer to choose between doing something and not doing it. You can do this with an if statement that omits the else part:

 

the basic if statement, introduction to programming using java

 

To execute this statement, the computer evaluates the expression. If the value is true, the computer executes the hstatementi that is contained inside the if statement; if the value is false, the computer skips that hstatementi.

Of course, either or both of the hstatementi’s in an if statement can be a block, so that an if statement often looks like:

 

Chapter 3.1.3 - The Basic If Statement | Introduction to Programming Using Java

 

As an example, here is an if statement that exchanges the value of two variables, x and y, but only if x is greater than y to begin with. After this if statement has been executed, we can be sure that the value of x is definitely less than or equal to the value of y:

 

Chapter 3.1.3 - The Basic If Statement | Introduction to Programming Using Java

 

Chapter 3.1.3 - The Basic If Statement | Introduction to Programming Using Java

 

Finally, here is an example of an if statement that includes an else part. See if you can figure out what it does, and why it would be used:

 

the basic if statement, introduction to programming using java

 

I’ll have more to say about control structures later in this chapter. But you already know the essentials. If you never learned anything more about control structures, you would already know enough to perform any possible computing task. Simple looping and branching are all you really need!

 

 

 

 

Read More…

Introduction to Programming Using Java – David J. Eck

Chapter 2 – Names and Things | Introduction to Programming Using Java

Chapter 2.5 – Details of Expressions | Introduction to Programming Using Java

Chapter 2.5.1 – Arithmetic Operators | Introduction to Programming Using Java

Chapter 2.5.3 – Relational Operators | Introduction to Programming Using Java

Chapter 2.6.2 – Command Line Environment | Introduction to Programming Using Java

Chapter 2.6.3 – IDEs and Eclipse | Introduction to Programming Using Java

2 thoughts on “Chapter 3.1.3 – The Basic If Statement | Introduction to Programming Using Java”

Leave a Comment