0% found this document useful (0 votes)
34 views

Data Types: Mynum Myfloatnum Myletter Mybool Mytext

The document discusses different Java data types like int, float, char, boolean and String. It then demonstrates the use of control flow statements like if-else, switch, while, do-while and for loops to control the flow of a program. It also shows how to declare and access elements of single and multi-dimensional arrays in Java.

Uploaded by

ErvinHysa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Data Types: Mynum Myfloatnum Myletter Mybool Mytext

The document discusses different Java data types like int, float, char, boolean and String. It then demonstrates the use of control flow statements like if-else, switch, while, do-while and for loops to control the flow of a program. It also shows how to declare and access elements of single and multi-dimensional arrays in Java.

Uploaded by

ErvinHysa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Data types

int myNum = 5;

float myFloatNum = 5.99f;

char myLetter = 'D';

boolean myBool = true;

String myText = "Hello";

If statement
int time = 22;

if (time < 10) {

System.out.println("Good morning.");

} else if (time < 20) {

System.out.println("Good day.");

} else {

System.out.println("Good evening.");

// Outputs "Good evening."

Switch statement
int day = 4;

switch (day) {

case 1:

System.out.println("Monday");

break;

case 2:

System.out.println("Tuesday");

break;

case 3:

System.out.println("Wednesday");
break;

case 4:

System.out.println("Thursday");

break;

case 5:

System.out.println("Friday");

break;

case 6:

System.out.println("Saturday");

break;

case 7:

System.out.println("Sunday");

break;

// Outputs "Thursday" (day 4)

While
int i = 0;

while (i < 5) {

System.out.println(i);

i++;

do while
int i = 0;
do {

System.out.println(i);

i++;

while (i < 5);


For loop
for (int i = 0; i <= 10; i = i + 2) {

System.out.println(i);

_______________________________________________________________________

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

for (String i : cars) {

System.out.println(i);

Arrays
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

System.out.println(cars[0]);

int[] myNum = {10, 20, 30, 40};

2D Arrays
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };

int x = myNumbers[1][2];

System.out.println(x); // Outputs 7

You might also like