Java Control Statements

Similar documents
Java Overview An introduction to the Java Programming Language

Grouping Objects. Primitive Arrays and Iteration. Produced by: Dr. Siobhán Drohan. Department of Computing and Mathematics

Inheritance in Java. Produced by: Eamonn de Leastar Dr. Siobhán Drohan

Java Classes. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

Iteration in Programming

Menu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by:

Ticket Machine Project(s)

Scope of this lecture. Repetition For loops While loops

Principles of Computer Science

Instructor: SIR MUHAMMAD NAVEED Created by: ARSLAN AHMED SHAAD ( ) MUHAMMAD BILAL ( ) ISIT:

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

Decision Making and Loops

Introduction to Programming Using Java (98-388)

CompSci 125 Lecture 11

Glossary. (Very) Simple Java Reference (draft, v0.2)

Grouping objects. Main concepts to be covered

Prepared by: Shraddha Modi

SELECTION. (Chapter 2)

Java Programming with Eclipse

Grouping objects. The requirement to group objects

Algorithms. Produced by. Eamonn de Leastar

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

6 COMPUTER PROGRAMMING

Java 1.8 Programming

Computational Expression

Loops. CSE 114, Computer Science 1 Stony Brook University

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

L o o p s. for(initializing expression; control expression; step expression) { one or more statements }

Le L c e t c ur u e e 3 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Control Statements

ECE 122. Engineering Problem Solving with Java

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

CS1004: Intro to CS in Java, Spring 2005

SCoLang - Language Reference Manual

Annotations in Java (JUnit)

Handout 5 cs180 - Programming Fundamentals Spring 15 Page 1 of 8. Handout 5. Loops.

CSC Java Programming, Fall Java Data Types and Control Constructs

Flow Control. CSC215 Lecture

CS313D: ADVANCED PROGRAMMING LANGUAGE

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Object-Oriented Programming in Java

Control Structures in Java if-else and switch

Introduction to the Java Basics: Control Flow Statements

Decision Making in C

Java Loop Control. Programming languages provide various control structures that allow for more complicated execution paths.

CS171:Introduction to Computer Science II

10/30/2010. Introduction to Control Statements. The if and if-else Statements (cont.) Principal forms: JAVA CONTROL STATEMENTS SELECTION STATEMENTS

Pull Lecture Materials and Open PollEv. Poll Everywhere: pollev.com/comp110. Lecture 12. else-if and while loops. Once in a while

Control Flow Statements

The Basic Parts of Java

The Java language has a wide variety of modifiers, including the following:

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

Control Structures in Java if-else and switch

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

Chapter 4 Control Structures

PROGRAMMING FUNDAMENTALS

University of Massachusetts Amherst, Electrical and Computer Engineering

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Computer Programming I - Unit 5 Lecture page 1 of 14

An Introduction to Processing

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

Iteration: Intro. Two types of loops: 1. Pretest Condition precedes body Iterates 0+ times. 2. Posttest Condition follows body Iterates 1+ times

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Course Outline. Introduction to java

Controls Structure for Repetition

Review for Test 1 (Chapter 1-5)

COMP 202 Java in one week

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009

5/23/2015. Core Java Syllabus. VikRam ShaRma

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

CS171:Introduction to Computer Science II

C++ Programming: From Problem Analysis to Program Design, Third Edition

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

CS 177 Week 5 Recitation Slides. Loops

Visual Programming. Lecture 3: Loops, Arrays. Mahmoud El-Gayyar

APCS Semester #1 Final Exam Practice Problems

1007 Imperative Programming Part II

Java Programming Training for Experienced Programmers (5 Days)

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Introduction. C provides two styles of flow control:

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

CS/B.TECH/CSE(OLD)/SEM-6/CS-605/2012 OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

COMP-202: Foundations of Programming. Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016

Java Applets. Last Time. Java Applets. Java Applets. First Java Applet. Java Applets. v We created our first Java application

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. 1

CS201 Some Important Definitions

REPETITION CONTROL STRUCTURE LOGO

Transcription:

Java Control Statements An introduction to the Java Programming Language Produced by: Eamonn de Leastar (edeleastar@wit.ie) Dr. Siobhan Drohan (sdrohan@wit.ie) Department of Computing and Mathematics http://www.wit.ie/

Essential Java Overview Introduction Syntax Basics Arrays Classes Classes Structure Static Members Commonly used Classes Control Statements Control Statement Types If, else, switch For, while, do-while Inheritance Class hierarchies Method lookup in Java Use of this and super Constructors and inheritance Abstract classes and methods Interfaces Collections ArrayList HashMap Iterator Vector Enumeration Hashtable Exceptions Exception types Exception Hierarchy Catching exceptions Throwing exceptions Defining exceptions Common exceptions and errors Streams Stream types Character streams Byte streams Filter streams Object Serialization 3

Overview: Road Map Control Statement Types If, else, switch For, while, do-while 2

What are Control Statements? Control statements are statements that control execution of other statements These other statements can be either selection or repetition statements Also called conditional and looping statements 3

Control Statement Types There are two general types of control statements: Selection (conditional) statements if, if-else, and switch statements Repetition (looping) statements for, while, and do-while statements There are also some other control statements specific to Java break, continue, and labels 4

