- 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.
Method Basics
The header of the method declares what the method will return (such as an
Syntax note: Every line of code in Java needs to end with a
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.
Variables and Data types
Operations Booleans and Logic
Math operations are performed in the standard order. Note that division of integers will round down to nearest integer. The mod
%
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 else and while
The if statement will execute code only if a condition is true:
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.
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;
}
}
Printing and Arrays
Print a newline
The above prints text and add a newline afterwards.
This code creates an empty 5-cell array:
This shortcut creates an array with numbers 0 to 4:
You can use for-loop to print values of array:
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]+" ");
}
Printing and Loops
Collapse Content
Show Content
Now that you know how to print, you can really demonstrate the power of loops.
You will be given two numbers, a
and b
. Print a
(the first number) b
times. For example, if given 3
and 5
, you should print 3
5 times: 33333
.
Print each case on one line. Remember to use System.out.print
to print without adding a newline.
Note: For more practice, try solving printing multiplication.
Challenge
Print each number a
b
times.
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Comments
TechLord256
Mar 30, 5:10 PMHow many possible solutions are there to this challenge?
Learneroo
Mar 30, 5:12 PMThere are many possible ways, but one way is particularly straightforward.
Marlon Buella
Apr 19, 11:44 PMThis code fragment also works:
for(int x = 1; x <= b; x++)
Greg
May 20, 4:42 PMyo keesh can anyone post the answer...
Asadujjaman Shimul
Dec 3, 3:22 AMimport java.util.Scanner;
public class PrintLoop
{
public static void main(String[] args)
{
int a;
int b;
Scanner input = new Scanner(System.in);
}
Gary
Feb 19, 10:43 AMfor(int c = b; c > 0; c = c - 1)
Harshi Athukorala
Aug 22, 11:44 PMimport java.util.Scanner;
public class Loop1{
public static void main(String args[]){
Scanner input1 = new Scanner(System.in);
System.out.println("Enter First number !");
}
rajni dound
Nov 18, 4:32 PMimport java.util.Scanner;
public class Main(){
public static void main(String[] args){
int a=2;
int b=4;
for(int i=1;i<=b;i=i+1){
System.out.print(a);
}
}
}
Aldor
Mar 20, 8:54 AMBUG!