CSE 14 Lecture Notes - Lecture 5: Boolean Expression

76 views5 pages
21 May 2018
School
Course
Professor
CMPS12A Lecture 5 Conditional Operators: If-Else Statements
The programmer has the choice of making code in the program function with certain conditions
met, thus you have control of whether you want this to work, that to work, etc. We know of
sequential code which is just putting code in the program and following its instruction in order
of source of code. There is conditional code, which is what we are talking about right now, and
then there is iterative code or looping that basically repeats a set of instructions until a Boolean
expression finally becomes true or false.
Example:
Relational Ops
Example
Value
<
5 < 10
true
<=
5 <= 10
true
>
5 > 10
false
>=
5 >= 10
false
==
5 == 10
false
!=
5!= 10
true
These relational ops, or Boolean expressions were named in the previous lecture, so check
them out. Anyways, these operations produce the Boolean value of true or false depending on
what the condition is, resulting in many ways of controlling the code that you want to work and
what you do’t.
There is also ways of combining two conditions in the same line for whenever you want two
things to be true or false, thus proceeding with the code.
Logical Ops symbol Example
and && (0 < 5) && (j < i) T or F depending on variables i and j
or || (0 < 5) || (3 > 4) T
not ! !(1 < 2) F
So, if we had variables A and B, we would make conditions where:
(!A && B) || (A && !B) or A != B
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

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

Already have an account? Log in
Now that we are getting to understand how conditions are used, lets take a look in how to use
them. That is where if statements come into play.
Example:
if (condition) { //start of condition
//something
//something //in between here is what we call the true branch, between the { }
} //end of condition
//something, here is not in the if statement anymore, so no condition here
Something to note when only one statement is inside an if operation, is that braces are
optional. We only put braces or { } when there is more than one line of code inside, but say we
have something like this:
if (condition)
stmnt;
stmnt;
So here, only one thing was inside the if operation, which is equal as to say:
if (condition) {
stmnt;
}
stmnt;
Example:
int i = 4;
if (i < 10)
System.out.println(true;
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

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

Already have an account? Log in

Document Summary

Cmps12a lecture 5 conditional operators: if-else statements. The programmer has the choice of making code in the program function with certain conditions met, thus you have control of whether you want this to work, that to work, etc. We know of sequential code which is just putting code in the program and following its instruction in order of source of code. There is conditional code, which is what we are talking about right now, and then there is iterative code or looping that basically repeats a set of instructions until a boolean expression finally becomes true or false. These relational ops, or boolean expressions were named in the previous lecture, so check them out. Anyways, these operations produce the boolean value of true or false depending on what the condition is, resulting in many ways of controlling the code that you want to work and what you do(cid:374)"t.

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