Control Structures: Part 1

Slides:



Advertisements
Similar presentations
Decisions If statements in C.
Advertisements

 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.
 2002 Prentice Hall. All rights reserved. 1 Chapter 4: Control Structures: Part 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures.
Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Control Structure.
Control Structures: Getting Started Sequence and Selection also arithmetic operators, data types, logical operators.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Control Structures: Part 1. Introduction Control Structures If / Then Selection Structure If / Then / Else Selection Structure While Repetition Structure.
1 Pertemuan 06 Repetition Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:
Structured Program Development in C
 2002 Prentice Hall. All rights reserved. 1 Chapter 3 – Control Structures Outline 3.1 Introduction 3.2 Algorithms 3.3 Pseudocode 3.4Control Structures.
The University of Texas – Pan American
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
ENGR 112 Decision Structures.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
 2002 Prentice Hall. All rights reserved. 1 Chapter 4: Control Structures: Part 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures.
Statements That Repeat. For...Next Loop Structure For counter = start To end Step increment statements Next counter Where Counter is tested to see if.
4.1 Introduction Structured programming –Control structures Helpful in building and manipulating objects BZUPAGES.COM.
Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - JavaScript: Control Structures I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
IE 411/511: Visual Programming for Industrial Applications Lecture Notes #4 Control Statements – Part 1.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Introduction to Problem Solving and Control Statements.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
 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.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Lecture 5: Layers of Control. Nested while Loops Problem Multiplying two numbers and outputting the result only if they are both less than 5. (i.e. Start.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (while) Outline 3.7The While Repetition.
Chapter 14 - JavaScript/JScript: Control Structures I
Chapter 4 – C Program Control
UNIT 5 Lesson 15 Looping.
Control Statements: Part 1
Chapter 5- Control Structures: Part 2
Visual Basic 6 (VB6) Data Types, And Operators
Control Statements: Part 1
Data Types 1.
Problem Solving and Control Statements: Part 2
JavaScript: Control Statements I
Control Structures: Part 2
Chapter 8 - JavaScript: Control Statements I
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.
Ch 7: JavaScript Control Statements I.
JavaScript: Control Statements.
JavaScript: Control Statements I
Outline Altering flow of control Boolean expressions
Chapter 3: Introduction to Problem Solving and Control Statements
MSIS 655 Advanced Business Applications Programming
Structured Program
Chapter 3 - Structured Program Development
3 Control Statements:.
Chapter 3 – Control Structures
Chapter 3 - Structured Program Development
Introduction to Problem Solving and Control Statements
Problem Solving and Control Statements
Chapter 4 - Control Structures: Part 1
EPSII 59:006 Spring 2004.
ASP control structure BRANCHING STATEMENTS
Structural Program Development: If, If-Else
Presentation transcript:

Control Structures: Part 1

Outline Introduction Control Structures If/Then Selection Structure If/Then/Else Selection Structure While Repetition Structure Do While/Loop Repetition Structure Do Until/Loop Repetition Structure Assignment Operators Formulating Algorithms: Case Study 1 (Counter-Controlled Repetition) Formulating Algorithms with Top-Down, Stepwise Refinement: Case Study 2 (Sentinel-Controlled Repetition) Formulating Algorithms with Top-Down, Stepwise Refinement: Case Study 3 (Nested Control Structures) Formulating Algorithms with Top-Down, Stepwise Refinement: Case Study 4 (Nested Repetition Structures)

Control Structures Selection Structures If/Then If/Then/Else Single-selection structure If/Then/Else Double-selection structure Select Case Multiple-selection structure

Control Structures Repetition Structures While Loop Do Loops: Do While/Loop Do/Loop While Do Until/Loop Do/Loop Until For Loops: For/Next For Each/Next

If/Then Selection Structure It is a single-entry/single-exit structure Example If studentGrade >= 60 Then MsgBox(“Passed”) End If

If/Then Selection Structure The preceding If/Then selection structure also could be written on a single line as If studentGrade >= 60 Then MsgBox("Passed") In the multiple-line format, all statements in the body of the If/Then are executed if the condition is true. In the single-line format, only the statement after the Then keyword is executed if the condition is true. Writing the closing End If keywords after a single-line If/Then structure is a syntax error.

If/Then/Else Selection Structure The If/Then/Else selection structure allows the programmer to specify that a different action (or sequence of actions) be performed when the condition is true than when the condition is false.

If/Then/Else Selection Structure Example If studentGrade >= 60 Then MsgBox(“Passed”) Else MsgBox(“Failed”) End If

If/Then/Else Selection Structure The VB Editor tries to help you write If statements. When in the code view window, if you type the keyword If and press Enter, the VB editor automatically adds the keywords Then and End If. You can add the Else branch as necessary. The VB editor will try to correct errors. If you type EndIf with no space, the editor will correct this and add the required space. If you type Else with a coding statement on the line, the editor will add a colon between Else and the coding statement – the colon is a statement separator.

If/Then/Else Selection Structure Illegal Syntax If HoursDecimal <= 40D Then MsgBox(“ Equals to 40”) Else MsgBox(“ Not Equals to 40”) End If VB Editor Correction with Colon Else : MsgBox(“ Not Equals to 40”) Preferred Syntax MsgBox(“Equals to 40”) Else MsgBox(“ Not Equals to 40”) If you type Else with a coding statement on the line, the editor will add a colon between Else and the coding statement

Nested If/Then/Else Nested If/Then/Else structures test for multiple conditions by placing If/Then/Else structures inside other If/Then/Else structures. Most Visual Basic programmers prefer to write the nested If/Then/Else structure using the ElseIf keyword.

Nested If/Then/Else For example, the following code will print “A” for exam grades greater than or equal to 90, “B” for grades in the range 80–89, “C” for grades in the range 70–79, “D” for grades in the range 60–69 and “F” for all other grades. If grade >= 90 Then MsgBox(“A”) ElseIf grade >= 80 Then MsgBox(“B”) ElseIf grade >= 70 Then MsgBox(“C”) ElseIf grade >= 60 Then MsgBox(“D”) Else MsgBox(“F”) End IF

While Repetition Structure Allows the programmer to specify that an action should be repeated, depending on the value of a condition

Write a program that finds the first power of two larger than 1000? Failure to provide the body of the structure with an action that causes the condition to become false creates an infinite loop

Do While/Loop Repetition Structure This structure behaves like the While repetition structure

DoWhile.vb Program Output Write a program to find the first power of two larger than 1000? DoWhile.vb Program Output Failure to provide the body of the structure with an action that causes the condition to become false creates an infinite loop

Do Until/Loop Repetition Structure Unlike the While and Do While/Loop repetition structures, the Do Until/Loop repetition structure tests a condition for falsity for repetition to continue. Statements in the body of a Do Until/Loop are executed repeatedly as long as the loop-continuation test evaluates to false.

DoUntil.vb Program Output once again consider a program designed to find the first power of two larger than 1000. DoUntil.vb Program Output The loop ends when the condition becomes true

Assignment Operators Fig. 4.11 Assignment operators.

Assignment.vb Program Output Calculates a power of two using the exponentiation assignment operator. Assignment.vb Program Output

Assignment Operators Although the symbols =, +=, -=, *=, /=, \=, ^= and &= are operators, we do not include them in operator-precedence tables. When an assignment statement is evaluated, the expression to the right of the operator is always evaluated first, then assigned to the variable on the left. Unlike Visual Basic’s other operators, the assignment operators can only occur once in a statement.