Lab5 PROGRAMMING 1 Loop chapter4.

Slides:



Advertisements
Similar presentations
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Advertisements

Introduction to Control Statements Presented by: Parminder Singh BCA 5 th Sem. [ Batch] PCTE, Ludhiana 5/12/ Control Statements.
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Iteration and Simple Menus Deterministic / Non-deterministic loops and simple menus.
Copyright 2009 by Pearson Education Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos: Ch.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
1 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
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.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Lecture 4b Repeating With Loops
Chapter 6: Loops.
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II
Control Structures.
Chapter 5: Control Structures II
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Chapter 5: Control Structures II
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
CiS 260: App Dev I Chapter 4: Control Structures II.
JavaScript: Control Statements.
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Control Structures - Repetition
Looping and Repetition
MSIS 655 Advanced Business Applications Programming
Outline Altering flow of control Boolean expressions
Lecture Notes – Week 3 Lecture-2
Control Statements Loops.
SELECTIONS STATEMENTS
Control Statements Loops.
Java LESSON 3 Loops.
PROGRAM FLOWCHART Iteration Statements.
Lab6 PROGRAMMING 1 metheds chapter5.
Lab4 PROGRAMMING 1 exercise chapter3.
Looping and Repetition
Control Statements:.
Presentation transcript:

Lab5 PROGRAMMING 1 Loop chapter4

BEFORE WE START Your Cell Phone Silent Please Keep Quite Find Your Computer and Switch On Ins.Ebtesam AL-Etowi

content . Loops & Selection Statements. introduction to Loop While, Do-while and For . Differences between While, Do-while and for . To implement program control with break and continue . Ins.Ebtesam AL-etowi

Loop &&selection statement Selection statements, like if…else, allow for conditional execution of code blocks in a single iteration Loops are structures that control repeated execution of the same block of statements code block if…else true false loop? code block true false Ins.Ebtesam AL-etowi

Introduction to Loop Loop consist of : Body of the loop : the code which want to do more than one time Control statement (conditions ): A Boolean expression controls the execution of the body of the loop Execute the loop again if true Terminate the loop if false False Two kind of loops: Body of the loop Condition True Finite loop The statement in the body will executed number of times, from zero to finite number Infinite loop A loop that’s continuo forever Ins.Ebtesam AL-etowi

Loop control structure Entry control loop Exit control loop The control conditions are tested before the start of the loop execution. If the conditions are not satisfied , then the body of the loop will not be executed. The Test is performed at the end of the body of the loop and there fore the body is executed unconditionally for the first time. Ins.Ebtesam AL-etowi

The while Loop The syntax for the while loop is as follows: 1 while (loop-continuation-condition) { // Loop body Statement(s); Change; } initialization ; 2 loop body 3 1-Initialization: the variable tested in the condition must be initialize to some value. 2-condition: the condition that tested before any iteration. If it false, then the program will not enter to loop and continue to the next statement after the loop 3-Change: the variable that tested must be change within the body loop. Ins.Ebtesam AL-etowi

The While loop }// end while int Number = 10; while ( Number >= 0 ) { System.out.println( "Number is " + Number ); Number--; }// end while System.out.println( "Loop ended"); Initialize the number to 10 . Test whether "Number >= 0". If it is FALSE, end the loop, and continue with the statements after the loop. In this case, the next statement to be executed would print out the "Loop ended" message. If the result of "Number >= 0" is TRUE, execute the statements in the body of the loop (in this case first print a message, and then decrement the value of "Number" by 1 ), and then return to step (ii) above. Ins.Ebtesam AL-etowi

Total 55 Exercise 1 What is the output of the following code segment? int Counter = 0, total=0; while (Counter<=10) { Total += Counter; Counter++; } // end while System.out.println( "Total " + T); Total 55 Ins.Ebtesam AL-etowi

