Presentation is loading. Please wait.

Presentation is loading. Please wait.

WELCOME to….. The If Statement.

Similar presentations


Presentation on theme: "WELCOME to….. The If Statement."— Presentation transcript:

1 WELCOME to….. The If Statement

2 DECISION MAKING Question: You are driving in your car and it starts to rain. The rain falls on your windshield and makes it hard to see. Should your windshield wipers be on or off?

3 DECISION MAKING A good answer might be: On

4 DECISION MAKING The windshield wipers (and many other things in a car) are controlled with an ON-OFF switch

5 Is it raining? DECISION MAKING The answer to the question is either
Yes (true) or No (false). Is it raining? Yes No

6 DECISION MAKING Is it raining? Yes Turn wipers on No
If the answer is Yes, Follow the line labeled Yes, Do the directions in the box “Turn wipers on", Follow the line to "continue" Is it raining? Yes Turn wipers on No Continue

7 DECISION MAKING Is it raining? No Yes Turn wipers off Turn wipers on
If the answer is NO, Follow the line labeled No, Do the directions in the box “Turn wipers off", Follow the line to "continue" No Is it raining? Yes Turn wipers off Turn wipers on Continue Continue

8 How many ways could you go from "start" to "continue"?
DECISION MAKING Question: On the last slide……. How many ways could you go from "start" to "continue"?

9 There were two paths through the chart
DECISION MAKING A good answer might be: There were two paths through the chart

10 DECISION MAKING The "windshield wiper" decision is a two-way decision (sometimes called a "binary" decision.) It seems small, but in programs -- complicated decisions are made of many small decisions.

11 DECISION MAKING A simple “IF” program could be:
if ( num < 0 ) // is num less than zero? System.out.println("The number " + num + " is negative"); // true-branch else System.out.println("The number " + num + " is positive"); // false-branch

12 DECISION MAKING The words if and else are markers that divide decision into two sections. The else is like a dividing line between the “Yes branch" and the “No branch".

13 DECISION MAKING Another way of saying the same thing
A two-way decision is like picking which of two roads to take to the same destination. The fork in the road is the if statement, and the two roads come together just after the false-branch

14 DECISION MAKING Some Rules
The if statement always asks a question (often about a variable.) If the answer is "true" only the true-branch is executed. If the answer is "false" only the false-branch is executed. No matter which branch is chosen, execution continues with the statement after the false-branch.

15 Using A Flowchart A flow chart shows the overall logic of the program. Most of the details of syntax are left out. It is often helpful to sketch a flowchart when you are designing a program. You can use the flowchart to get the logic correct, then fill in the details when you write the program.

16 DECISION MAKING Curly Braces
To include more than one statement in a branch, enclose the statements with braces, { and }. A group of statements grouped together like this is called a block statement, (or usually, just block.) There can be as many statements as you want in a block. A block can go anyplace a single statement can go. All the statements in the true block are executed when the answer to the question is true. Of course, all the statements in the false block are executed when the answer to the question is false. The false block consists of the block that follows the else.

17 DECISION MAKING Here is how an outline of how make a two-way decision with curly braces: ... statements done before the decision if { .... // true branch …. } else { .... // false branch ( condition ) .... // false branch } ... statements done after the branch comes back together

18 DECISION MAKING - Recap
Here were some details: The condition evaluates to true or false, often by comparing variables and values. The else divides the true branch from the false branch. The statement after the false branch (or false block) always will be executed. A block consists of several statements inside a pair of braces, { and }. The true branch can be a block. The false branch can be a block. There can be as many statements in a block as you need. When a block is chosen for execution, the statements in it are executed one by one.

19 DECISION MAKING Conditions
The condition can compare what is held in a variable to other values. You can use the comparisons: <, >, and so on. (More about these in 3 slides.) The first statement after the false branch will be executed no matter which branch is chosen. The if-else is like a fork in the road, but the road always comes together again.

20 DECISION MAKING Question:
Do you believe that the following section of a program is correct? if ( num < 0 ) System.out.println("The number " + num + " is negative"); else System.out.println("The number " + num + " is positive"); System.out.print ("positive numbers are greater "); System.out.println("or equal to zero "); System.out.println("Good-by for now");

21 DECISION MAKING No. The programmer probably wants the three statements after the else to be part of a false block, but has not used braces to show this.

22 DECISION MAKING Question:
Do you believe that the following section of a program is correct? if (inputNumber == randomNumber) { System.out.println(“***********”); System.out.println(“**You Win**”); } else System.out.println(“**You lose***”); System.out.println(randomNumber + “.”);

23 DECISION MAKING This is a trick question – so look again
if (inputNumber == randonNumber) { System.out.println(“***********”); System.out.println(“**You Win**”); } else System.out.println(“**You lose***”); System.out.println(randomNumber + “.”);

24 DECISION MAKING Yes The condition in an if statement MUST be enclosed in parentheses. However a line like if (inputNumber == randomNumber)  NO SEMI COLON is not a complete statement (just as :If I had a hammer” isn't a complete sentence). So this line shouldn’t end with a semicolon. (Watch this one when you write your code)

25 Boolean Expressions We need to look at the condition part of the if statement. Usually this will be a boolean expression. Recall that an expression is a combination of literals, operators, variables, and parentheses used to calculate a value. A boolean expression is an expression that evaluates to true or false. Boolean expressions often make comparisons between numbers. A relational operator says what comparison you want to make.

26 Boolean Expressions Operator Meaning Example A == B is A equal to B ?
numberOfCows == 5 A < B is A less than B ? numberOfCows < 5 A <= B is A less than or equal to B ? numberOfCows <= 5 A > B is A Greater than B ? myInitial > ‘G’ A >= B is A Greater than or equal to B ? numberOfCows >= 5 A != B is A not equal to B ? buttonClicked != panicButton

27 Boolean Expressions Evaluate Expression Value 25 == 25 ? 25 != 25
25 <= 25 25 > 25 25 >= 25 25 = 25 -5 < 7 -305 <= 97

28 Boolean Expressions A good answer will be: Expression Value 25 == 25
true 25 != 25 false 25 <= 25 25 > 25 25 >= 25 25 = 25 illegal -5 < 7 -305 <= 97

29 Some last minute thoughts
When you place a bunch of statements inside curly braces, you get a block,; and a block behaves, in all respects, like a single statement. For easier reading, IF statements are indented. If you don’t indent your statements in some logical fashion, neither you nor anyone else can usually make sense of your code.

30 One last warning….. When you write IF statements, you may be tempted to throw all the rules about curly braces out the window and rely on indentation. Unfortunately, this seldom works. If you indent three statements after the word else and forget to enclose those statements in curly braces, the computer THINKS that the else part includes only the first of the three statements. What's worse is the indentation misleads you into believing that the else part includes all three statements. This makes it more difficult for you to figure out why your code is not behaving the way you think it should behave… So………. Here is the warning…….

31 One last warning….. WATCH THOSE BRACES!!!!!!!!!


Download ppt "WELCOME to….. The If Statement."

Similar presentations


Ads by Google