Expressions An expression is a series of variables, operators, and method calls (constructed according to the syntax of the language) that evaluates to.

Slides:



Advertisements
Similar presentations
Control Structures Corresponds with Chapters 3 and 4.
Advertisements

8-May-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Introduction to Control Statements Presented by: Parminder Singh BCA 5 th Sem. [ Batch] PCTE, Ludhiana 5/12/ Control Statements.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 2 Sanjay Goel University at Albany,
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
SYDE223 Tutorial 3: Introduction to Java Annie En-Shiun Lee January 19, 2010.
UNIT II Decision Making And Branching Decision Making And Looping
L EC. 03: C ONTROL STATEMENTS Fall Java Programming.
DAT602 Database Application Development Lecture 5 JAVA Review.
Fundamentals of C and C++ Programming Control Structures and Functions.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
Controlling Execution Dong Shao, Nanjing Unviersity.
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,
Chapter 05 (Part III) Control Statements: Part II.
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.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
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.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
 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.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
JavaScript and Ajax (Control Structures) Week 4 Web site:
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
JavaScript, Sixth Edition
ECE122 Feb 10, Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)
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.
Chapter 3: Decisions and Loops
Control Structures.
CiS 260: App Dev I Chapter 4: Control Structures II.
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
JavaScript: Control Statements I
Starting JavaProgramming
Outline Altering flow of control Boolean expressions
Pemrograman Dasar Smt I 2004/2005
In this class, we will cover:
Lab5 PROGRAMMING 1 Loop chapter4.
In this class, we will cover:
Chap 7. Advanced Control Statements in Java
Presentation transcript:

Expressions An expression is a series of variables, operators, and method calls (constructed according to the syntax of the language) that evaluates to a single value. Expressions perform the work of a program. Expressions are used to compute and to assign values to variables and to help control the execution flow of a program. The job of an expression is twofold: –To perform the computation indicated by the elements of the expression –To return a value that is the result of the computation.

Expressions Examples: char aChar = 'S'; boolean aBoolean = true; System.out.println("The largest byte value is " + largestByte);... if (Character.isUpperCase(aChar)) {... }

The operators Precedence

Statements Statements are roughly equivalent to sentences in natural languages. A statement forms a complete unit of execution. The types of Statement: –declaration –control flow –Assignment expressions –Any use of ++ or -- –Method calls –Object creation expressions

Statements Examples: –aValue = ;//assignment statement –aValue++; //increment statement –System.out.println(aValue); //method call statement –Test testObj = new Test(4);//Object creation statement –double aValue = ; //Declaration statement –if (…) { … } //Control Flow Statement –for (… ; … ; … ) { … } //Control Flow Statement

Blocks A block is a group of zero or more statements between balanced braces A block can be used anywhere a single statement is allowed. public class MaxVariablesDemo { public static void main(String args[]) { byte largestByte = Byte.MAX_VALUE; if (Character.isUpperCase(aChar)) { System.out.println("The character " + aChar + " is upper case."); } else { System.out.println("The character " + aChar + " is lower case."); } System.out.println("The value of aBoolean is " + aBoolean); }

Control Flow Statements Without control flow statements, the interpreter executes statements in the order they appear in the file from left to right, top to bottom. Control flow statements are used to, –Conditionally execute statements, –Repeatedly execute a block of statements, –And otherwise change the normal, sequential flow of control. The general form of a control flow statement: control flow statement details { statement(s) } The braces, { and }, are not required if the block contains only one statement.

Control Flow Statements

The if/else Statements The if statement enables your program to selectively execute other statements, based on some criteria. Generally, the simple form of if can be written like this: if (expression) { statement(s) } The else block is executed if the if part is false. Generally, the simple form of if-else can be written like this: if (expression) { statement(s) //The expression value is true } else { statement(s) //The expresion value is false }

The if/else Statements Another form of the else statement, else if, executes a statement based on another expression. An if statement can have any number of companion else if statements but only one else. Example: IfElseDemo.java

The if/else Statements The Java programming language supports an operator, ?:, that is a compact version of an if statement. In Example: if (Character.isUpperCase(aChar)) { System.out.println("The character " + aChar + " is upper case."); } else { System.out.println("The character " + aChar + " is lower case."); } That statement could rewrite using the ?: operator: System.out.println("The character " + aChar + " is " + (Character.isUpperCase(aChar) ? "upper" : "lower") + "case.");

The switch Statement Use the switch statement to conditionally perform statements based on an integer expression. An if statement can be used to make decisions based on ranges of values or conditions, whereas a switch statement can make decisions based only on a single integer value. Also, the value provided to each case statement must be unique. A sample program: SwitchDemo.java

The switch Statement Another point of interest in the switch statement is the break statement after each case. Each break statement terminates the enclosing switch statement, and the flow of control continues with the first statement following the switch block. The break statements are necessary because without them, the case statements fall through. That is, without an explicit break, control will flow sequentially through subsequent case statements. Example: SwitchDemo2.java Finally, you can use the default statement at the end of the switch to handle all values that aren't explicitly handled by one of the case statements.

The switch Statement... switch (month) { case 1: System.out.println("January"); break; case 2: System.out.println("February"); break; case 3: System.out.println("March"); break; case 4: System.out.println("April"); break; case 5: System.out.println("May"); break; case 6: System.out.println("June"); break; case 7: System.out.println("July"); break; case 8: System.out.println("August"); break; case 9: System.out.println("September"); break; case 10: System.out.println("October"); break; case 11: System.out.println("November"); break; case 12: System.out.println("December"); break; default: System.out.println("Hey, that's not a valid month!"); break; } Example SwitchDemo3.java

The for Statement The for statement provides a compact way to iterate over a range of values. The general form of the for statement can be expressed like this: for (initialization; termination; increment) { statement(s) } Example: ForDemo.java

The for Statement The initialization is an expression that initializes the loop-it's executed once at the beginning of the loop. The termination expression determines when to terminate the loop. This expression is evaluated at the top of each iteration of the loop. When the expression evaluates to false, the loop terminates. The increment is an expression that gets invoked after each iteration through the loop. All these components are optional. for ( ; ; ) { // infinite loop... }

The while Statements A while statement is used to continually execute a block of statements while a condition remains true. The general syntax of the while statement is: while (expression) { statement } First, the while statement evaluates expression, which must return a boolean value. If the expression returns true, then the while statement executes the statement(s) associated with it. The while statement continues testing the expression and executing its block until the expression returns false. Example: WhileDemo.java

The do-while Statements The Java programming language provides another statement that is similar to the while statement—the do-while statement. The general syntax of the do - while is: do { statement(s) } while (expression); Instead of evaluating the expression at the top of the loop, do-while evaluates the expression at the bottom. Thus the statements associated with a do-while are executed at least once. Example: DoWhileDemo.java

The break Statements The break statement has two forms: unlabeled and labeled. An unlabeled break terminates the enclosing switch statement, and flow of control transfers to the statement immediately following the switch. It can be also used to terminate a for, while, or do-while loop. Example: BreakDemo.java The unlabeled form of the break statement is used to terminate the innermost switch, for, while, or do-while.

The break Statements The labeled form terminates an outer statement, which is identified by the label specified in the break statement. Example: BreakLabelDemo.java The break statement terminates the labeled statement; it does not transfer the flow of control to the label. The flow of control transfers to the statement immediately following the labeled (terminated) statement.

The continue Statement You use the continue statement to skip the current iteration of a for, while, or do-while loop. The unlabeled form skips to the end of the innermost loop's body and evaluates the boolean expression that controls the loop, basically skipping the remainder of this iteration of the loop. Example: ContinueDemo.java The labeled form of the continue statement skips the current iteration of an outer loop marked with the given label. Example: ContinueLabelDemo.java