ECE122 Feb 10, 2005. Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

Introduction to C Programming
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
 2007 Pearson Education, Inc. All rights reserved C Program Control.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 9 - JavaScript: Control Statements II Outline 9.1 Introduction 9.2 Essentials of Counter-Controlled.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
1 CSCE 1030 Computer Science 1 Control Statements in Java.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 8 Java Fundamentals Control Structures Fri.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
ECE 122 Feb. 8, if Statement A condition is an expression that can be either true, or false. “if” statement allows a program to make a decision.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
Chapter 4 – C Program Control
Control Statements: Part 1
JavaScript: Control Statements I
Lecturer CS & IT Department UOS MBDIN
JavaScript: Control Statements.
JavaScript: Control Statements I
Chapter 5- part 2 Control Statements: Loops 2
Programming Fundamentals Lecture #6 Program Control
MSIS 655 Advanced Business Applications Programming
Chapter 8 JavaScript: Control Statements, Part 2
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises UTPA – Fall 2012 This set of slides is revised from lecture.
The University of Texas – Pan American
3 Control Statements:.
Chapter 5 Control Statements: Loops 2
Control Statements Paritosh Srivastava.
Chapter 8 JavaScript: Control Statements, Part 2
Control Statements:.
Presentation transcript:

ECE122 Feb 10, 2005

Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)

Compound Assignment Operator OperatorExampleMeaning +=x += yx = x + y -=x -= yx = x - y *=x *= yx = x * y /=x /= yx = x / y %=x %= yx = x % y

Increment & Decrement Operator Prefix increment ++e.g. ++i Increase i by 1, then use the new value of i to evaluate the expression that i resides Postfix increment ++e.g. i++ Use the current value of i to evaluate the expression that i resides, then increase i by 1 Prefix decrement --e.g. --i Decrease i by 1, then use the new value of i to evaluate the expression that i resides Postfix decrement -- e.g. I\-- Use the current value of i to evaluate the expression that i resides, then decrease i by 1

Operator Precedence OperatorType ++ --Unary postfix Unary prefix * / %multiplicative + -additive >=relational == !=equality ? :conditional = += -= *= /= %=assignment

Pseudo Code Pseudo code is informal language that helps programmer THINK and develop algorithm without worrying about strict Java language. Pseudo code can be written in plain English. Pseudo code doesn’t need to execute on a computer

Pseudo code and real code Pseudo code If test score is higher than or equal to 60, Print out the message to console, “I passed”. If the test score is lower than 60, Print out the message to console, “I failed”. Java code if (testScore >= 60) System.out.println(“I passed”); else System.out.println(“I failed”);

switch statement Multiple selection statement. Perform a different action dependent on the value of a variable or expression. The value of the variable or expression can only be any of data type of byte, short, int, or char.

Example of switch statement switch (temperature/10) { case 9: System.out.println("The temperature is higher than 90."); break; //Cannot omit this case 8: System.out.println("The temperature is between 80 and 90."); break; default: System.out.println("The temperature is between 70 and 80."); break; }

break Statement The “break” statement, when executed in a “switch”, “while”, “for”, “do…while” causes immediate exit from that statement. Execution continues with the first statement after the control statement. Common uses of the break statement are to skip the remainder of a switch. Without a break statements, each time a match occurs in the switch, the statements for that case and subsequent cases execute until a break statement or the end of the switch is encountered. “Falling through” to the statements in subsequent cases. It is a good practice to have “break” statement at the end of each “case”, so that accidental falling through will not happen.

while loop A repetition statement (loop) allows a block of code between {} to repeat while some condition remains true. Braces can be omitted if the block statement is a single statement. int i = 10; while (i < 100) i = i + 10;

Counter-controlled Repetition Use a variable called a counter (or control variable) to control the number of repetition. Definite repetition. The number of repetition is known before the loop begins executing.

Example: WhileStatement

Nested Control Structure Control statement can be nested with each other to create a more power control structure. int j = 5; while (j > 0) { if (j > 2) System.out.println("i is "+ j + " and is greater than 2"); else System.out.println("i is " + j + " and is less than 2"); j = j - 1; }

do…while repetition statement Similar to while repetition statement In while statement, the program will evaluate loop-continuation condition first before executing any statement. In do…while statement, the program will execute the statement at least once before evaluating loop-continuation condition

Compare do…while and while do…while int k = -1; do { System.out.println("k="+k); //This statement execute } while (k > 0); While int l = -1; while (l > 0) { System.out.println("l="+l); //This statement will not execute }

Demo do…while

Essential of Counter-controlled Repetition A control variable (loop counter) The initial value of control variable The increment (or decrement) by which the control variable is modified each time through the loop (each iteration of the loop). The loop-continuation condition that determines whether looping should continue.

while/do..while generalization While statement Initialization; while (loopContinuationCondition) { statement; increment; } Do..while statement Initialization; Do { statement; increment; } while (loopContinuationCondition)

Reading Assignment “Java 2” p78-83, 92-97, p , p