Boolean Expressions, If Statements and Loops

Boolean Expressions

 
A boolean expression is evaluated by using certain operators to come to a true or false answer. The operators are:
 
== – Returns true if the values on either side are equal
 
!= – Returns true if the values on either side are not equal
 
< – Returns true if the value on the left is less than the value on the right
 
<= – Returns true if the value on the left is less than or equal to the value on the right
 
> – Returns true if the value on the left is greater than the value on the right
 
>= – Returns true if the value on the left is greater than or equal to the value on the right
 
&& – (And) Returns true if both values are true.
 
|| – (Or) Returns true if either value is true
 

If Statements

 
An if statement is used when the user wants to have the code make a logical decision. For example, if the user wanted to have the program perform one action given a certain value and another given another value, they would use an if statement.
 
An if statement is set up like so:
 
if(condition){
do this code;
}
 
This means that if the condition is read as true, then it will read the code in the brackets. If false, it will not. The condition can be either a boolean variable, the word true or false or an expression. This leads to if statements like so:
 
If statement using boolean expression
 

There are three different ways to use an if statement:

 

Just an if statement (or multiple):

 
With just an if statement, it either reads the code or it doesn’t. This happens for each statement. It may even read all the code, if the conditions are all true.
 

Using an else statement:

 
The else statement allows the user to have two options. This way it either reads the first code or the second but not both.
 
if else statement boolean expression
 

Using an else if statement:

 
The else if allows the user to have another condition that builds on top of the negation of its predecessors.
 
Else if statement with boolean expression
 

Loops

 
Loops are a part of programming that allow the programmer to repeat a certain action multiple times. This becomes very useful when writing large amounts of code where a lot of it is repeated. Instead of writing out the same code multiple times in a row, you can put it in a loop which will repeat it until some condition is met, to make it exit the loop. This condition is a boolean statement.
 

Four Important Types of Loops

 

While Loop:

 
A while loop is the most basic loop that you can use. It is designed so that it can be run for an indeterminate amount of time. This means that you don’t need to know how many times the code needs to be run. You just need to know the exit condition.
 
It is set up like so:
 
while (condition){
do this code;
}
 

Do While Loop:

 
A do while loop is essentially the same as a while loop, except a do while runs the code before it checks the condition. Then it decides if it will do it again.
 
It looks like this:
 
do{
code;
}while(condition);
 
Note: the semicolon after the condition is essential.
 

For Loop:

 
A for loop allows the programmer to define the number of times it loops. This makes it essentially a while loop with a counter, with the condition giving a number that the counter is counting up to.
 
If we were to implement a for loop as a while loop it would look like this:
 
int counter = 0;
while(counter<end-condition){
do this code;
counter = counter +1;
}
 
Note: when it comes to incrementing a numeric variable there are three ways that you can do it. The first way is shown above “counter = counter +1;”. The next way is to concatenate the middle section. You can do this by saying “counter += 1;”. By saying “+=” you are saying add the thing after the “+=” to the original and save it back there. The third way is even shorter, but only works for increments of 1. You would write that like so: “counter++;”. All three of these can also be used to decrement (-) a variable, but the first two can be used for division and multiplication as well.
 
Given what a for loop looks like in terms of a while, we can now take a look at the actual way to write a for loop:
 
for(int counter=0; counter<end_condition; counter++){
do this code;
}
 
Note: since writing the word “counter” out multiple times can become tiresome, and programmers enjoy being efficient, most agree that the variable names “i”, “j” and “l” are reserved as your standard counter names.
 
So say you wanted to loop through some code 5 times, you would write:
 
for(int i=0; i<5; i++){
do this code;
}
 
It is the programming standard to initialize your variables at 0. This has to do with certain data structures having their first variable stored in position 0.
 
It is also perfectly correct to write:
 
for(int i=1; i<=5; i++){
do this code;
}
 
This will do the exact same thing unless you are referencing the variable in the inside code. This is because you are not changing the boolean expression.
 

For Each Loop:

 
A for each loop is a very useful type of loop. It allows you to perform the same task to an entire list of variables. It is organized like so:
 
for(String word : list){
do this code;
}
 
This is useful if you want to perform the same action to each thing in a list without knowing the size of the list beforehand.
 

Summary

 
Booleans, if statements, and loops are very useful aspects of coding. To fully realize the potential of if statements and loops, you need a firm grasp of logic and conditional statements.
 

Please follow and like us:
Facebook
Facebook
Google+
Google+
http://aiclarke.com/2015/12/26/boolean-expressions-statements-loops">
YouTube
LinkedIn
RSS