Overview: Road Map Control Statement Types If, else, switch For, while, do-while 2

if statement syntax if keyword boolean condition to be tested actions if condition is true if(perform some test) Do these statements if the test gave a true result

Example if Statement int i = 1; if(i > 0) System.out.println("Greater than zero"); int i = 1; if(i > 0) System.out.println("Greater than zero"); int i = 3; if(i > 2) System.out.println("Greater than zero"); System.out.println("Greater than one"); System.out.println("Greater than two"); 6

if-else statement syntax if keyword boolean condition to be tested actions if condition is true if(perform some test) Do these statements if the test gave a true result else Do these statements if the test gave a false result else keyword actions if condition is false

Example if-else Statement int i = 1; if(i > 0) System.out.println("Greater than zero"); else System.out.println("Not greater than zero"); Braces can be omitted if single statements used. int i = 3; if(i > 2) System.out.println("Greater than zero"); System.out.println("Greater than one"); System.out.println("Greater than two"); else System.out.println("Either equal to two"); System.out.println("Or less than two"); 8

Nested if statements Any form of if statement can be nested There can be other if statements within of if statements if (condition) if(nested_condition1) nested_action1; else if(nested_condition2) nested_action2; else nested_action3; else action2; 9

Example Nested Statement int i = 3; if(i > 2) System.out.println("Greater than zero"); System.out.println("Greater than one"); System.out.println("Greater than two"); else if(i == 2) System.out.println("Equal to two"); else System.out.println("Less than two"); 10

More if statement syntax if(condition1 perform some test) Do these statements if condition1 gave a true result else if(condition2 perform some test) Do these statements if condition1 gave a false result and condition2 gave a true result else Do these statements if both condition1 and condition2 gave a false result

The switch statement The switch statement works in exactly the same way as a set of if statements, but is more compact and readable. The switch statement switches on a single value to one of an arbitrary number of cases. Two possible patterns of use are

The switch statement One possible pattern of use A switch statement can have any number of case labels.

The switch statement Second possible pattern of use The break statement after every case is needed, otherwise the execution falls through into the next label s statements. In this pattern, all three of the first values will execute the first statements section, whereas values four and five will execute the second statements section.

The switch statement The default case is optional. If no default is given, it may happen that no case is executed. The break statement after the default (or the last case, if there is no default) is not needed but is considered good style. From Java 7, the expression used to switch on and the case labels may be strings. Previous versions switched on int and char only.

The switch statement - example

The switch statement - example

The switch statement - example switch (group) case 'A': System.out.println("10.00 a.m "); break; case 'B': System.out.println("1.00 p.m "); break; case 'C': System.out.println("11.00 a.m "); break; default: System.out.println( Enter option A, B or C only!");

Example switch statement int i = 2; switch(i) case 1: System.out.println("1"); case 2: System.out.println("2"); case 3: System.out.println("3"); default: System.out.println("default"); int i = 2; switch(i) case 1: System.out.println("1"); break; case 2: System.out.println("2"); break; case 3: System.out.println("3"); break; default: System.out.println("default"); 2 3 default 2 Console Console 13

Example switch statement int i = 2; switch(i) case 1: System.out.println("1"); break; case 2: case 3: System.out.println("2"); System.out.println("or"); System.out.println("3"); break; default: System.out.println("default"); 2 or 3 Console 14

Overview: Road Map Control Statement Types If, else, switch For, while, do-while 2

For loop pseudo-code General form of a for loop for(initialization; boolean condition; post-body action) statements to be repeated

For loop syntax for(int i = 0; i < 4; i++) for(initialization; boolean condition; post-body action) statements to be repeated

Example for Statement for(int i=1; i<5; i++) System.out.println("Index is equal to " + i); Console Index is equal to 1 Index is equal to 2 Index is equal to 3 Index is equal to 4 int i=1; for(;;) System.out.println("Infinite loop"); if(i==2) break; i++; Console Infinite loop Infinite loop 16

While loop pseudo code General form of a while loop while keyword boolean condition while(loop condition) loop body Statements to be repeated Pseudo-code expression of the actions of a while loop while we wish to continue, do the things in the loop body

Construction of while loop Declare and initialise loop control variable (LCV) while(condition based on LCV) do the job to be repeated update the LCV This structure should always be used

Using while Statement while statement is used for repeating statements while some condition applies Condition is evaluated before the statement Condition evaluates to either true or false If condition evaluates to true statement executes If condition evaluates to false statement does not execute, and evaluation proceeds after the block 17

Example while Statement int i=1; while(i < 5) System.out.println(i); i++; int i=5; while(i < 5) System.out.println(i); i++; 1 2 3 4 Console Console 18

Using do-while Statement Similar to the while statement Condition is evaluated at the end of the statement Block is executed at least once Uses do and while keywords do statement; while(condition); 19

Example do-while Statement int i=1; do System.out.println(i); i++; while(i < 5); 1 2 3 4 Console int i=5; do System.out.println(i); i++; while(i < 5); 5 Console 20

Summary Control Statement Types If, else, switch For, while, do-while 2

Except where otherwise noted, this content is licensed under a Creative Commons Attribution-NonCommercial 3.0 License. For more information, please see http://creativecommons.org/licenses/by-nc/3.0/