COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

Similar documents
Introduction to C/C++ Lecture 3 - Program Flow Control

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

Quiz 1: Functions and Procedures

APCS Semester #1 Final Exam Practice Problems

Conditions, logical expressions, and selection. Introduction to control structures

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

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

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

Flow Control. CSC215 Lecture

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

Introduction to Programming Using Java (98-388)

Lecture 5 Tao Wang 1

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

Learn more about our research, discover data science, and find other great resources at:

ECE 122. Engineering Problem Solving with Java

Chapter 4: Making Decisions

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

Decision Making in C

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

SCoLang - Language Reference Manual

CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

PROGRAMMING FUNDAMENTALS

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

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Review for Test 1 (Chapter 1-5)

While Loops CHAPTER 5: LOOP STRUCTURES. While Loops. While Loops 2/7/2013

CONDITIONAL EXECUTION: PART 2

Accelerating Information Technology Innovation

More Programming Constructs -- Introduction

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

Unit 6 - Software Design and Development LESSON 3 KEY FEATURES

Language Reference Manual

Unit 2: Java in the small. Prepared by: Dr. Abdallah Mohamed, AOU-KW

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

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

COP 1220 Introduction to Programming in C++ Course Justification

Introduction to Java & Fundamental Data Types

Midterm Review Topics Java Basics and Structure Variables Branching Loops Methods Arrays. Midterm Review Kage Weiss Lab 001 TA, SPR 17

Repetition Structures

Unit 2: Java in the small

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

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

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Unit 6 - Software Design and Development LESSON 3 KEY FEATURES

CS111: PROGRAMMING LANGUAGE II

Control Structures in Java if-else and switch

Programming for Engineers Iteration

(Not Quite) Minijava

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Computing Science 114 Solutions to Midterm Examination Tuesday October 19, In Questions 1 20, Circle EXACTLY ONE choice as the best answer

Control Structures in Java if-else and switch

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

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

Language Fundamentals Summary

B. Subject-specific skills B1. Problem solving skills: Supply the student with the ability to solve different problems related to the topics

Chapter 4: Control structures. Repetition

SECTION II: LANGUAGE BASICS

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

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

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Homework #2. If (your ID number s last two digits % 6) = 0: 6, 12, 18

CS106X Handout 03 Autumn 2012 September 24 th, 2012 Getting Started

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

CS313D: ADVANCED PROGRAMMING LANGUAGE

Operators. Java operators are classified into three categories:

Control structures and logic

Computational Expression

Chapter 4: Control structures

SFU CMPT Topic: Control Statements

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Introduction to Bioinformatics

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

Introduction. C provides two styles of flow control:

EXERCISES SOFTWARE DEVELOPMENT I. 03 Control Flow & Branching Statements if/switch, while/do-while/for 2018W

G Programming Languages - Fall 2012

Lecture 7 Tao Wang 1

UNIT 3

Computer Science & Engineering 150A Problem Solving Using Computers

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

COMS 469: Interactive Media II

CGS 3066: Spring 2015 JavaScript Reference

SNS COLLEGE OF ENGINEERING,

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

Fundamentals of Programming Session 9

Principles of Computer Science

Scope of this lecture. Repetition For loops While loops

Internet & World Wide Web How to Program, 5/e by Pearson Education, Inc. All Rights Reserved.

Chapter 17. Fundamental Concepts Expressed in JavaScript

Quick Reference Guide

New Concepts. Lab 7 Using Arrays, an Introduction

Transcription:

COMSC-051 Java Programming Part 1 Part-Time Instructor: Joenil Mistal

Chapter 5 5 Controlling the Flow of Your Program Control structures allow a programmer to define how and when certain statements will be executed. In other words, if certain conditions are met, specified behaviors will result.

Chapter 5 Topics: What you will learn in this chapter: How to determine if a certain condition is met. How to control what a Java program does and when it should to it. How to use loops in Java to repeat an action. How to determine which control structure to use.

Comparisons Using Operators and Methods page 130 Two types of control operators that are used frequently in control structures: Comparative Logical The syntax for operators will differ whether the data type is primitive or composite:

Comparing Primitive Data Types with Comparison Operators page 130 Control Operators for Primitive Data Types: Operator Java Syntax English Equivalent Equality Relational Logical x == y x!= y x < y x <=y x > y x >= y AND (&&) : x && y OR ( ) : x y NOT (!) :!x Is x equal to y? Is x not equal to y? Is x less than y? Is x less than or equal to y? Is x greater than y? Is x greater than or equal to y? Are x and y both true? Is x, y or both true? Is x false? Comparative

Comparing Primitive Data Types with Comparison Operators page 130 Local operators are specific to Booleans (True or False). They are used to combine or negate ore or more conditions. There are three logical operators: AND (&&) OR ( ) NOT (!)

Comparing Primitive Data Types with Comparison Operators page 130 AND (&&) Operator If two ore more Boolean operands are joined using the AND operator, all must be evaluate to true for the overall express to evaluate to true X Y X && Y TRUE TRUE True AND True = TRUE TRUE FALSE True AND False = FALSE FALSE TRUE False AND True = FALSE FALSE FALSE False AND False = FALSE

Comparing Primitive Data Types with Comparison Operators page 130 OR ( ) Operator If two ore more Boolean operands are joined using the OR operator, at least one of them must evaluate to true in order for the overall expression to evaluate to true X Y X Y TRUE TRUE True OR True = TRUE TRUE FALSE True OR False = TRUE FALSE TRUE False OR True = TRUE FALSE FALSE False OR False = FALSE

