Chapter 3: Decisions and Loops

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

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming First Edition.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Chapter 3: Program Statements
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
CONTROLLING PROGRAM FLOW
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Bordoloi and Bock Control Structures: Iterative Control.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-1 Lecture Objectives To understand: –what values can be stored in a Boolean.
Repetition Statements b Repetition statements allow us to execute a statement multiple times repetitively b They are often simply referred to as loops.
Flow of Control (2) : Loops Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
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.
Chapter 6: Loops.
Chapter 4 Repetition Statements (loops)
The switch Statement, and Introduction to Looping
Programming Logic and Design Fourth Edition, Comprehensive
Topics The if Statement The if-else Statement Comparing Strings
CiS 260: App Dev I Chapter 4: Control Structures II.
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
JavaScript: Control Statements I
MSIS 655 Advanced Business Applications Programming
Topics The if Statement The if-else Statement Comparing Strings
Alice in Action with Java
IF if (condition) { Process… }
Outline Altering flow of control Boolean expressions
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
CSC215 Lecture Flow Control.
Chapter 3: Program Statements
CSC215 Lecture Control Flow.
Chapter 8: More on the Repetition Structure
3.5- The while Statement The while statement has the following syntax:
Three Special Structures – Case, Do While, and Do Until
ICT Programming Lesson 3:
Alternate Version of STARTING OUT WITH C++ 4th Edition
Chapter8: Statement-Level Control Structures April 9, 2019
Chap 7. Advanced Control Statements in Java
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
CSC215 Lecture Control Flow.
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
Presentation transcript:

Chapter 3: Decisions and Loops Programming with Alice and Java First Edition by John Lewis and Peter DePasquale

Objectives Make decisions using an If/Else statement. Base decisions on conditions that use equality, relational operators, and Boolean functions. Use logical operators to create complex conditions. Nest If/Else statements and loops. Execute a set of statements repeatedly using While and Loop statements. Use a loop control variable to tailor loop processing.

The If/Else Statement Control statements allow you to control the flow of a program’s logic. Control statements are based on the result of a condition. The condition produces a Boolean (true or false) value, which determines which statements are executed next. An If/Else statement determines which of two sets of statements are executed. The default structure of an If/Else statement

Using the If/Else Statement The choose true condition of the If/Else statement returns a true or false result determined randomly

Using the If/Else Statement (continued) There can be as many statements as needed in either section of If/Else

The Equality and Relational Operators The following six equality and relational operators can be used to compare numeric data.

Nested If/Else Statements A statement inside an If/Else statement could itself be an If/Else statement – nested if statement. This If/Else is nested within Else section

Repetition – The While Statement Repetition statements allow the programmer to repeat one or more statements a number of times. Another term for repetition statement is loop. There are two repetition statements in Alice: the While statement; the Loop statement. A While statement is based on a Boolean condition, and repeatedly executes the statement it contains (body of the loop) as long as its condition remains true.

More about the While Statement The default form of a While statement The condition of a While loop is used to decide if its statements should be executed yet again, and may be evaluated many times. The condition in a If/Else statement is evaluated once. An infinite loop is a loop whose condition never becomes false, and this loop never ends.

Using the While Statement This is an example of infinite loop that contains the statements that cause the shark to swim in a circle The While loop body

Using the While Statement (continued) The While loop body A logical operator

The Logical Operators Operator Truth Table Description not a a true false not a false true True if a is false, and false if a is true both a and b a true true false false b true false true false a and b true false false false True if a and b are both true, and false otherwise either a or b, or both a or b true true true false True if a or b or both are true, and false otherwise

The Loop Statement The Loop statement allows you to control the exact number of repetitions. After dragging a new Loop statement into a method, the number of repetitions needs to be specified. The Loop statement uses a condition that tests the value of an integer variable—the loop control variable (index)— and terminates when this value reaches a specified end value.

Using the Loop Statement

Nested Repetition Statements Repetition statements can be nested – the body of a loop can contain another loop. Each iteration of the outer loop causes complete execution of the inner loop See next slide

Nested Repetition Example Outer loop Inner loop

Summary Statements that control the flow of a program are based on Boolean conditions (they are either true or false). An If/Else statement determines which of two sets of statements are executed. The Else portion of an If/Else statement can be left empty. Equality and relational operators produce Boolean results. An If/Else statement can be part of another If/Else statement. A While loop executes its statements until its condition becomes false. An infinite loop is a loop whose condition never becomes false. Logical operators allow you to construct complex conditions for decisions and loops. A Loop statement executes the loop body a specific number of times. The alternate version of the Loop statement provides explicit access to the loop control variable When loops are nested, the inner loop executes all of its iterations for each iteration of the outer loop.