Arrays, Conditionals & Loops in Java. Arrays in Java Arrays in Java, are a way to store collections of items into a single unit. The array has some number.

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
The switch Statement, DecimalFormat, and Introduction to Looping
TODAY’S LECTURE Review Chapter 2 Go over exercises.
UNIT II Decision Making And Branching Decision Making And Looping
L EC. 03: C ONTROL STATEMENTS Fall Java Programming.
Iteration and Simple Menus Deterministic / Non-deterministic loops and simple menus.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
1 Week 6 Branching. 2 What is “Flow of Control”? l Flow of Control is the execution order of instructions in a program l All programs can be written with.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Application development with Java Lecture 6 Rina Zviel-Girshin.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 8 Java Fundamentals Control Structures Fri.
CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.
CSI 3125, Preliminaries, page 1 Control Statements.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
5 Copyright © 2004, Oracle. All rights reserved. Controlling Program Flow.
Loops and Logic. Making Decisions Conditional operator Switch Statement Variable scope Loops Assertions.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
The switch Statement, and Introduction to Looping
Unit-1 Introduction to Java
Loops in Java.
Control Structures.
Expressions and Control Flow in JavaScript
Java - Data Types, Variables, and Arrays
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
CISC124 Labs start this week in JEFF 155. Fall 2018
Computer Science Core Concepts
CMPE212 – Reminders The other four assignments are now posted.
Arrays in Java.
PROGRAM FLOWCHART Iteration Statements.
Chap 7. Advanced Control Statements in Java
Loops CGS3416 Spring 2019 Lecture 7.
CSC215 Lecture Control Flow.
Looping and Repetition
Presentation transcript:

Arrays, Conditionals & Loops in Java

Arrays in Java Arrays in Java, are a way to store collections of items into a single unit. The array has some number of slots, each of which holds an individual item. Arrays in Java are actual objects that can be passed around and treated just like other objects

Creating arrays in Java 1.Declare a variable to hold the array. 2.Create a new array object and assign it to the array variable. 3.Store things in that array.

Declaring arrays Declare a variable that will hold the array. Declaration includes the type of object the array will hold and the name of the array, followed by empty brackets ([]). For example, string difficultWords[]; int temps[]; float y[];

Accessing Array Elements To get at a value stored within an array, use the array subscript expression ([]): myArray[subscript];

Multi-Dimensional Arrays In Java, multidimensional arrays are actually arrays of arrays. You can create a nonrectangular multidimensional array by having elements of an array refer to arrays of different sizes.

Multi-Dimensional Arrays (contd..) For example, to initialize a two-dimensional array of which the first element has two sub-elements and the second one has three sub-elements, you can declare it as int [] [] a = new int [2] [3]; or you can both declare and initialize it to some values like : int [] [] a = { { 1, 2}, {3, 4, 5} };

Control Statements in Java Java’s program control statements can be put into the following categories: 1.Selection 2.Iteration 3.Jump

Java’s Selection statements The if statement is Java’s conditional branch statement if (conditional_expression) statement1 else statement2 Here, if the condition is true then statement1 is executed Otherwise, the statement or block of statements after the else part will be executed.

if statement in Java For Example, int a, b; //…. if(a<b) a=0; else b=0; Here, if a is less than b, then a is set to zero. Otherwise, b is set to zero.

Nested ifs A nested if is an if statement that is the target of another if or else. if(i==10) { if(j<20) a=b; if(k>100) c=d; // this if is else a = c; // associated with this else } else a=d; //this else refers to if(i==10)

The conditional Operator ( ?:) An alternative to use the if and else keywords in a conditional statement is to use the conditional operator also called the ternary operator.

The conditional Operator ( ?:) (contd..) The conditional operator is most useful for very short or simple conditionals and looks like this: test ? trueresult : falseresult ; test is a boolean expression, meaning that it returns true or false If the test is true, the conditional operator returns the value of trueresult, if it’s false, it returns the value of falseresult.

Switch Conditionals The switch statement provides an easy way to dispatch execution to different parts of your code based on the value of an expression. It provides a better alternative than a large series of if-else-if statements.

Switch Conditionals (contd..) Syntax of the switch statement switch(test) { case valueOne: resultOne; break; case valueTwo: resultTwo; break;

Switch Conditionals (contd..) case valueThree: resultThree; break; …… default : defaultresult; }

Iteration Statements Java’s iteration statements can be categorized as 1. for loop 2. while loop 3. do-while loop

for Loop The simplest form of the for loop is shown here for (initialization; condition; iteration) statement

while Loop The while loop repeats a statement or block while its controlling expression is true. Here is its general form: while (condition) { // body of loop }

do-while Loop The do-while loop always executes its body at least once, because its conditional expression is at the bottom of the loop. Its general form is do { // body of loop }while (condition);