Java Language Basics.

Slides:



Advertisements
Similar presentations
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Advertisements

More Java Syntax. Scanner class Revisited The Scanner class is a class in java.util, which allows the user to read values of various types. There are.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
10 ThinkOfANumber program1July ThinkOfANumber program CE : Fundamental Programming Techniques.
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
DAT602 Database Application Development Lecture 5 JAVA Review.
Chapter 5 Loops.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
By Chad Blankenbeker.  The for-loop is best used when you know how many times it is going to be looped  So if you know you want it to only loop 10 times,
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING For Loop.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Sophomore Scholars Java
Slides by Evan Gallagher
Slides by Evan Gallagher
Loops A loop is: Java provides three forms for explicit loops:
Lecture 4b Repeating With Loops
CSC111 Quick Revision.
Java Language Basics.
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
Chapter 4 Repetition Statements (loops)
Loops.
John Hurley Cal State LA
Chapter 6 More Conditionals and Loops
Chapter 5: Control Structures II
Repetition-Counter control Loop
Repetition.
TK1114 Computer Programming
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
While Statement.
Decision statements. - They can use logic to arrive at desired results
Arrays, For loop While loop Do while loop
Looping and Repetition
Conditional Loops.
Chapter 6 More Conditionals and Loops
Starting JavaProgramming
Outline Altering flow of control Boolean expressions
The for-loop and Nested loops
In this class, we will cover:
Java Programming Loops
Control Statements Loops.
Lec 4: while loop and do-while loop
python.reset() Also moving to a more reasonable room (CSE 403)
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Announcements Lab 6 was due today Lab 7 assigned this Friday
Using Objects (continued)
Arrays in Java.
Java Programming Loops
In this class, we will cover:
Control Statements Loops.
Repetition Statements
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Loops CGS3416 Spring 2019 Lecture 7.
How do you do the following?
More on iterations using
Presentation transcript:

Java Language Basics

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); }

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); } }

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++; } } }

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)

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:

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) }

for-statement Demo class ForDemo { public static void main(String[] args){ for(int i=1; i<11; i++){ System.out.println("Count is: " + i); } } }

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.

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]); }

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]); }