Download presentation
Presentation is loading. Please wait.
1
Java Language Basics
2
Getting User Input public static void main(String[] args) {
Scanner reader = new Scanner(System.in); System.out.println("Enter a number:"); int a = reader.nextInt(); //Gets User Input of data type Int System.out.println("The value of a" + a); System.out.println(" "); System.out.println("Enter the Name"); reader = new Scanner(System.in); String test= reader.nextLine(); //Gets User Input of data type String System.out.println("Enter the Name = " + test); }
3
Do While Loop Example class DoWhileDemo { public static void main(String[] args){ int count = 1; do { System.out.println("Count is: " + count); count++; } while (count < 11); } }
4
While Loop Example class WhileDemo { public static void main(String[] args){ int count = 1; int limit = 15; while (count < limit) { System.out.println("Count is: " + count); count++; } } }
5
Password Assignment Write a program that gets a Password from the user. Once, the program has an idea of what the password is, write a do while loop that asks the user to retype the password. The do while loop should check if the original password is the same as the new password entered by the user. If the password is correct, the do while loop should terminate. Hint: When you want check if two strings are equal string Password; string providedPassword; if(Password.equals(providedPassword)
6
Difference: while and do-while
The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once, as shown in the following DoWhileDemo program:
7
The for Statement The for statement provides a compact way to iterate over a range of values. Programmers often refer to it as the "for loop" because of the way in which it repeatedly loops until a particular condition is satisfied. The general form of the for statement can be expressed as follows: for (initialization; termination; increment) { statement(s) }
8
for-statement Demo class ForDemo { public static void main(String[] args){ for(int i=1; i<11; i++){ System.out.println("Count is: " + i); } } }
9
Arrays An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.
10
Array Code class ArrayListDemo {
public static void main(String[] args) { int[] anArray; anArray = new int[2]; anArray[0] = 000; anArray[1] = 111; System.out.println("Element at index 0: "+ anArray[0] ); System.out.println("Element at index 1: "+ anArray[1]); }
11
Array and For loops You can use for loops to cycle through the array index for(int i = 0; i < anArray.length; i++){ System.out.println("Element at index "+i+" is "+ anArray[i]); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.