Condition Statements

Condition statements, also sometimes referred to as control statements are a very essential part of programming. Condition statements are the decision points in code. When your program gets to a condition statement it checks to see whether a requirement is met before it moves forward.


The most commonly used control statements are if & else statements.

A good example of this is a program that checks for a valid pin number. If the user enters the right PIN number the program will let the user know that the PIN is right otherwise it will tell the user that the PIN is wrong.

This program checks if the statement following the if is true. If the value of the pinNumber variable is 9606 the program will do what is in the ‘if’ block, otherwise it will do what is in the else block.

If we combine else & if, it would work just like we would expect it to work
Let’s see an example that uses an ‘else if’ statement to check a person’s age range

When this program starts it checks to see if the age is less than 13, if it is then it prints Child and stops, if not it will jump to the next block, if the age is less than or equal to 19 then it will print Teenager, if not it will move on to the next block to check if age is under 65, if so it will print Adult, but if the conditions are met then the program will print Senior

Program -> Write a program that gets a user’s number grade and prints a letter grade Ex. if input is 90 the output will be A. 100-85 is A, 84-70 is B, 69-60 is C, 59-50 is D, anything below 50 is F

*you should use ==, >, <, !=, etc. to compare numerical values only, to check if two strings are equal use string1.equals(string2); to check if two strings are not equal we use the ! ‘not’ symbol like this !string1.equals(string2) ;

 

Ex this program will check if two strings do not match

If the value of my variable is not ‘yo’ the program will print Not a match, otherwise the program will print  Yo Match found.

Determine the result of the following

“dashboard”.equals(“dashboard”)
2 > 4
!(“Howdy”.equals(“Hey”))
!(34==32)
“PosTs”.equals(“posts”)
3!=5 && !(3==5)
Back to Course Menu
What do you think about this topic? Share your suggestions / comments