COMP 202 Lecture Notes - Lecture 4: Infinite Loop, While Loop, 5,6,7,8

34 views6 pages
COMP 202: Foundations of Programming
Lecture 4: Loops and Arrays
If-If vs. If-Else:
The difference between these two statements is that in order for the one on the left-hand side to work, (not
condition) must become true in the midst of the statement in order for the command to be processed
int x = 1;
if (x > 0){
System.out.println(“Positive. Altering the value.”);
x--;
}
if (x <= 0){
System.out.println(“Not positive.”);
}
This code would print:
Positive. Altering the value.
Not positive.
int x = 1;
if (x > 0){
System.out.println(“Positive. Altering the value.”);
x--;
}
else (x <= 0){
System.out.println(“Not positive.”);
}
This code would print:
Positive. Altering the value.
If-else If-else statements:
For when you have a more complicated set of conditions with 2+ options
Option 1: Nested if-else statements:
if (condition 1){
//commands
} else{
if(condition 2){
//commands
} else{
if (condition 3){
//commands
}
}
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 6 pages and 3 million more documents.

Already have an account? Log in
COMP 202: Foundations of Programming
}
Option 2: All at the same level:
if (condition 1){
//commands
} else if (condition 2){
//commands
} else if (condition 3){
//commands
} else {
//commands
}
Order Matters:
Much like else, the “else if” is only executed if the prior conditions were false. So, there can be a
big difference if the order of the options is changed
Omitting Curly Brackets:
People do it. Don’t do it.
The Math Library:
Java comes with a math library. There are many, many methods that can be used from this library. Also
contains two useful point values, e and pi, which can be accessed by Math.E and Math.PI
Some useful methods:
Math.abs(x) absolute value of: |x|
Math.sqrt(x) square root of: √x
Math.pow(x,y) x to the power of y: xy
Math.sin(x) trig sine of x, where x is in radians
Math.ceil(x) smallest integer that is greater than or equal to the argument
If statements let us choose whether to execute some code, or to choose between multiple blocks of code to
execute. Another way to control the flow is to execute something multiple times
As a method to execute something, copy-pasting is less than ideal:
- annoying
- error prone
- difficult to maintain (hard to edit to fix mistakes)
- does not generalize
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 6 pages and 3 million more documents.

Already have an account? Log in
jc123 and 40170 others unlocked
COMP 202 Full Course Notes
100
COMP 202 Full Course Notes
Verified Note
100 documents

Document Summary

The difference between these two statements is that in order for the one on the left-hand side to work, (not condition) must become true in the midst of the statement in order for the command to be processed. Int x = 1; if (x > 0){ if (x <= 0){ Int x = 1; if (x > 0){ else (x <= 0){ For when you have a more complicated set of conditions with 2+ options. Option 1: nested if-else statements: if (condition 1){ Option 2: all at the same level: if (condition 1){ Much like else, the else if is only executed if the prior conditions were false. So, there can be a big difference if the order of the options is changed. There are many, many methods that can be used from this library. Also contains two useful point values, e and pi, which can be accessed by math. e and math. pi.

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents