University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.

Slides:



Advertisements
Similar presentations
Session 5 JavaScript/JScript: Control Structures II Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.11 Assignment Operators 2.12 Increment and Decrement Operators.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter5: Control Statements Part II.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Chapter 5 - Control Structures - Part 2 Outline 5.1Introduction 5.2Essentials of Counter-Controlled Repetition 5.3The for Repetition Structure 5.4Examples.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
Program Control Dilshad M. Shahid New York
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2005 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
© 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
1 Lecture 4 for loops and switch statements Essentials of Counter-Controlled Repetition Counter-controlled repetition requires  Name of control.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
 The initialization, loop-continuation condition and increment portions of a for statement can contain arithmetic expressions.  For example,
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled.
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.
 2009 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
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.
IE 411/511: Visual Programming for Industrial Applications Lecture Notes #5 Control Statements: Part 2.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Sections 5.1 – 5.4 © Copyright by Pearson Education, Inc. All Rights Reserved.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Essentials of Counter-Controlled Repetition Counter-controlled repetition requires: Control variable (loop counter) Initial value of the control variable.
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
 2009 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Control Statements: Part1  if, if…else, switch 1.
 2005 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
 2007 Pearson Education, Inc. All rights reserved Control Statements: Part2.
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.

 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Chapter 4 Control Statements: Part 2
Chapter 4 – C Program Control
Chapter 4 C Program Control Part I
Chapter 5- Control Structures: Part 2
Control Statements: Part 2
Control Structures: Part 2
JavaScript: Control Statements I
Chapter 5- part 2 Control Statements: Loops 2
Chapter 8 JavaScript: Control Statements, Part 2
Chapter 5 Control Statements: Loops 2
Chapter 6 Control Statements: Part 2
Chapter 4 - Program Control
Presentation transcript:

University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish

Outlines Essentials of Counter-Controlled Repetition for Repetition Statement Examples Using the for Statement do…while Repetition Statement switch Multiple-Selection Statement break and continue Statements Logical Operators

Counter-controlled repetition requires 1. a control variable (or loop counter) 2. the initial value of the control variable 3. the increment (or decrement) by which the control variable is modified each time through the loop (also known as each iteration of the loop) 4. the loop-continuation condition that determines whether looping should continue. Essentials of Counter-Controlled Repetition

the for repetition statement, which specifies the counter-controlled-repetition details in a single line of code for Repetition Statement

line 10 in is sometimes called the for statement header, or simply the for header for Repetition Statement

If the initialization expression in the for header declares the control variable the control variable can be used only in that for statement. This restricted use of the name of the control variable is known as the variable’s scope. The scope of a variable defines where it can be used in a program for Repetition Statement

All three expressions in a for header are optional. If the loopContinuationCondition is omitted, Java assumes that the loop-continuation condition is always true, thus creating an infinite loop. You might omit the initialization expression if the program initializes the control variable before the loop. You might omit the increment expression if the program calculates the increment with statements in the loop’s body or if no increment is needed for Repetition Statement

Examples Using the for Statement Vary the control variable from 1 to 100 in increments of 1. Vary the control variable from 100 to 1 in decrements of 1. Vary the control variable from 7 to 77 in increments of 7. Vary the control variable from 20 to 2 in decrements of 2. Vary the control variable over the following sequence of values: 2, 5, 8, 11, 14, 17, 20.

Examples Using the for Statement Vary the control variable over the following sequence of values: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0.

Examples Using the for Statement Application: Summing the Even Integers from 2 to 20

Examples Using the for Statement Application: Compound-Interest Calculations A person invests $1000 in a savings account yielding 5% interest. Assuming that all the interest is left on deposit, calculate and print the amount of money in the account at the end of each year for 10 years. Use the following formula to determine the amounts: a = p (1 + r) n where p is the original amount invested (i.e., the principal) r is the annual interest rate (e.g., use 0.05 for 5%) n is the number of years a is the amount on deposit at the end of the nth year.

Examples Using the for Statement

the format specifier %20s output the String "Amount on Deposit“ and it is displayed with a field width of 20—that is, printf displays the value with at least 20 character positions. If the value to be output is less than 20 character positions wide, the value is right justified in the field by default If the year value to be output were more than four character positions wide, the field width would be extended to the right to accommodate the entire value

Examples Using the for Statement To indicate that values should be output left justified, simply precede the field width with the minus sign (–) formatting flag. Many classes also provide methods that perform common tasks and do not require objects. these are called static methods. For example, the designers of Java’s Math class defined static method pow for raising a value to a power. You can call a static method by specifying the class name followed by a dot (.) and the method name, as in ClassName.methodName( arguments )

Examples Using the for Statement In the format specifier %,20.2f The comma (,) formatting flag indicates that the floating-point value should be output with a grouping separator. It is displayed with a field width of 20 The.2 specifies the formatted number’s precision

do … while Repetition Statement The do…while statement tests the loop-continuation condition after executing the loop’s body; therefore, the body always executes at least once

do … while Repetition Statement

Class work

switch Multiple-Selection Statement Java provides the switch multiple-selection statement to perform different actions based on the possible values of an integer variable or expression. Each action is associated with the value of a constant integral expression (i.e., a constant value of type byte, short, int or char, but not long) that the variable or expression on which the switch is based may assume.

switch Multiple-Selection Statement

break and continue Statements The break statement, when executed in a while, for, do…while or switch, causes immediate exit from that statement

break and continue Statements The continue statement, when executed in a while, for or do…while, skips the remaining statements in the loop body and proceeds with the next iteration of the loop. In while and do…while statements, the program evaluates the loop-continuation test immediately after the continue statement executes. In a for statement, the increment expression executes, then the program evaluates the loop-continuation test.

break and continue Statements

Logical Operators Java provides logical operators to enable you to form more complex conditions by combining simple conditions. The logical operators are: && (conditional AND), || (conditional OR), & (boolean logical AND), | (boolean logical inclusive OR), ^ (boolean logical exclusive OR) ! (logical NOT).

Logical Operators Conditional AND (&&) Operator if ( grade >= 90 && level== 5 ) ++levelfivestudent; Conditional OR (||) Operator if ( ( semesterAverage >= 90 ) || ( finalExam >= 90 ) ) System.out.println ( "Student grade is A" );

Logical Operators Boolean Logical AND (&) and Boolean Logical Inclusive OR (|) Operators The boolean logical AND (&) and boolean logical inclusive OR (|) operators work identically to the && (conditional AND) and || (conditional OR) operators, with one exception: The boolean logical operators always evaluate both of their operands (i.e., they do not perform short-circuit evaluation). ( birthday == true ) | ( ++age >= 65 )

Logical Operators Boolean Logical Exclusive OR (^) A simple condition containing the boolean logical exclusive OR (^) operator is true if and only if one of its operands is true and the other is false Logical Negation (!) Operator if ( ! ( grade == sentinelValue ) ) System.out.printf( "The next grade is %d\n", grade ); if ( grade != sentinelValue ) System.out.printf( "The next grade is %d\n", grade );