CSE 14 Lecture Notes - Lecture 5: Boolean Expression
![](https://new-preview-html.oneclass.com/zGLBOE8nxeoljaxpYMlRmAWakpZ9Mbwd/bg1.png)
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
![](https://new-preview-html.oneclass.com/zGLBOE8nxeoljaxpYMlRmAWakpZ9Mbwd/bg2.png)
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
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.