0% found this document useful (0 votes)
7 views17 pages

5 The If Statement

Uploaded by

740186
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views17 pages

5 The If Statement

Uploaded by

740186
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

The if/else statement

conditions

This is an important lesson


Programs that have conditions
Allow for differing paths as they
offer choices. Think about games
and how one choice in a game
leads to a differing path for the
game/program
The if statement
Executes a block of statements only if a test is true

if (test) {
statement;
...
statement;
}

• Example:
double gpa = console.nextDouble();
if (gpa >= 2.0) { This is how the if condition looks
System.out.println("Application accepted."); in a flowchart, notice how it skips
} yes statement if it’s not true, and
continues on with the program
The if/else statement
Executes one block if a test is true, another if false

if (test) {
statement(s);
} else {
statement(s);
}

• Example:
double gpa = console.nextDouble();
if (gpa >= 2.0) {
System.out.println("Welcome to Mars University!");
} else { The else allows a second
System.out.println("Application denied."); statement to be execute if
} the Yes is not True, this is
where we see choice (this
or that)
Relational expressions
• if statements use logical tests.

if (i <= 10) { ...


• Tests use relational operators:
Operator Meaning Example Value
== equals 1 + 1 == true
2
These are the different
!= does not equal 3.2 != true operators you can use in
2.5 the test
< less than 10 < 5 false
> greater than 10 > 5 true
<= less than or equal to 126 <= false
100
>= greater than or equal to 5.0 >= true
Can you find the Bug in

Misuse of if the code here?

• What's wrong with the following code?


Scanner console = new Scanner(System.in);
System.out.print("What percentage did you earn? ");
int percent = console.nextInt();
if (percent >= 80) {
System.out.println("You got an A!");
}
if (percent >= 70) {
System.out.println("You got a B!");
}
if (percent >= 60) {
System.out.println("You got a C!");
}
if (percent >= 50) {
System.out.println("You got a D!");
}
if (percent < 50) {
System.out.println("You got an F!");
}
...
What would it output if
you typed 82 for your
mark?
What if you want to check
Nested if/else multiple conditions until
one of them is true?

Chooses between outcomes using many tests


if (test) {
statement(s);
} else if (test) {
statement(s);
} else { Else is the default case, if
statement(s); all are false(ifs/else ifs) ,
} else becomes true

• Example:
if (x > 0) {
System.out.println("Positive");
} else if (x < 0) {
System.out.println("Negative");
} else {
System.out.println("Zero");
} Ifs with else ifs allows you
to check many tests. If
one is true it exits the if
Nested if/else/if Sometimes you want to
check if something is true
and have no default case,
• If it ends with else, exactly one path must be taken. no else needed then
• If it ends with if, the code might not execute any path.

if (test) {
statement(s);
} else if (test) {
statement(s);
} else if (test) {
statement(s);
}

• Example:
if (place == 1) {
System.out.println("Gold medal!");
} else if (place == 2) {
System.out.println("Silver medal!");
} else if (place == 3) {
System.out.println("Bronze medal.");
}
Path means statements to
Nested if structures be executed

 exactly 1 path (mutually exclusive)  0 or 1 path (mutually exclusive)


if (test) { if (test) {
statement(s); statement(s);
} else if (test) { } else if (test) {
statement(s); statement(s);
} else { } else if (test) {
statement(s); statement(s);
} }

 0, 1, or many paths (independent tests; not exclusive)


if (test) {
statement(s);
}
if (test) {
statement(s);
}
if (test) {
statement(s);
}
Which nested if/else You must decide which
combination of if/else if
and else is best for YOUR
is best? given problem

• (1) if/if/if (2) nested if/else (3) nested if/else/if


• Whether a user is lower, middle, or upper-class based on income.
• (2) nested if / else if / else

• Whether you made the dean's list (GPA ≥ 3.8) or honor roll (3.5-3.8).
• (3) nested if / else if

• Whether a number is divisible by 2, 3, and/or 5.


• (1) sequential if / if / if

• Computing a grade of A, B, C, D, or F based on a percentage.


• (2) nested if / else if / else if / else if / else
Try this example, lets see if you can do this
• Create a program that asks the users their numerical grade in a class
and then prints how they did with a letter (A,B,C,D,F) and maybe a
comment beside it.

• Use if statements accordingly.


Modify the program to use AND and OR
• Use && to represent and i.e if ( a && b) a and b
4+ 95% - 100%

4 87% - 94%

• Use || to represent or if (a || b) a or b 4- 80% - 86%

3+ 77% - 79%

3 73% - 76%

3- 70% - 72%

• Modify your grade assignment for levels.


2+ 67% - 69%

2 63% - 66%

2- 60% - 62%

1+ 57% - 59%

1 53% - 56%

1- 50% - 52%

R 0% - 49%
Example 2
• Write a program that will input 4 marks, calculate their average and
then tell you if you passed the semester or failed.

• *Hint you can use the flowchart/psuedocode you already created for
this from b4 (boom)
Example 3
• Write a program that gets 2 values from the user and then outputs
which is the bigger number of the two.

• *Hint you can use the flowchart/psuedocode you already created for
this from b4
Example 4
• Write a program that gets 3 inputs from the user (int numbers) and
then outputs the largest of the 3 numbers

• *Hint you can use the flowchart/psuedocode you already created for
this from b4
Example 4b
• Write a program that gets 3 inputs from the user (int numbers) and then
outputs the largest of the 3 numbers

• Modify it so that a tie can also happen

• *Hint you can use the flowchart/psuedocode you already created for this
from b4
Without && and ||
Example 5
• Create the program (from your flowchart) to find the sum of first 50 natural numbers.

• Hint -Use your flowchart from before to solve this


• Also, there is no user input.

• Good Luck

You might also like