If statements.
We start with one way if statements, meaning if a statement is true we execute the code, and if the statement is false we skip the code all together.
In Java the syntax works like this.
if (boolean expression) {
do this code inside of the blocks
}
You can in Java skip the code blocks if its only one statement or line of code. (i always put them in to avoid any confusion on my part)
A two way if statement executes one set of instructions if it the boolean expression is true, and executes a different set of code if the expression is false.
the syntax looks like this
if (boolean expression){
do this code
}
else{
do this code
}
you can also nest an if statement inside of an if statement. Meaning that you can simply put an if or if else statement in the block containing the code to be executed in an if or else block.
Ruby works exactly the same you just leave out the method blocks and the parenthesis that surround the boolean expression.
if
do this
end
if
do this
else
do this
end
or you can even do
if
do this
elseif
do this
else
do this
end
The text then makes an improvement on the math quiz with our new found if statement knowledge. The new quiz is subtraction instead of addition and lets you know the answer if you are wrong.
here is the code in Java
import java.util.Scanner;
public class SubQuiz{
public static void main(String args [ ] ){
// below is a diff method for getting a random #
int number1 = (int) (Math.random( )*10);
int number2 = (int) (Math.random( )*10);
// our if statement assigns the highest # to the first integer
if (number1 < number2){
int temp = number1
number1 = number2
number2 = temp
}
System.out.print (" what is" + number1 + "-" + number2 + "?");
Scanner input = new Scanner(System.in);
int answer = input.NextInt( );
// above makes new scanner object and turns input from string to int
// if the number that was input matches the correct answer our boolean statement is true and
the system outputs you are correct.
if (number1 - number2 == answer)
System.out.println ("you are correct");
else
System.out.println ("you are wrong" + number 1 + "-" + number2 + "should be" +(number1 - number2 );
// else it prints out you are wrong and the correct anwer
}
}
and here is my ruby code for the same thing.
class SubQuiz
number1 = rand(10-1)
number1 = number1.to_i
number2 = rand(10-1)
number2= number2.to_i
if
number1 < number2
temp = number1
number2 = temp
end
puts ( "what is #{number1} - #{number2} ?")
answer1 = gets
answer1 = answer1.to_i
answer2 = number1 - number2
if
(answer1==answer2)
puts "You are correct!"
else
puts "wrong, correct answer should be #{answer2}"
end
end
pretty self explanatory, if else is the same without the brackets.
Next we will get into some Logical operators and switch statements, then to the chapter 3 exercises.
Also hopefully i will come up with a blog with details about some of the gui stuff i mentioned earlier as well as some other problem solving exercises i uncovered whilst surfing the web.
Next semester starts in less than a week, hopefully i can keep posting to this blog without too many lengthy breaks.
No comments:
Post a Comment