Problem Solving and Control Statements

Slides:



Advertisements
Similar presentations
Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
Advertisements

Control Structures: Getting Started Sequence and Selection also arithmetic operators, data types, logical operators.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Chapter 7 – Control Structures A payroll company calculates the gross earnings per week of employees. Employees’ weekly salaries are based on the number.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Dale Roberts Program Control using Java - Boolean Expressions Dale Roberts, Lecturer Computer Science, IUPUI Department of.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 8 Dental Payment Application Introducing CheckBox es and Message Dialogs.
 2009 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Introduction to Problem Solving and Control Statements.
IE 411/511: Visual Programming for Industrial Applications Lecture Notes #5 Control Statements: Part 2.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
Lab 6 (1) Range Review, Control Logic and Loops ► Control Logic and Loops ► Exercise.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2B) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
Chapter 4 Select … Case Multiple-Selection Statement & Logical Operators 1 © by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon.
 2007 Pearson Education, Inc. All rights reserved Control Statements: Part2.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais1.
Problem Solving and Control Statements: Part 2
Introduction to Computer Programming
Chapter 4 Control Statements: Part 2
Chapter 4 – C Program Control
Chapter 5- Control Structures: Part 2
Visual Basic 6 (VB6) Data Types, And Operators
Chapter 4 C Program Control Part II
Multiple selections Logical Operators Precedences
Problem Solving and Control Statements: Part 2
Control Statements: Part 2
Control Structures: Part 2
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2B) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
JavaScript: Control Statements.
JavaScript: Control Statements I
Chapter 5 – Control Structures: Part 2
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 5- part 2 Control Statements: Loops 2
Control Structures: Part 1
MSIS 655 Advanced Business Applications Programming
Chapter 8 JavaScript: Control Statements, Part 2
Chapter 3: Introduction to Problem Solving and Control Statements
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises UTPA – Fall 2012 This set of slides is revised from.
Chapter 5 Control Statements: Loops 2
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 6 Control Statements: Part 2
Introduction to Problem Solving and Control Statements
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Case & Repetitive Statements
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Using C++ Arithmetic Operators and Control Structures
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Lecture 9: Implementing Complex Logic
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Control Statements:.
Presentation transcript:

Problem Solving and Control Statements

For…Next Repetition Statement Counter-controlled repetition requires: the name of a control variable (or loop counter) that’s used to determine whether the loop continues to iterate the initial value of the control variable the increment (or decrement) by which the control variable is modified each time through the loop the condition that tests for the final value of the control variable (that is, whether looping should continue).

Examples Using the For…Next Statement The following examples demonstrate different ways of varying the control variable in a For…Next statement. In each case, we write the appropriate For…Next header using local type inference. Vary the control variable from 1 to 100 in increments of 1. For i = 1 To 100 or For i = 1 To 100 Step 1 Vary the control variable from 100 to 1 in decrements of 1. For i = 100 To 1 Step -1

Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate different types of repetition statements. When the Exit Do statement executes in a Do While…Loop, Do…Loop While, Do Until…Loop or Do…Loop Until statement, the program terminates that repetition statement and continues execution with the first statement after the repetition statement. Similarly, the Exit For statement and the Exit While statement cause immediate exit from For…Next and While…End While loops, respectively. The Exit Select statement causes immediate exit from a Select…Case statement.

For Counter As Integer = 0 To 5 ‘ Exit the loop if Counter is 3 If Counter = 3 Then Exit For End If MessageBox.Show(“Current Counter = “ & Counter.ToString) Next Counter

Using Continue in Repetition Statements A Continue statement terminates only the current iteration of a repetition statement and continues execution with the next iteration of the loop. The Continue Do statement can be executed in a Do While…Loop, Do…Loop While, Do Until…Loop or Do…Loop Until statement. Similarly, the Continue For statement and Continue While statement can be used in For…Next and While…End While statements, respectively.

For Counter As Integer = 0 To 5 ‘ Exit the loop if Counter is 3 If Counter = 3 Then Continue For End If MessageBox.Show(“Current Counter = “ & Counter.ToString) Next Counter

Select…Case Multiple-Selection Statement Occasionally, an algorithm contains a series of decisions that test a variable or expression separately for each value that the variable or expression might assume. The algorithm takes different actions based on those values. The Select…Case multiple-selection statement handles such decision making.

Select…Case Multiple-Selection Statement Types of Case Statements Case statements also can use relational operators to determine whether the controlling expression satisfies a condition. For example Case Is < 0 uses keyword Is along with the relational operator, <, to test for values less than 0. Multiple values can be tested in a Case statement by separating the values with commas, as in Case 0, 5 To 9 which tests for the value 0 or values in the range 5–9. Also, Cases can be used to test String values.

