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.

Slides:



Advertisements
Similar presentations
Flow of Control Chapter 3.
Advertisements

Introduction to C Programming
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Creating PHP Pages Chapter 7 PHP Decisions Making.
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Chapter 5: Control Structures II (Repetition)
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
true (any other value but zero) false (zero) expression Statement 2
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.
16/27/ :53 PM6/27/ :53 PM6/27/ :53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +,
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
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.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.
Java Programming, Second Edition Chapter Five Input and Selection.
Visual C# 2005 Decision Structures. Visual C# Objectives Understand decision making Learn how to make decisions using the if statement Learn how.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
Jaeki Song ISQS6337 JAVA Lecture 04 Control Structure - Selection, and Repetition -
Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.
Programming Logic and Design Fifth Edition, Comprehensive
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Loops George Mason University. Loop Structure Loop- A structure that allows repeated execution of a block of statements Loop body- A block of statements;
JavaScript, Fourth Edition
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.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
CS 106 Introduction to Computer Science I 09 / 26 / 2007 Instructor: Michael Eckmann.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
A FIRST BOOK OF C++ CHAPTER 5 REPETITION. OBJECTIVES In this chapter, you will learn about: The while Statement Interactive while Loops The for Statement.
A First Book of C++ Chapter 5 Repetition.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
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.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Programming Logic and Design Fifth Edition, Comprehensive Chapter 4 Making Decisions.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Branching statements.
Lecture 4b Repeating With Loops
Java Programming Fifth Edition
Chapter 4 – C Program Control
Chapter 3: Decisions and Loops
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
JavaScript: Control Statements.
JavaScript: Control Statements I
Control Structures - Repetition
Outline Altering flow of control Boolean expressions
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Control Statements Paritosh Srivastava.
Using C++ Arithmetic Operators and Control Structures
CSC215 Lecture Control Flow.
Presentation transcript:

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 to use compound expressions in if statements How to make decisions using the switch statement How to use the conditional operator

3 Objectives How to use the NOT operator How to loop using the while statement How to loop using the for statement How to loop using the do statement How to use nested loops

4 Making Decisions Using the if Statement The if and if-else statements are the two most commonly used decision-making statements in C# The if statement takes the form if(expression) statement; Expression represents any C# expression that can be evaluated as true or false Statement represents the action that will take place if the expression evaluates as true

5 Making Decisions Using the if Statement You can place any number of statements within the block contained by curly braces, including another if statement (which is called a nested if statement)

6 Making Decisions Using the if-else Statement Some decisions you make are dual-alternative decisions; they have two possible outcomes The if-else statement takes the form if(expression) statement1; else statement2;

7 Making Decisions Using the if-else Statement Dual-alternative if-else statement used in a program

8 Using Compound Expressions In if Statements As an alternative to nested if statements, you can use the conditional AND operator within a Boolean expression to determine whether two expressions are true The two sample codes shown below work the same way

9 Using Compound Expressions In if Statements You are never required to use the AND operator because nested if statements always achieve the same results You must include a complete Boolean expression on each side of the && operator A common mistake is as follows: if(saleAmount >1000 &&<5000) If the first expression is false, the second expression is never evaluated, because its value does not matter

10 Using Compound Expressions In if Statements You can use the conditional OR operator when you want some action to occur even if only one of two conditions is true The OR operator is written as || When the OR operator is used in an if statement, only one of the two Boolean expressions in the if statement needs to be true for the resulting true statement to execute

11 Using Compound Expressions In if Statements You can combine as many AND and OR operators in an expression as you need When you combine AND and OR operators within the same Boolean expression, the AND operators take precedence You can use parentheses to correct the logic and the order of the evaluation of expressions

12 Using Compound Expressions In if Statements What would happened if the parentheses were not used in the above if statement?

13 Using Compound Expressions In if Statements Output of MovieDiscount program before adding parentheses to alter precedence

