Control Flow Statements: Repetition/Looping

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

Selection Feature of C: Decision making statements: It allow us to take decisions as to which code is to be executed next. Since these statements control.
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Control Structures.
Chapter 4: Control Structures I (Selection)
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 5 Selection Statements.
3-1 Chapter 3 Flow of Control (part a - branching)
The if-else Statements
Pemrograman Dasar Control Flow Statements: Decision Making or Selection PTIIK - UB 1.
Computer Programming 12 Mr. Jean April 2 nd, 2014.
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Lecture 3: Control Structures - Selection BJ Furman 10SEP2012.
The "if structure" is used to execute statement(s) only if the given condition is satisfied.
Programming In C++ Spring Semester 2013 Lecture 4 Programming In C++, Lecture 3 By Umer Rana.
Control Structure There are two kind of control structure in GWBASIC one is iteration/loop or repetitive and second make decision/condition. Iteration/Loop.
3. S/E with Control Structures 3.1 Relational Operators and Expressions 3.2 If and if-else Statements 3.3 The Type Double 3.4 Program Design with the While.
Overview Program flow Decision / branching / selection structures True & False in Python Comparison operators & Boolean expressions if … if … else if …
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
1 2. Program Construction in Java. 2.4 Selection (decisions)
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
4.1 Object Operations operators Dot (. ) and new operate on objects The assignment operator ( = ) Arithmetic operators + - * / % Unary operators.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1.
Control Flow Statements
CSI 3125, Preliminaries, page 1 Control Statements.
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Chapter 3. Control Structure C++ Programing. Conditional structure C++ programming language provides following types of decision making statements. Click.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Control Structure  What is control Structure?  Types of Controls  Use the control structure in VBScript.  Example Summery.
C syntax (simplified) BNF. Program ::= [ ] Directives ::= [ ] ::= | |… ::=#include > ::=#define.
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.
Silberschatz and Galvin  C Programming Language Decision making in C Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
Control Flow (Python) Dr. José M. Reyes Álamo.
Chapter 3: Decisions and Loops
Unit-1 Introduction to Java
Loops in Java.
CiS 260: App Dev I Chapter 4: Control Structures II.
Expressions and Control Flow in JavaScript
Control Structures.
Loop Control Structure.
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13]
Alice in Action with Java
IF if (condition) { Process… }
CSE 1020:Control Structure
Repetition Control Structure
Control structures Chapter 3.
Computer Science Core Concepts
Control structures Chapter 3.
Conditionals.
Program Flow.
Control structures Chapter 3.
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
SEQUENCE Start Initial situation Step 1 Step 2 Step 3 End
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
Presentation transcript:

Control Flow Statements: Repetition/Looping Pemrograman Dasar Control Flow Statements: Repetition/Looping PTIIK - UB

Statements Control flow statements regulate the order in which statements get executed. Kinds of control flow statements: Decision making: if-then statement if-then-else statement, switch-case statement Repetition/looping: while statement for statement do-while statement Branching statement: break statement continue statement return statement

Repetition/Looping Three forms of repetition statements: while statements for statements do-while statements

while statements Syntax: while (boolean_exp) statement; or .. } statements true boolean_exp false

while statements Example: while(product <= 1000) product = 2*product; product=2*product; product <= 1000 true false

break dan continue break continue Exit from decision-making (switch) or repetition (for, while dan do-while) continue Skips the current iteration of a for, while and do-while.

break Example: int x = 1; while(x<=10){ System.out.println(x); x++; if (x>5) break; } Exit from the loop

continue Example: int x; for(x=1; x<=10; x++) { if (x == 5) System.out.println(x); }

Exercise

Exercise Input: Asumsi Maka output-nya: Berapakah kue untuk si Rakus? … Asumsi Misalkan jawaban pertanyaan di atas adalah 5 Maka output-nya: Si Rakus melihat 5 kue, memakan 1, menyisakan 4 Si Rakus melihat 4 kue, memakan 1, menyisakan 3 Si Rakus melihat 3 kue, memakan 1, menyisakan 2 Si Rakus melihat 2 kue, memakan 1, menyisakan 1 Si Rakus melihat 1 kue, memakan 1, menyisakan piringnya

Submission Deadline: 21 October 2012 To: ismiarta@gmail.com Subject: [PROGDAS SI…] Si Rakus Content: Nama: NIM: Kelas: Table Source code

for statements You use the basic for statement to loop over a range of values from beginning to end. for (initialization-expression; loop-expression; update-expression) statement; //or for (initialization-expression; loop-expression; update-expression) { … }

for statements for (exp1; exp2; exp3) statement; exp1: initialization-expression exp2: loop-expression exp3: update-expression The initialization-expression allows you to declare and/or initialize loop variables, and is executed only once. Then the loop-expression of boolean or Boolean type is evaluated and if it is true the statement in the body of the loop is executed. After executing the loop body, the update-expression is evaluated, usually to update the values of the loop variables, and then the loop-expression is reevaluated. This cycle repeats until the loop-expression is found to be false. The presence of exp1, exp2, and exp3 are optional.

for statements true false exp1 exp3 statements exp2 exp1 exp3

System.out.println(“x =” + x); for statements Example: for (x=1; x <= 10; x++) System.out.println(“x =” + x); x = 1 x++ System.out.println(“x =” + x); true x<=100 false

for statements Examples: int x; for(x=1; x<=10; x++) System.out.println(x); int x; for(x=10; x>=1; x--) System.out.println(x);

for statements Examples: exp1 and exp3 may consist of more than one expressions, separated by comma(s) Examples: int i,j; for(i=1, j=30; i<j; i++, j--){ System.out.println(i + " -- " + j); }

for statements Infinite Loop for(;;){ … } Contoh: int i = 0; for(;;){ printf("%d ", i); if (i>=10) break; // break out of the loop }

while and for Equivalence of for and while: exp1; while(exp2){ statement1; statement2; …. exp3 } for(exp1; exp2; exp3){ statement1; statement2; …. } equivalent

while and for Equivalence of for and while: Contoh: exp1; while(exp2){ statement1; statement2; …. exp3 } for(exp1; exp2; exp3){ statement1; statement2; …. } equivalent int x=1; while(x<=10){ System.out.println(x); x++; } int x; for(x=1;x<=10;x++) System.out.println(x); equivalent

Exercise Input: Asumsi Maka output-nya: Berapakah kue untuk si Rakus? … Asumsi Misalkan jawaban pertanyaan di atas adalah 5 Maka output-nya: Si Rakus melihat 5 kue, memakan 1, menyisakan 4 Si Rakus melihat 4 kue, memakan 1, menyisakan 3 Si Rakus melihat 3 kue, memakan 1, menyisakan 2 Si Rakus melihat 2 kue, memakan 1, menyisakan 1 Si Rakus melihat 1 kue, memakan 1, menyisakan piringnya