Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Similar documents
Object-Oriented Programming in Java

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

Chapter 4: Loops and Files

Chapter 4: Loops and Files

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1

Chapter 5: Prefix vs. Postfix 8/19/2018. The Increment and Decrement Operators. Increment and Decrement Operators in Program 5-1

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Chapter 5: Loops and Files

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Increment and the While. Class 15

Java Loop Control. Programming languages provide various control structures that allow for more complicated execution paths.

Introduction. C provides two styles of flow control:

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Repetition CSC 121 Fall 2014 Howard Rosenthal

DELHI PUBLIC SCHOOL TAPI

LECTURE 5 Control Structures Part 2

Chapter 4: Control structures. Repetition

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

Chapter 4: Control structures

REPETITION CONTROL STRUCTURE LOGO

Computer Programming I - Unit 5 Lecture page 1 of 14

For Loop. Variations on Format & Specific Examples

Looping. Arizona State University 1

Lecture 7 Tao Wang 1

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

The for Loop. Lesson 11

Introduction to the Java Basics: Control Flow Statements

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Programming Constructs Overview. Method Call System.out.print( hello ); Method Parameters

Example. Write a program which sums two random integers and lets the user repeatedly enter a new answer until it is correct.

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

CS110D: PROGRAMMING LANGUAGE I

ECE 122. Engineering Problem Solving with Java

Chapter Goals. Contents LOOPS

Loops / Repetition Statements

While Loops CHAPTER 5: LOOP STRUCTURES. While Loops. While Loops 2/7/2013

Introduction to C/C++ Lecture 3 - Program Flow Control

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

Programming Fundamentals

Lecture 10. Daily Puzzle

Chapter 4: Making Decisions

Programming for Engineers Iteration

More on control structures

Chapter 4: Making Decisions

Example: Monte Carlo Simulation 1

Prepared by: Shraddha Modi

Control Statements. Objectives. ELEC 206 Prof. Siripong Potisuk

Assignment: 1. (Unit-1 Flowchart and Algorithm)

Instructor: SIR MUHAMMAD NAVEED Created by: ARSLAN AHMED SHAAD ( ) MUHAMMAD BILAL ( ) ISIT:

STUDENT LESSON A12 Iterations

A Look Back at Arithmetic Operators: the Increment and Decrement

Recap: Assignment as an Operator CS 112 Introduction to Programming

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

Iteration statements - Loops

L o o p s. for(initializing expression; control expression; step expression) { one or more statements }

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Decision Structures. Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Topics. Introduction to Repetition Structures Often have to write code that performs the same task multiple times. Controlled Loop

CS 112 Introduction to Programming

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

10/30/2010. Introduction to Control Statements. The if and if-else Statements (cont.) Principal forms: JAVA CONTROL STATEMENTS SELECTION STATEMENTS

Decision Structures. Lesson 03 MIT 11053, Fundamentals of Programming

Chapter 6. Section 6.4 Altering the Flow of Control. CS 50 Hathairat Rattanasook

Control Statements. Musa M. Ameen Computer Engineering Dept.

int j = 0, sum = 0; for (j = 3; j <= 79; j++) { sum = sum + j; System.out.println(sum); //Show the progress as we iterate thru the loop.

CHAPTER 9 FLOW OF CONTROL

LECTURE 04 MAKING DECISIONS

More Programming Constructs -- Introduction

Control Structures II. Repetition (Loops)

Chapter 2: Functions and Control Structures

Module 4: Decision-making and forming loops

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Operators. Java operators are classified into three categories:

3 The L oop Control Structure

Lecture 7: General Loops (Chapter 7)

Unit 5. Decision Making and Looping. School of Science and Technology INTRODUCTION

Technical Questions. Q 1) What are the key features in C programming language?

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Information Science 1

Flow Control. CSC215 Lecture

Repetition and Loop Statements Chapter 5

YOLOP Language Reference Manual

Information Science 1

PLD Semester Exam Study Guide Dec. 2018

Le L c e t c ur u e e 3 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Control Statements

CSCE150A. Introduction. While Loop. Compound Assignment. For Loop. Loop Design. Nested Loops. Do-While Loop. Programming Tips CSCE150A.

Lecture 3 Operators MIT AITI

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 5. Repetition in Programs. Notes. Notes. Notes. Lecture 05 - Loops

Transcription:

Loops and Files Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Chapter Topics o The Increment and Decrement Operators o The while Loop o Shorthand Assignment Operators o The do-while Loop o The for Loop o Nested Loops o The break and continue Statements o Labeled Loops MIT 12043, By S. Sabraz Nawaz

The Increment and Decrement Operators MIT 12043, By S. Sabraz Nawaz 3

The Increment and Decrement Operators There are numerous times where a variable must simply be incremented or decremented like number = number + 1; number = number 1; Java provides two unary operators for adding 1 to or subtracting 1 from the value of a numeric variable. These are the unary increment operator, ++, and the unary decrement operator -- Operator Operator Name Sample Expression Explanation ++ prefix increment ++a Increment a by 1, then use the new value of a in the expression in which a resides. ++ -- -- postfix increment prefix decrement postfix decrement a++ --b b-- Use the current value of a in the expression in which a resides, then increment a by 1. Decrement b by 1, then use the new value of b in the expression in which b resides. Use the current value of b in the expression in which b resides, then decrement b by 1. MIT 12043, By S. Sabraz Nawaz 4

Differences Between Prefix and Postfix An increment or decrement operator that s prefixed to (placed before) a variable is referred to as the prefix increment or prefix decrement operator An increment or decrement operator that s postfixed to (placed after) a variable is referred to as the postfix increment or postfix decrement operator When used in an expression: o prefix notation indicates that the variable will be incremented or decremented prior to the rest of the equation being evaluated o postfix notation indicates that the variable will be incremented or decremented after the rest of the equation has been evaluated MIT 12043, By S. Sabraz Nawaz 4-5

Differences Between Prefix and Postfix MIT 12043, By S. Sabraz Nawaz 6

Differences Between Prefix and Postfix MIT 12043, By S. Sabraz Nawaz 7

Looping / Repetition MIT 12043, By S. Sabraz Nawaz 8

Loops A repetition (or looping) statement allows you to specify that a program should repeat an action while some condition remains true. Two kinds of loops o Finite loop: The statements in the block may be executed any number of times, from zero to infinite number. o Infinite loop: A loop that continues forever. A loop consist of; o Body of the loop o Control statement MIT 12043, By S. Sabraz Nawaz 9

Looping Constructs in Java Java provides three different looping structures. o The while loop o The do while loop o The for loop MIT 12043, By S. Sabraz Nawaz 10

Loop Control Structures Entry Control loop Exit Control loop cond true loop body false loop body false cond true If condition is not satisfied body of the loop is never executed. The body of the loop is executed unconditionally for the 1st time. MIT 12043, By S. Sabraz Nawaz 11

Important Parts in a Loop Initialization: The variables tested in the condition must be initialized to some values. If the condition is false at the outset, the loop is never entered. Testing: The condition is tested before each iteration. If false, the program continues with the first statement after the loop. Change: At least one of the variables tested in the condition must change within the body of the loop. MIT 12043, By S. Sabraz Nawaz 12

while Loop MIT 12043, By S. Sabraz Nawaz 13

The while Loop The while loop is a pretest loop, which means that it will test the value of the condition prior to executing the loop. While the condition is true, the statements will execute repeatedly. MIT 12043, By S. Sabraz Nawaz 14

The while loop Flowchart boolean expression? true statement(s) false MIT 12043, By S. Sabraz Nawaz 15

The while Loop This is an entry-controlled loop The body of the loop initialization; while (condition) { statement1; statement2;... statementn; } condition is any logical expression, as in if MIT 12043, By S. Sabraz Nawaz 16

The while Loop Care must be taken to set the condition to false somewhere in the loop so the loop will end. Loops that do not end are called infinite loops. A while loop executes 0 or more times. If the condition is false, the loop will not execute. Example: The following program displays numbers from 1 to 10 on the screen MIT 12043, By S. Sabraz Nawaz 17

Loop Example 01 Write a program that uses a while loop to print the sum of integers from 1 to 10. MIT 12043, By S. Sabraz Nawaz 18

Infinite Loops In order for a while loop to end, the condition must become false. The following loop will not end: int x = 20; while(x > 0) { System.out.println("x is greater than 0"); } The variable x never gets decremented so it will always be greater than 0. MIT 12043, By S. Sabraz Nawaz 19

Shorthand Assignment Operators

Shorthand Assignment Operators Java provides special shorthand assignment operators that simplify the coding of certain assignment statements. The assignment statement x = x + 10; can be written, using Java shorthand, as x += 10; The assignment statement x = x - 10; is the same as x -= 10; Like that x = x * 10 x *= 10 Like that x = x / 10 x /= 10 MIT 12043, By S. Sabraz Nawaz 21

