Notes Java

Topics: Programming language, Control flow, Java Pages: 42 (9916 words) Published: May 12, 2013
UNIT 3 i
Control structures

Unit 3
TCC 121/05

Programming Fundamentals with Java

Control Structures

TCC 121/05 Programming Fundamentals with Java

Course Team Coordinator: Mr. Vincent Chung Sheng Hung Content Writer: Dr. Wong Li Pei Instructional Designer: Ms. Jeanne Chow Academic Member: Ms. Khor Ean Teng

Mr. Vincent Chung Sheng Hung

Professor Andrew Lui, The Open University of Hong Kong

In-house Editor: Ms. Jeanne Chow Graphic Designer: Ms. Audrey Yeong

Wawasan Open University is Malaysia’s first private not-for-profit tertiary institution dedicated to adult learners. It is funded by the Wawasan Education Foundation, a tax-exempt entity established by the Malaysian People’s Movement Party (Gerakan) and supported by the Yeap Chor Ee Charitable and Endowment Trusts, other charities, corporations, members of the public and occasional grants from the Government of Malaysia. The course material development of the university is funded by Yeap Chor Ee Charitable and Endowment Trusts.

© 2008 Wawasan Open University First revision 2011, Second revision 2012

Except where otherwise noted, this work is licensed under the terms of the Creative Commons Attribution-ShareAlike 3.0 Unported License. To view a copy of this license, visit http:// or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA. This course material was published to support the learning of students registered with Wawasan Open University. Wawasan Open University does not grant any degree, certification or credits based solely on your completion of this course material.

Wawasan Open University Jalan Sultan Ahmad Shah, 10050 Penang. Tel: (604) 2180333 Fax: (604) 2279214 Email: Website:

UNIT 3 iii
Control structures

Unit 3 Control Structures
Unit overview Unit objectives 3.1 Comparative and logical operators Objectives Introduction Comparative operators Logical operators The AND operator (&&) The OR operator (||) The NOT operator (!) The exclusive OR operator (^) Comparing objects Comparing strings Suggested answers to activities 1 1

3 3 3 3 5 6 6 7 8 10 12 16

3.2 Selection or branching
Objectives Introduction One-way selection Two-way selection Compound statements Multiple selections Conditional operator (? :) switch structures Suggested answers to activities

19 19 19 20 23 26 28 32 34 41

TCC 121/05 Programming Fundamentals with Java

3.3 Loop or repetition
Objectives Introduction What is a loop structure and why it is needed? while looping structure do...while looping structure for looping structure break and continue statements in a looping structure Nested looping structure Suggested answers to activities

47 47 47 47 48 50 53 57

59 64

Summary of Unit 3 Suggested answers to self-tests References

69 71 77

UNIT 3 1
Control structures

Unit Overview


ontrol flow is the concept used to control the order in which code executes. Control flow is common to all popular programming languages including the Java programming language. Programmers need control flows because solutions are rarely straightforward enough to just start sequentially at the top and finish at the bottom. To come out with a solution for a real-world problem, programmers need something to be done which relies on something else (conditional) or to repeat certain tasks (loops). For example, a company computes wages for the employees. In such scenarios, the wages might be computed as follows: if hours worked is less than or equal to 40, wages is equal to 2.3 times hours worked. This is an example of the branching structure needed in designing a computerised solution. As for the repetition structure, we can use a repetition structure to display all the odd numbers...
Continue Reading

Please join StudyMode to read the full document

You May Also Find These Documents Helpful

  • Computer Application Java Notes Class 10 Essay
  • Key Words in Java Essay
  • Essay about Javascript Course Notes
  • Essay on Note Taking Skills
  • Java Essay
  • Essay about Taking Notes in College
  • Journal on Note Taking Essay
  • JAVA Essay

Become a StudyMode Member

Sign Up - It's Free