Comparing Primitive Data Types with Comparison Operators page 131 NOT (!) Operator If the NOT operator precedes a Boolean operand that evaluates to true, the overall expression will evaluate to false and vice versa. It evaluates as the opposite expression. X TRUE FALSE! X! TRUE = FALSE! FALSE = TRUE

Understanding Language Control page 135 This section explores how comparison operators and methods can be used in Java control structures These structures include: If-then Switches While Loop For Loop

Creating if-then Statements page 135 The most fundamental control structure is an ifthen statement. The most basic syntax is as follows: if (condition ) { /* then execute these statements */ } If a condition is met, then execute a piece of code between the curly brackets. Otherwise, the program will not execute the statements and continue just after the last curly bracket.

Creating if-then Statements page 136 Example: If the variable accountbalance is greater than 100, then output a notification of Safe balance. If the accountbalance is not greater than 100, nothing happens

Creating if-then Statements page 136 The basic if-then statement can also be extended with the keyword else. This provides an alternative set of statements to be executed if the condition is not true Example

Creating if-then Statements page 136 The else keyword can also be followed by a second if statement, which is then evaluated only if the first condition is true Example

Nesting if-then Statements page 137 Control structures, such as if-then statements, can also be nested. This concept looks something like the following: Nested if-then Statement

Nesting if-then Statements page 137 This could also be accomplished using the Boolean operators: Boolean Operator

Creating Loops page 138 Loops are control structures that cycle through a section of code as long as some condition is met. This allows for repetitive execution without repetitive coding. It reduces the redundancy which improves the maintainability of code. For Loop While Loop

Creating Loops page 138 A for loop executes a block of code over a range of values. An index keeps track of the loop The standard syntax for a for loop is as follows: for (/*Initialization*/; /* Termination */; /*Increment*/) { / * execute these statements */ } For Loop

Creating Loops page 138 A for loop executes a block of code over a range of values. An index keeps track of the loop The standard syntax for a for loop is as follows: for (/*Initialization*/; /* Termination */; /*Increment*/) { / * execute these statements */ } For Loop

Creating Loops page 138 The for loops requires three parts: 1. Initialization declares the index variable for the loop and it s starting value (i.e int i = 0) 2. Termination specifies a stopping criteria or maximum value for the index variable 3. Increment indicates how the index variable sould change after each iteration (i.e. i++ - meaning that the value of i wil crease y each loop) for (/*Initialization*/; /* Termination */; /*Increment*/) { / * execute these statements */ }

Creating Loops page 138 The for loops requires three parts: 1. Initialization declares the index variable for the loop and it s starting value (i.e int i = 0) 2. Termination specifies a stopping criteria or maximum value for the index variable 3. Increment indicates how the index variable sould change after each iteration (i.e. i++ - meaning that the value of i wil crease y each loop) for (/*Initialization*/; /* Termination */; /*Increment*/) { / * execute these statements */ }

Creating Loops page 138 Example: You would like to calculate the average sales per staff member for each month and the total annual sales for the year using For Loops:

Creating Loops page 141 Try It Out: Your first for Loop

Creating Loops page 143 What is an Enhanced for Loop? This loop is an alternative form specifically for arrays and iterable objects. The enhanced for loops uses an iterator which is the same type as the elements in the array. Example:

Creating Loops page 138 A while loop executes a block of code on a certain condition. When the execution of a program reaches a while loop, it will first check if the conditional expression evaluates to true. The standard syntax for a while loop is as follows: while (/ * conditional expression */) { / * execute thee statements */ } While Loop

Creating Loops page 150 Example of while loop:

Creating Loops page 141 Try It Out: Your First while Loop

Creating Loops page 143 What is an do while Loop? A do while loop first executes the statements within the loop and then checks the conditional expression to see if the loop should repeat The standard syntax for a do while loop is as follows: do {/ * execute thee statements */ } while (/ * conditional expression */)

Creating Loops page 153 Try It Out: Your first do while Loop

Comparing for and while Loops page 156 In general, keep in mind the following points: If you are iterating over a collection, consider a for loop first. If you know the number of loops in advance, consider a for loop If you do not know the number of iterations, but the number will depend on a certain condition, consider a while loop first. For Loop While Loop

Creating Switches page 156 A switch statement evaluates a single variable and depending on its value, executes a certain block of code. The syntax of is as follows: The 3 keywords in the syntax- case, break and default switch (/ * variable */) { case 1: / * execute thee statements */; break; case 2: / * execute thee statements */; break; default: / * execute thee statements */; } Switches

Creating Switches page 156 The switch checks the value of the variable for a match in one the cases. If they are equal, the statements in that case will be executed. Cases are evaluated sequentially, the statements that follow the : for that cases will be executed. switch (/ * variable */) { case 1: / * execute thee statements */; break; case 2: / * execute thee statements */; break; default: / * execute thee statements */; }

Creating Switches page 156

Creating Switches page 160 Try It Out: Creating a switch

Comparing Switches and ifthen Statements page 161 In general, keep in mind the following points: If you have a single variable that can take multiple values, a switch might be suitable. If you have multiple variables or conditions to consider, you will probably need an if-then statement. If the value you are considering can have a finite number of values, consider using a switch If the variable can take any value within a continuous rang of numbers, consider an if-then statement If-then Switches

Reviewing Keywords for Control page 162 KEYWORD for while do if else switch case default break continue return ASSOCIATED CONTROL STRUCTURES for loop and Enhanced for loop while loop and do while loop do while loop if-then statement if-then statement switch statement switch statement switch statement General use General use General use

Reviewing Control Structues page 168 Two types of control operators that are used frequently in control structures Comparative Logical Types of controls structures include: If-then Switches While Loop For Loop