Statements and Control Flow



Similar documents
Object Oriented Software Design

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Chapter 2: Elements of Java

J a v a Quiz (Unit 3, Test 0 Practice)

Object Oriented Software Design

Example of a Java program

Introduction to Java

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Informatica e Sistemi in Tempo Reale

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Contents. 9-1 Copyright (c) N. Afshartous

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Lecture 2 Notes: Flow of Control

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

JavaScript: Control Statements I

Building Java Programs

Statements and Control Flow

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

Iteration CHAPTER 6. Topic Summary

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

C++FA 5.1 PRACTICE MID-TERM EXAM

Moving from CS 61A Scheme to CS 61B Java

Conditional Statements Summer 2010 Margaret Reid-Miller

Unit 6. Loop statements

Selection Statements

Sample CSE8A midterm Multiple Choice (circle one)

PIC 10A. Lecture 7: Graphics II and intro to the if statement

1.3 Conditionals and Loops

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Chapter 5. Selection 5-1

The C++ Language. Loops. ! Recall that a loop is another of the four basic programming language structures

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Fundamentals of Programming

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

13 Classes & Objects with Constructors/Destructors

Sources: On the Web: Slides will be available on:

VB.NET Programming Fundamentals

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

While Loop. 6. Iteration

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

LOOPS CHAPTER CHAPTER GOALS

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Conditions & Boolean Expressions

Basic Programming and PC Skills: Basic Programming and PC Skills:

PHP Tutorial From beginner to master

Lecture 1 Introduction to Java

Concepts and terminology in the Simula Programming Language

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Chapter 3 Operators and Control Flow

Chapter 2 Introduction to Java programming

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

Java Interview Questions and Answers

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Pemrograman Dasar. Basic Elements Of Java

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

AP Computer Science Java Subset

CS177 MIDTERM 2 PRACTICE EXAM SOLUTION. Name: Student ID:

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Part I. Multiple Choice Questions (2 points each):

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Arrays. Introduction. Chapter 7

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

Computer Programming C++ Classes and Objects 15 th Lecture

Topic 11 Scanner object, conditional execution

The if Statement and Practice Problems

Java Basics: Data Types, Variables, and Loops

Chapter 3: Writing C# Expressions

Recursion and Recursive Backtracking

Java (12 Weeks) Introduction to Java Programming Language

Java Programming Fundamentals

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Common Beginner C++ Programming Mistakes

1.00/ Session 2 Fall Basic Java Data Types, Control Structures. Java Data Types. 8 primitive or built-in data types

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

TIP: To access the WinRunner help system at any time, press the F1 key.

CS 106 Introduction to Computer Science I

Variables, Constants, and Data Types

Two-way selection. Branching and Looping

Advanced Bash Scripting. Joshua Malone

Introduction to Java Lecture Notes. Ryan Dougherty

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Section IV.1: Recursive Algorithms and Recursion Trees

The C Programming Language course syllabus associate level

The Cool Reference Manual

COMP 110 Prasun Dewan 1

Lecture 22: C Programming 4 Embedded Systems

Transcription:

Contents 1. Introduction 2. Types and Variables 3. Statements and Control Flow 4. Reading Input 5. Classes and Objects 6. Arrays 7. Methods 8. Scope and Lifetime 9. Utility classes 10. Introduction to Object-Oriented Analysis and Design 3-1 Copyright (c) 1999-2004 N. Afshartous

Chapter 3: Statements and Control Flow After this chapter you will be able to: - Use shorthands for increment/decrement - Understand and override operator precedence - Use if statements - Incorporate for and while loops 3-2 Copyright (c) 1999-2004 N. Afshartous

Pre and Post-Increment ++x // Pre-increment x x++ // Post-increment x Examples: a = ++x * b; x = x + 1; a = x * b; a = x++ * b; a = x * b; x = x + 1; 3-3 Copyright (c) 1999-2004 N. Afshartous

Pre and Post-Decrement --x x-- // Pre-decrement x // Post-decrement x Examples: a = --x * b; a = x-- * b; x = x - 1; a = x * b; a = x * b; x = x - 1 3-4 Copyright (c) 1999-2004 N. Afshartous

Assignment Shorthands x += 5; x = x + 5; x -= 10; x = x - 10; x *= 4; x = x * 4; x /= 3; x = x / 3; x %= 2; x = x % 2; The shorthands on the left allow the compiler to optimize the assignment 3-5 Copyright (c) 1999-2004 N. Afshartous

Questions int a=2,b=3,c; c = ++a * b++; a,b,c? int a=2,b=3,c=0; c += --a * b++; a,b,c? int a=4,b=3,c=20; c /= ++a; a,b,c? int a=2,b=3,c=4; c *= ++a * b++; a,b,c? 3-6 Copyright (c) 1999-2004 N. Afshartous

Unary Operators A unary operator operates on a single value -x ++x --y +5 3-7 Copyright (c) 1999-2004 N. Afshartous

Binary Operators Binary operators operate on two values a <= 5 a + 10 c d 3-8 Copyright (c) 1999-2004 N. Afshartous

Operator Precedence Evaluation of expressions is not always left to right a = 4 + 5 * 6; What value is in a? Multiplication operator has higher precedence than the addition operator 3-9 Copyright (c) 1999-2004 N. Afshartous

Operator Precedence cont, Operators in order of decreasing precedence 1. unary +,- 7. && 2. *, /, % 8. 3. +,- 4. ==,!= 5. <, <=, >, >= 6.! 3-10 Copyright (c) 1999-2004 N. Afshartous