The do-while Loop The do-while loop is a variation of the while loop. Its syntax is given below: 1-Initialization: the variable tested in the condition must be initialize to some value. 2-Change: the variable that tested must be change within the body loop. 3-Condition: the condition that tested After the first iteration. If it false, then the program will not enter to loop and continue to the next statement after the loop. 1 do { // Loop body; Statement(s); Change; } while (loop-continuation-condition); initialization ; 2 3 Ins.Ebtesam AL-etowi

The do-while Loop int i = 0; do { System.out.println(i); i++; } while ( i < 10 ); System.out.println(“Loop ended”); Initialize the i to 1 . Print the i and increment by 1 Test whether “i<10". If it is FALSE, end the loop, and continue with the statements after the loop. In this case, the next statement to be executed would print out the "Loop ended" message. If the result of “i<10" is TRUE, return to step (ii) above. Ins.Ebtesam AL-etowi

Exercise 2 Exercise 2 What is the output of the following code segment? int Counter = 10, i=2; do{ System.out.println( (i*i) ); Counter++; i++; } while (Counter<=10);// end while System.out.println( "End"); 4 End Ins.Ebtesam AL-etowi

The for Loop In general, the syntax of a for loop is shown as below: for ( initialization; condition ; Change ) { statements; } 2 3 1 1-Initialization: is used to initialize variables used in the loop and is run only once at the start. 2-Condition: The Boolean expression is checked before each iteration. If it is false the loop will terminate. 3-Change: The increment/decrement of the loop control variable and is run after each successful iteration(after executed the body) Ins.Ebtesam AL-etowi

Exercise 3 What is the output of the following code segment? for ( int Count = 0; Count < 10; Count++ ) { System.out.print(Count+" "); } 0 1 2 3 4 5 6 7 8 9 Ins.Ebtesam AL-etowi

Differences between While, Do-while and For for (initialization ; condition; Change) { // loop body statement(s); } While Initialization; while (condition) { // loop body statement(s); Change; } Do-While Initialization; do { // loop body statement(s); Change; } while (condition); Use when number of repetitions is not known Use when loop body must be executed at least once and number of repetitions is not known Use when number of repetitions is known (print 100 times Ins.Ebtesam AL-etowi

Break Statement Or used in for, while, do-while loop, The break statement in Java has two forms: labeled and unlabeled 1- Unlabeled break Normally, we knew unlabeled break used in switch-case statement, to break the following execution in the block. Example: int HOUR = 3; switch(HOUR) { case 1: break; case 2: break; case 3: break; case 4: break; default: break; } Or used in for, while, do-while loop, for example: for(int i=0; i<10; i++) { System.out.print(i + " "); if(i == 3) break; } (Which printed: [0 1 2 3 ]) Ins.Ebtesam AL-etowi

Break statement 2- Labeled break There is another type of break called labeled break. We can use labeled break to break the following execution and transfer the of control back to the labeled statement immediately. Example: int input = 3; validation: { System.out.println("start"); if(input == 4) System.out.println("pass"); if(input < 4) break validation; System.out.println("the input is not equal 4 and not smaller than 4"); if(input > 4) break validation; System.out.println(“end”); } System.out.println("another end"); It should print: start another end The program execution will break from the validation block (declared curly braces) and jump to the end followed by the block. Ins.Ebtesam AL-etowi

Continue Statement Continue Statement : Continue statement is used when we want to skip the rest of the statement in the body of the loop and continue with the next iteration of the loop. There are two forms of continue statement in Java: 1. Unlabeled Continue Statement 2. Labeled Continue Statement 1. Unlabeled Continue Statement This form of statement causes skips the current iteration of innermost for, while or do while loop. Example: for(int var1 =0; var1 < 5 ; var1++)         for(int var2=0 ; var2 < 5 ; var2++)         {                 if(var2 == 2)                         continue;                         System.out.println(“var1:” + var1 + “, var2:”+ var2); } In this example, when var2 becomes2, the rest of the inner for loop body will be skipped. Ins.Ebtesam AL-etowi

Continue Statement Example: Outer: 2. labeled Continue Statement Labeled continue statement skips the current iteration of the loop marked with the specified label. This form is used with nested loops. Example: Outer: for(int var1 =0; var1 < 5 ; var1++) {           for(int var2=0 ; var2 < 5 ; var2++)         {                 if(var2 == 2)                         continue Outer; System.out.println(“var1:” + var1 + “, var2:”+ var2); } } In this example, when var2 becomes 2, rest of the statements in body of inner as well outer for loop will be skipped, and next iteration of the Outer loop will be executed. Ins.Ebtesam AL-etowi

Thank You !