- Introduction
- Super Simple Formula
- Your First Program
- Variables, Methods and Parameters
- Variables in Programming and Algebra
-
Math and Comparison Operators - If Statement
- While and For Loops
- Loopy Thinking
-
Data Types - Booleans
- Logical Operators (and Booleans)
- Output and Printing
- Printing and Loops
-
Arrays - Arrays and Loops
- Array Loop Practice
-
What's Next? - Quick Reference
Quick Reference
Quickly lookup Java basics, and load code examples directly into editor.
int
), the method name (doStuff
), and the method's parameters (such as a
and b
). The method body follows, where you usually need to write code that returns a value at the end.
//method header: methodName(parameters)
static int doStuff(int a, int b){
//your code here - erase this comment and write your code!
return 1;
}
Syntax note: Every line of code in Java needs to end with a
;
or it will be considered part of the next line. Comments are marked with 2 backslashes //
and are ignored by the computer.%
operator returns the remainder after division.
int product = 4 * (1+2) / 2; //6.
int roughDivision = 8 / 3; //2
int remainder = 3 % 2; //1, the remainder of 3/2
booleans are used to store true/false values, such as the results of comparisons:
boolean check = false; //this will store result of comparison
int five = 5;
//the following are all true
check = (five == 5); //equals
check = (five != 3); //not equals
check = (five >= 5); //greater or equals
The logical operator AND &&
will return true if multiple conditions are all true, and OR ||
will return true if at least one condition is true.
//these pointless statements will both set check to true
check = (3>2 && 2>1); //AND
check = (3>2 || 1>3); //OR
if(condition){
//doSomething
}
The else statement will execute code if the condition was false. The while statement will repeatedly execute the same code while a condition is true.
public static void controlFlow(int number) {
//this if/else block will print out 1 statement for any number
if(number > 10){
System.out.println("big number");
}
else if(number > 5){
System.out.println("medium number");
}
else{
System.out.println("small number");
}
//this loop will print the numbers from 0 to 9
int i = 0;
while(i < 10){
System.out.println(i);
i = i + 1;
}
}
System.out.println("");
The above prints text and add a newline afterwards.
System.out.print
prints without a newline, but you can add in spaces " " to separate text.
System.out.println("hello"); //prints with newline
String word1 = "hello";
String word2 = "world";
System.out.print(word1 + " " + word2); //prints "hello world"
Arrays are used to store multiple items of one data type together. They are declared with the type they will hold and with brackets []
. This code creates an empty 5-cell array:
int[] ar1 = new int[5];
This shortcut creates an array with numbers 0 to 4:
int[] nums = {0,1,2,3,4};
You can use for-loop to print values of array:
for(int i=0; i < nums.length; i=i+1){
System.out.print(nums[i]+" ");
}
Logical Operators (and Booleans)
In previous nodes, we showed how you can use if
, while
and for
statements to execute code only when a certain condition is true. This works fine if you just care about checking one condition, but sometimes you will want to run code when either A OR B is true, or only when A AND B are true.
In such cases, you will want to use the logical operators "And" and "Or". In Java they are written as &&
and ||
. For example:
int a = 5;
int b = 7;
boolean aLargish;
boolean bLarger; //declare a true/false variable
bLarger = b > a && a > 0; //true, since both true.
aLargish = a > b || a > 0; //true, since a>0 and it's an OR statement
boolean both = aLargish && bLarger; //true again!
In the above code, the results of the first comparison was stored in the boolean variable bLarger
. As in the previous node, this could then be used in a conditional statement :
if(bLarger){
//do something...
}
Alternatively, you can put the expression itself in the if-statement without using a variable:
if(b > a && a > 0){
//do something...
}
Note that:
-
a && b
will returntrue
only when BOTHa
ANDb
are true. -
a || b
will returntrue
when eithera
AND/ORb
are true.
Challenge
As briefly mentioned in About Programming, computers ultimately calculate just by doing simple logical operation like AND and OR on Logic Gates. While these gates are physical parts of a computer, you can simulate them in software. The NAND gate is a logical gate that returns false only when both its inputs are true. In other cases it returns true. You will simulate a NAND gate in this challenge. Try to do this challenge without using any if
statements.
Challenge
You will be given two integer inputs, a
and b
. Simulate a NAND gate by returning a boolean false only if both inputs are equal to 1, and returning true in all other cases.
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Comments
Carlos Díaz Ramirez
Jan 1, 10:43 PMinverse way also works 'a==1 && b ==1'
Carlos Díaz Ramirez
Jan 1, 10:43 PM'a==1 && b ==1' can work too
Learneroo
Jan 1, 10:47 PM@Carlos, with an overall not
!
, yes.pat
May 21, 9:37 AMWhat is the code to solve this?
Vignesh Prabhu S
May 30, 8:27 AMboolean bLarger = ( a==1 && b==1);
return (!bLarger);
this works
Jesse Li
Aug 7, 8:41 PMcan be done in 1 line of code using sums
'return ((a + b) != 2);'
mistermase
Aug 21, 12:46 PMboolean gates = a!=1 || b!=1;
Ellis Keith
Dec 8, 7:04 AMThe not so advanced one line solution:
return( ! (a == 1 && b ==1));
joeal
Feb 13, 1:56 AMreturn a!=1 || b!=1;
flowra
Feb 13, 4:58 PMalso this solution is true :
return !(a==1 && b==1);
Marlon Buella
Apr 19, 11:33 PM!((a == 1) && (b == 1))
also works!Stanixlav
Aug 1, 2:38 PMThis might be a slight cheat, but we know that a and b are both integers, meaning there are no fractions in play. Therefore, the following also works: my code
Asadujjaman Shimul
Dec 3, 2:57 AMimport java.util.Scanner;
public class BooleanExm2
{
public static void main(String[] args)
{
int a;
int b;
Scanner input = new Scanner(System.in);
}
Gary
Feb 18, 2:19 PMLol, my too-many-steps solution:
boolean inputa = a > b || a < b;
boolean inputb = a == 0 && b == 0;
boolean both = inputa || inputb;
return both;
rajni dound
Nov 18, 3:58 PMimport java.util.Scanner;
public class Main(){
public static void main(String[] args){
int a= 4;
int b= 1;
}
Trev
Jun 28, 11:08 PMthanks guys!