Tag Archive for logic

Java Tutorial #8-1 / 8-2 — Using math functions with if logic

Apologies about this being a two-part video, AND for my mic volume being weird, in future videos this is fixed. I’ll consider re-recording this later


Topics Discussed: Using math functions with if-based logic to choose the output of a program

Source Code Available Here

8-2 Here


Topics discussed: Same as above

Source Code Available Here

In these videos I talk a little bit about java.lang.Math; An include that allows us to do some simple mathematical calculations in our code in conjunction with the if-statements we’ve been talking about.


java.lang.Math functions

There are a LOT of different constructs in the java.lang.Math library, but the ones we use in this lesson seem to be the most common of them. Raising a number to a power, and taking the square root of a number. The code for each of these is as follows: (assuming you have the import in your code)


Math.pow(numberOrVariable, powerRaisingTo);
Example:
Math.pow(x, 3); // raises x to the 3rd power.

Math.sqrt(x); //Takes the square root of x.

Java Tutorial #10 — Loop logic / intro


Topics Discussed: While loops, do_while loops, counter controlled loops, loop syntax, loop scoping

Source Code Available Here


Loops
In this video I discuss two or the most important constructs in the entire language of java. In the next lesson I talk a bit more about for loops, which seem to be used a little more than while and do_while which we discussed here.


While loops
While loops are statements that will be repeated until either a break statement is reached, or until the condition is no longer true. Here’s a few examples of loops using while:


//syntax while(condition){ //do stuff }
//Examples below

int i=0;
while(i < 9){
     //do stuff
     i++; //increment i-- if you remove this statement, the loop will run infinitely.
}

boolean isTrue=true;
i=0; //reset 0 before the loop

while(isTrue){//while the boolean is true
     if(i > 10){
          isTrue = false;
}//end if
     i++; // increment i
}//End loop

i=0;//reset 0 before the loop
while (i < 10){
     System.out.println(i); //output the value of i each time through the loop
     i++; // increment i each time through the loop
}

In the examples above you’ll notice that we always have a ‘way out’ of the loop, that’s because there is a common error known as an ‘infinite loop’ in which your code executes endlessly because your loop is unable to exit.

Do_while loops
Do_while loops are the same basic idea as while loops, except that they execute the contents of the loop once before checking the conditional. These are very useful in making users enter certain values for their input, as seen below:


int aValue=0;
do{ // do these instructions
System.out.println("Enter a value 1 or greater for the variable: ");
aValue= input.nextInt();
}while(aValue < 1);//until this condition is no longer true.


Incrementing numbers
Any number that is of a type that can hold a whole number value can be incremented or decremented. In MOST cases you will be using “post-incrementing” which is seen as varName++; however, there are other ways of incrementing numbers:


++i; // pre-increment
i++; // post-increment (most-common)
--i; // pre-decrement
i--; // post-decrement (common while decrementing)

Each time you hit the statement i++;, you increase the value of i by the value of 1.