Loop Example 02 The Example 01 is modified using shorthand assignment operator MIT 12043, By S. Sabraz Nawaz 22

do while Loop MIT 12043, By S. Sabraz Nawaz 23

The do-while Loop The do-while loop is a post-test loop, which means it will execute the loop prior to testing the condition MIT 12043, By S. Sabraz Nawaz 24

The do-while Loop Flowchart statement(s) boolean expression? true false MIT 12043, By S. Sabraz Nawaz 25

The do-while Loop This is an exit-controlled loop The code runs through the body of the loop at least once initialization; do { statement1; statement2;... statementn; } while ( condition ); if condition is false, the next iteration is not executed 26

Loop Example 03 Write a program that uses do-while loop to print the sum of squares of integers from 1 to 10.

Loop Example 04 In this example you can see that although the expression evaluates to false, the code inside the loop block is executed once; this is because do-while is exit controlled loop MIT 12043, By S. Sabraz Nawaz 28

for Loop MIT 12043, By S. Sabraz Nawaz 29

The for Loop The for loop is a pre-test loop The for loop allows the programmer to initialize a control variable, test a condition, and modify the control variable all in one line of code MIT 12043, By S. Sabraz Nawaz 30

The for Loop Flowchart boolean expression? true statement(s) update false MIT 12043, By S. Sabraz Nawaz 31

The for Loop for(initialization; condition; update) { statement1; statement2;... statementn; } 32

The Sections of The for Loop The initialization section of the for loop allows the loop to initialize its own control variable. The initialization section can initialize multiple variables. Variables declared in this section have scope only for the for loop The condition of the for statement acts in the same manner as the condition section of a while loop The update of the for loop is the last thing to execute at the end of each loop MIT 12043, By S. Sabraz Nawaz 33

Loop Example 05 The following example displays numbers from 1 to 10 MIT 12043, By S. Sabraz Nawaz 34

Loop Example 06 Write a program that uses for loop to print the sum of squares of integers from 1 to 10.

Loop Example 07 Display the even numbers between 0 and 20 MIT 12043, By S. Sabraz Nawaz 36

Nested Loops MIT 12043, By S. Sabraz Nawaz 37

Nested Loops Like if statements, loops can be nested. If a loop is nested, the inner loop will execute all of its iterations for each time the outer loop executes once. for(int i = 0; i < 10; i++) for(int j = 0; j < 10; j++) loop statements; The loop statements in this example will execute 100 times. MIT 12043, By S. Sabraz Nawaz 38

Loop Example 08 Develop a 10 x 10 multiplication table MIT 12043, By S. Sabraz Nawaz 39

Summary of Loop Control Structures for for (n=1;n<=10;n++) {.... } while n = 1; while (n<=10) {.... n = n+1; } do while n = 1; do {.. n+=1; } while (n<=10);

break and continue MIT 12043, By S. Sabraz Nawaz 41

The break Statement - jumping out of a loop It is possible to force an immediate exit from a loop, bypassing any remaining code in the body of the loop and the loop s conditional test, by using the break statement When a break statement is encountered inside a loop, the loop is terminated and program control resumes at the next statement following the loop (It is considered bad form to use the break statement in this manner) MIT 12043, By S. Sabraz Nawaz 42

break Statement

Loop Break 02 Go through the code and understand what s going on The use of break statement MIT 12043, By S. Sabraz Nawaz 44

Loop Break 03 Go through the code and understand what s going on The use of break statement MIT 12043, By S. Sabraz Nawaz 45

The continue Statement - skipping a part of a loop The continue statement forces the next iteration of the loop to take place, skipping any code between itself and the conditional expression that controls the loop Like the break statement, the continue statement should be avoided because it makes the code hard to read and debug MIT 12043, By S. Sabraz Nawaz 46

continue Statement 47

Loop Continue 01 In the above example, the continue statement causes the next iteration of the for loop Variable i takes the value from 0 15 but only even numbers are displayed on the screen MIT 12043, By S. Sabraz Nawaz 48

Labeled Loops To jump outside a loop that is outside the current one OR to continue a loop that is outside the current one The continue statement terminates the inner loop when j = i and continues with the next iteration of the outer loop 49

End of Lesson MIT 12043, By S. Sabraz Nawaz 50