Overriding Precedence Can control evaluation order using parenthesis a = (4 + 5) * 6; What value is in a? Use parentheses to control evaluation order or if unsure of operator precedence 3-11 Copyright (c) 1999-2004 N. Afshartous

Control Flow Statements if statement for loop while loop switch statement 3-12 Copyright (c) 1999-2004 N. Afshartous

If Statement Syntax: if (bool_expression) statement The statement is executed if the boolean expression is true if (x > 0) y = z / x; 3-13 Copyright (c) 1999-2004 N. Afshartous

If With Else Syntax: if (bool_expr) statement1 else statement2 statement1 is executed if the boolean expression is true. Otherwise statement2 is executed if (a < 4) System.out.println("a is < 4"); else System.out.println("a is >= 4"); 3-14 Copyright (c) 1999-2004 N. Afshartous

Grouping Statements Use braces to group statements together if (a < 4) { System.out.println("a is < 4"); a++; } What is wrong with this? if (a < 4) System.out.println("a is < 4"); a++; 3-15 Copyright (c) 1999-2004 N. Afshartous

Dangling Else Problem if (a < 4) if (a > 2) System.out.println("a is 3"); else System.out.println("a is >= 4"); Which if does the else go with? In Java an else will be associated with the closest if if (a < 4) if (a > 2) System.out.println("a is 3"); else System.out.println("a is <= 2"); 3-16 Copyright (c) 1999-2004 N. Afshartous

Dangling Else with Braces Use braces to associate an else with an outer if if (a < 4){ if (a > 2) System.out.println("a is 3"); } else System.out.println("a is >= 4"); 3-17 Copyright (c) 1999-2004 N. Afshartous

For Loop Syntax: for (init; bool_expr; update) statement init is executed once as long the boolean expression is true the statement is executed update is executed after each iteration for (int i = 1; i <= 4; i++) System.out.println(i); Output 1 2 3 4 3-18 Copyright (c) 1999-2004 N. Afshartous

Declaring Loop Control Variable Loop control variable may be declared before or at for loop for (int i = 1; i <= 4; i++) System.out.println(i); OR int i; for (i = 1; i <= 4; i++) System.out.println(i); 3-19 Copyright (c) 1999-2004 N. Afshartous

Loop Control Variable Visibility Variable i is not visible outside the scope of the for loop for (int i = 1; i <= 4; i++){ System.out.println(i); } System.out.println(i); 3-20 Copyright (c) 1999-2004 N. Afshartous

Loop Control Variable Visibility Variable i is visible outside the scope of the for loop int i; for (i = 1; i <= 4; i++) System.out.println(i); System.out.println(i); 3-21 Copyright (c) 1999-2004 N. Afshartous

Example for (int i = 1; i <= 4; i++) if ((i % 2) == 0 ){ System.out.print(i); System.out.println(" is even"); } else { System.out.print(i); System.out.println(" is odd"); } Output 1 is odd 2 is even 3 is odd 4 is even 3-22 Copyright (c) 1999-2004 N. Afshartous

While Loop Syntax: while (bool_expression) statement The statement is executed while the boolean expression remains true int i = 1; while (i < 5) System.out.println(i++); Output 1 2 3 4 3-23 Copyright (c) 1999-2004 N. Afshartous

Grouping with Braces Use braces to create a loop body with more than one statement int i = 1; while (i < 5){ System.out.print("i = "); System.out.println(i++); } Output i = 1 i = 2 i = 3 i = 4 3-24 Copyright (c) 1999-2004 N. Afshartous

Summing Calculate the sum of all numbers from 1 to 10 int sum = 0; int i = 1; while (i <= 10) sum += i++; System.out.print("the sum from 1 to 10 is: "); System.out.println(sum); Output the sum from 1 to 10 is: 55 3-25 Copyright (c) 1999-2004 N. Afshartous

Switch Statement The switch statement dispatches control flow based on an integer or character value int itemnumber = 2; switch (itemnumber) { case 1: price = 10; break; case 2: price = 20; break; case 3: price = 30; break; default: System.out.println("invalid price"); } System.out.print("Item price: $"); System.out.println(price); Output Item price: $20 3-26 Copyright (c) 1999-2004 N. Afshartous

Default Case Default branch is executed if no case matches int itemnumber = 10; boolean valid = true; switch (itemnumber) { case 1: price = 10; break; case 2: price = 20; break; case 3: price = 30; break; default: valid = false; System.out.println("invalid price"); } if (valid) { System.out.print("Item price: $ "); System.out.println(price); } Output invalid price 3-27 Copyright (c) 1999-2004 N. Afshartous

Switch with Characters char charcode = a ; boolean valid = true; switch (charcode) { case a : price = 10; break; case b : price = 20; break; case c : price = 30; break; default: valid = false; System.out.println("invalid price"); } if (valid) { System.out.print("Item price: $"); System.out.println(price); } Output Item price: $10 3-28 Copyright (c) 1999-2004 N. Afshartous

Multiple Case Labels All three cases correspond to the same branch char charcode = a ; boolean valid = true; switch (charcode) { case a : case b : case c : price = 30; break; default: valid = false; System.out.println("invalid price"); } if (valid) { System.out.print("Item price: $ "); System.out.println(price); } 3-29 Copyright (c) 1999-2004 N. Afshartous

It s Exercise Time 3-30 Copyright (c) 1999-2004 N. Afshartous