top of page
realcode4you

If-ELSE Statement, Operators, Variables And Its Types In Java | Java Programming Help

Much like math class, we use variables to store values. Now in math class we are only ever storing numbers (integers, decimals, fractions, etc.). This would work fine for storing someone's age (an integer). But to store that persons name we need letters of the alphabet.


Data Types

  • Integer

  • Long

  • Double

  • Boolean

  • Character

  • String


Variables

In Java, variables must be declared before they can be used.

Example:

  • Store 5 in variable x

  • Store Bob in variable name

  • Store true in variable result

  • Store 5.25 in variable score

  • Store @ in variable symbol


Variable Names

You can't just give any name you want to a variable! Here are the rules to follow:

  1. Variable names must start with either an upper or lowercase letter

  2. A variable name may contain numbers or an underscore (ex: number_1)

  3. You cannot use one of the reserved keywords (see D2L for a list)

  4. Variable names should be camelCase (firstName, dayOfWeek, etc.)


Printing Variables

You can print variable x using below print statement.

System.out.println("x");

IF-ELSE Statements

IF Statements

Until now our programs have always done the same thing each time they were run. But to be truly useful programs need to change based on user input. To accomplish this in programming we use IF Statements. Code inside an IF Statement will run only if certain conditions are met:


Example:

if (x == 5){  
System.out.println("This only prints if x is 5");  
System.out.println("This also only prints if x is 5"); 
} 
System.out.println("This prints no matter what x is"); 

Else Statement

An Else Statement acts as a catch-all. If none of the previous conditions were met, whatever code is in the Else statement will run.

x = 7; 
if (x == 5){  
System.out.println ("if x is not 5, this will not print"); 
} else{  
System.out.println ("This code runs because the previous condition was not met"); 
}

Else If Statements

In Java you can use else if to check additional conditions if the previous ones have failed. You can have as many else if statements in an IF statement as you want. Note: As soon as one condition is met, Java skips to the end of the IF statement without checking any of the others.


Example:

x = 6; 
if (x == 5){  
System.out.println ("x is not 5, this will not print"); 
} else if (x==6){  
System.out.println ("x does = 6, this line prints!"); 
} else{  
System.out.println ("This code never runs because the previous condition was met"); 
} 

Comparison Operators

Comparison operators are used to check the size of values compared to each other. They will return a Boolean value of true or false.


Operator Description

== Equal To

!= Not Equal To

> Greater Than

< Less Than

>= Greater Than or Equal To

<= Less Than or Equal To

x.equals(y) Equal To for Strings x and y



Logical Operators

There are three logical operators: and, or and not


Operator Name Description

&& AND Returns True if a AND b are true

|| OR Returns True if at least one of a OR b is true

^ XOR Returns True if exactly one of a OR b is true

! NOT Returns True if a is false




Practice Examples

Ex:1

x = 10 y = 5 z = -5


if (y > 5) {
 System.out.print("A");
} else {
 System.out.print("B");
}

Output: B


Ex:2

if (x > y) {
 System.out.print("A");
} else if(x > z) {
 System.out.print("B");
}

Output: A


Ex:3

if (x == 5) {
 System.out.print("A");
} else if(x < 10) {
 System.out.print("B");
} else if(x > 10){
 System.out.print("C");
}

Output:


Ex:4

if (2*z >= x) {

System.out.print("A");

} else if(y == z) {

System.out.print("B");

} else {

System.out.print("C");

}


Output: C




Here you get all types of Java Programming help, Project help and homework help. If you need any help in Java related task then you can contact us or send your requirement details at:


realcode4you@gmail.com

Comments


bottom of page