14 Using Compound Expressions In if Statements Output of MovieDiscount program after adding parentheses to alter precedence

15 Making Decisions Using the switch Statement By nesting a series of if and else statements, you can choose from any number of alternatives An alternative to a series of nested if statements is to use the switch statement

16 Making Decisions Using the switch Statement The switch structure uses four new keywords: –switch starts the structure and is followed immediately by a test expression –case is followed by one of the possible values that might equal the switch expression –break optionally terminates a switch structure at the end of each case –default optionally is used prior to any action that should occur if the test expression does not match any case

17 Making Decisions Using the switch Statement Example switch structure

18 Making Decisions Using the switch Statement Not allowing code to reach the end of a case is known as the “no fall through rule” The governing type of a switch statement is established by the switch expression A switch statement does not need to contain a default case You are never required to use a switch structure because the same results can be achieved with the if statement

19 Making Decisions Using the switch Statement Example switch structure using multiple labels to execute a single statement block

20 Using the Conditional Operator The conditional operator is used as an abbreviated version of the if-else statement It requires three expressions separated with a question mark and a colon testExpression ? trueResult : falseResult

21 Using the NOT Operator The NOT operator is written as an exclamation point (!) Any expression that evaluates as true becomes false when preceded by the NOT operator, and any false expression preceded by the NOT operator becomes true Examples: if(!OverSeventeen) Console.WriteLine(“You can’t see the movie”); else Console.WriteLine(“You can see the movie”);

22 Using the while Loop A loop is a structure that allows repeated execution of a block of statements Within a looping structure, a Boolean expression is evaluated. If it is true, a block of statements called the loop body executes, and then the Boolean expression is evaluated again. As long as the expression is true, the statements in the loop body continue to execute

23 Using the while Loop You can use a while loop to execute a body of statements continuously while some condition continues to be true A while loop consists of the keyword while, followed by a Boolean expression within parentheses, followed by the body of the loop A loop that never ends is called an infinite loop

24 Using the while Loop To make a while loop end, three separate actions should occur: –Some variable, the loop control variable, is initialized –The loop control variable is tested in the while statement –The body of the while statement must take some action that alters the value of the loop control variable

25 Using the while Loop Output of typical execution of LoopingBankBal program A value that a user must supply to stop a loop is called a sentinel value

26 Using the for Loop A loop that executes a specific number of times is a definite loop or a counter loop To write a definite loop, you initialize a loop control variable, and as long as it does not pass a limit, you continue to execute the body of the while loop When you use a for statement, you can indicate the starting value for the loop control variable, the test condition that controls loop entry, and the expression that alters the loop control variable

27 Using the for Loop The three semicolon separated sections of a for statement are used for: –Initializing the loop control variable –Testing the loop control variable –Updating the loop control variable

28 Using the for Loop Printing integers 1 through 10 with while and for loops

29 Using the do Loop To create a loop whose body executes at least once, you use the do loop. The do loop checks the bottom of the loop after one repetition has occurred. You can place loops within other loops

30 Chapter Summary You use an if statement to make a single-alternative decision When you make a dual-alternative decision, you can use an if-else statement You can use the conditional AND operator within a Boolean expression to determine whether two expressions are both true You can use the conditional OR operator when you want some action to occur when one or both of two conditions are true

31 Chapter Summary When you compare AND and OR operators within the same Boolean expression without parentheses, the AND operators takes precedence The switch statement tests a single variable against a series of exact matches The conditional operator is used as an abbreviated version of the if-else statement You use the NOT operator to negate the result of any Boolean expression

32 Chapter Summary You can use a while loop to execute a body of statements continuously while some condition continues to be true When you use a for statement, you can indicate the starting value of the loop control variable, the test condition that controls loop entry, and the expression that alters the loop control variable The do loop checks the bottom of the loop after one repetition has occurred You can nest any combination of loops to achieve desired results