If Statements Flashcards
Write a program that allows the user to enter their age. If the age is 18 or older, print ‘You are eligible to vote.’ Otherwise, print ‘You are not eligible to vote.’
Write an example of an if, else if, and else statement
if (intVariable = 0){
con.println(“1 solution”);
}else if (intVariable > 0){
con.println(“2 zeros”);
}else{
con.println(“no zeros”);
}
Function of Logic Statements
take two or more numbers/variables
compare
based on comparison, perform an action (or don’t)
||
OR logic symbol
Math Logic Comparison Symbols
== exactly equals to
!= not equal to
<= less than or equal to
>= greater than or equal to
< less than
> greater than
**note: the above symbols compare 2 numbers
Function of subString(#1, #2)
grabs specific letters from a string
** note: putting one number will not change the string
#1 is inclusive, #2 is exclusive (i.e. first letter is #0 and so on…)
AND logic symbol
&&
alternate condition check
else if statement
OR logic symbol
||
Write the general If Statement structure
if(somecomparisonconditionistrue){
//run the code within the squiggly brackets
}
Function of “else if” Statement
an optional alternate condition check:
checks the statement/condition following the else if when the above if and/or else if statements are false
runs the code within the { } if true
String Logic Comparison symbols
.equals()
.equalsIgnoreCase()
.length()
Write a program that will let you enter 3 sides of a triangle. If each of the three sides is less than half the perimeter, then a triangle can be formed (e.g., 2, 3, 1000 cannot form a triangle). If a triangle can be formed, find the area of the triangle using Heron’s Formula. If no triangle can be formed, say: ‘Error, can’t make a triangle’.
Write an example of a subString
strVariable = strWord.substring(0,2);
// code takes the first 2 letters of the word in the variable “strWord”
The students of your class are to be subdivided into 3 teams according to their first name. Names starting with A-I belong to team 1, Names starting with J-P belong to team 2, and Names starting with Q-Z belong to team 3. Write a program that lets you enter a name and place the person on a team. Ex: Moe belongs to team 2.