Select…Case Multiple-Selection Statement If GradeString = “A” Then MessageBox.Show(“Super”) ElseIf Gradestring = “B” Then MessageBox.Show(“Good”) ElseIf GradeString = “C” Then MessageBox.Show(“Average”) ElseIf GradeString = “D” MessageBox.Show(“Better luck next time”) ElseIf GradeString = “F” Then End If Select Case GradeString Case “A” MessageBox.Show(“Super”) Case “B” MessageBox.Show(“Good”) Case “C” MessageBox.Show(“Average”) Case “D”, “F” MessageBox.Show(“Better luck next time”) End Select

Select…Case Multiple-Selection Statement If GradeInteger = 100 Then MessageBox.Show(“Super”) ElseIf GradeInteger >=90 And GradeInteger <=99 Then MessageBox.Show(“Very good”) ElseIf GradeInteger >= 80 And GradeInteger <=89 Then MessageBox.Show(“Good”) ElseIf GradeInteger >=70 And GradeInteger <=79 Then MessageBox.Show(“Average”) ElseIf GradeInteger >=60 And GradeInteger <=69 Then MessageBox.Show(“Poor”) Else MessageBox.Show(“Better luck next time”) End If Select Case GradeInteger Case 100 MessageBox.Show(“Super”) Case 90 To 99 MessageBox.Show(“Very good”) Case 80 To 89 MessageBox.Show(“Good”) Case 70 To 79 MessageBox.Show(“Average”) Case 60 To 69 MessageBox.Show(“Poor”) Case Else MessageBox.Show(“Better luck next time”) End Select

Logical Operators To make a decision that relied on the evaluation of multiple conditions, we performed these tests in separate statements or in nested If…Then or If…Then…Else statements. To handle multiple conditions more efficiently, the logical operators can be used to form complex conditions by combining simple ones. Logical operators are And, Or, AndAlso, OrElse, Xor and Not.

Logical Operator - And If expression1 And expression2 Then

Logical Operators Logical And Operator Suppose we wish to ensure that two conditions are both True in a program before a certain path of execution is chosen. In such a case, we can use the logical And operator as follows: If gender = "F" And age >= 65 Then seniorFemales += 1 End If This If…Then statement contains two simple conditions. The readability can be improved by adding redundant parentheses: (gender = "F") And (age >= 65)

Logical Operators - Or If expression1 Or expression2 Then

Logical Operators Logical Or Operator (Also Called the Logical Inclusive Or Operator) Now let’s consider the Or operator. Suppose we wish to ensure that either or both of two conditions are True before we choose a certain path of execution. We use the Or operator as in the following program segment: If (semesterAverage >= 90 Or finalExam >= 90) Then resultLabel.Text = "Student grade is A" End If This statement also contains two simple conditions.

Logical Operators Logical AndAlso and OrElse Operators The logical AND operator with short-circuit evaluation (AndAlso) and the logical inclusive OR operator with short-circuit evaluation (OrElse) are similar to the And and Or operators, respectively, with one exception—an expression containing AndAlso or OrElse operators is evaluated only until its truth or falsity is known.

Logical Operators – AndAlso/OrElse expression 1 expression 2 expression 1 AndAlso expression 2 True False Not evaluated expression 1 expression 2 expression 1 OrElse expression 2 True Not evaluated False

Logical Operators For example, the expression (gender = "F" AndAlso age >= 65) stops evaluating immediately if gender is not equal to "F" (that is, the entire expression is False); the second expression is irrelevant because the first condition is False. Evaluation of the second condition occurs if and only if gender is equal to "F" (that is, the entire expression could still be True if the condition age >= 65 is True). This performance feature for the evaluation of AndAlso and OrElse expressions is called short-circuit evaluation.

Logical Operators – Xor Xor – Exclusive Or Used in Cryptography, Parity checks expression 1 expression 2 expression 1 Xor expression 2 True False

Logical Operator - Not If Not expression Then

Logical Operators Logical Not Operator The Not (logical negation) operator enables you to “reverse” the meaning of a condition. Unlike the logical operators And, AndAlso, Or, OrElse and Xor, which each combine two conditions, the logical negation operator is a unary operator, requiring only one operand. The logical negation operator is placed before a condition to choose a path of execution if the original condition (without the logical negation operator) is False.

Logical Operators The logical negation operator is demonstrated by the following program segment: If Not (value = 0) Then resultLabel.Text = "The value is " & value End If The parentheses around the condition value = 0 are necessary because the logical negation operator (Not) has a higher precedence than the equality operator.

App: Dental Payment Calculator A dentist’s office administrator wishes to create an app that employees can use to bill patients. The app must allow users to enter the patient’s name and specify which services were performed during the visit. The app will then calculate the total charges. If a user attempts to calculate a bill before any services are specified, or before the patient’s name is entered, an error message will be displayed informing the user that necessary input is missing.