Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

Size: px
Start display at page:

Download "Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur"

Transcription

1 Control Structures Code can be purely arithmetic assignments At some point we will need some kind of control or decision making process to occur C uses the if keyword as part of it s control structure Use else for opposite condition Roberto Muscedere 1 Any process, whether it be a program or a production line, or even a thought, there is always a need to evaluate some type of condition. The CPUs of today operate very efficiently when working with pure arithmetic. Unfortunately, conditions in programs reduce the efficiency of the CPUs considerably. Although it may not seem like it, a lot of work goes into designing CPUs so that they can predict what a condition will be before it reaches it. This is known as branch prediction. When it incorrectly predicts the branch, processing is essentially stopped for several operational cycles. In order to achieve efficient coding, it is best to reduce the number of conditions while programming. This usually happens during the planning stages of algorithms. The more conditional statements, the more potential errors your code may have if they are not fully tested, and this can take a lot of time. This is not to say that it is bad to use a conditional statement, but you should try to avoid using an excessive amount of them Loops 1

2 Program Control with IF IF executes code if the expression in the brackets evaluates to anything other than 0, or false Generally use a BLOCK for more than 1 line of conditional code int i = -10, j = 20; /* Set the absolute value of i and j */ if ( i<0 ) i = -i; if ( j<0 ) j = -j; Roberto Muscedere 2 The relational operator i<0 is really a function. When it is true, it returns a 1, where as if it is false, it returns a Loops 2

3 Program Control with IF and ELSE ELSE code is executed when IF codes does not In this example, second IF is part of the first ELSE int i = -10, j; /* Find the sign of i and store in j */ if ( i<0 ) j = -1; else if ( i>0 ) j = 1; else j = 0; Roberto Muscedere 3 With every IF there can only be one ELSE, that is if you choose to use it. The term ELSE IF does not really exist in C, but compound IF s do. In this example, there are two IF-ELSE statements, but since there are no braces, it may appear that somehow the statements are connected Loops 3

4 Program Control with IF and ELSE Rewritten in order to examine the connection of the BLOCKS We can remove the braces since each conditional code consists of only one statement if ( i<0 ) j = -1; else if ( i>0 ) j = 1; else j = 0; Roberto Muscedere 4 Here we place the braces in so that you can see how the statements relate. The second IF is the execution block of the first ELSE. Some programming languages have an ELSIF of ELSE-IF statement. C does not do this, but because we don t require the need for braces for single line code blocks, we can effectively make it operate as if it did have a merged command Loops 4

5 Relational and Logical Operators Operator Name Example > Greater than x > y >= Greater than or equal to x >= y < Lesser than x < y <= Lesser than or equal to x <= y == Equal to x == y!= Not equal to x!= y! Logical NOT!x && Logical AND x && y Logical OR x y Roberto Muscedere 5 Whenever a relational or logical operation is performed, either a 1 or a 0 is returned. 1 representing true, and 0 representing false. Relational operators are really like functions. For example, (5 > 2) will always return 1 or true. Where as (1==5) will always return 0 or false. For most comparisons, we will use variables. The concept remains the same Loops 5

6 Most Common Error with IF To test equality, we must use == int i = 10; If we use = the compiler will think it is an assignment, and it will evaluate true and the BLOCK will be executed /* Relational expression is an assignment, and evaluates true */ /* should be i==9 */ if ( i=9 ) /* this code executes */ /* i is 9 */ Roberto Muscedere 6 As mentioned before, assignments are functions. In this example, i=9 returns 9, so the if is considered true because the function returned a non-zero value. If we used i=0, the if would be consider false and the code wouldn t execute, but it is still an error because i is being set to 0 unintentionally Loops 6

7 Using Relational Operators in Assignments Use relational operators in assignments int i = 10, j; In this example, we set a condition on j based on i prior to the IF statement j = ( i==10 ); i *= 100; if (j) /* execute this code */ /* i is not 10, but j is true */ Roberto Muscedere 7 Since relational operators are just functions, we can use them in our variable assignments. Here we determine what the condition will be before actually executing any code. We can then modify the original condition element, i, and later process the code based on the original condition Loops 7

8 Conditional Expression Use conditional expression for in line IF-ELSE Usage: (Condition)? True : False; True & False can be any statement; must be set int i = -10, j = 20; /* Set the absolute value of i and j */ i = ( i<0 )? -i : i; ( j<0 )? j = -j : 0; Can be used anywhere Roberto Muscedere 8 Conditional Expressions can also be nested. This is one reason they are used. Here is an example of the sign() function (shown before) using only conditional expressions: j = ( i > 0 )? 1 : ( i < 0 )? -1 : 0; The first condition checks if i is greater than 1. If true, it returns 1. Else if check if i is less than 1. If true, it returns -1. Else, it returns Loops 8

9 Program Control with SWITCH SWITCH is similar to IF No relational operators Only one value For structured comparison For example, user inputs Can reuse some parts of code int i = 2; switch( i ) case 1: /* 1 */ break; case 2: /* 2, follow to 3 */ case 3: /* 3 */ break; default: /* neither */ Roberto Muscedere 9 The SWITCH command is mostly used in situations where you are comparing to a specific value. For example, a menu where the user is asked to select an option or number. Each case would then perform the specific operation. The BREAK command is used to leave the SWITCH command. In the example provided above, the case where i=2, the code for 2 will be executed and the it will follow through to the code for i=3. This happens because the SWITCH was not terminated by a BREAK. The last case, DEFAULT is for any situation where the condition of the SWITCH was not met. You will generally not see BREAK commands on the last case because these is nothing to follow through to. SWITCH exists for optimization reasons. Consider, for example, having to compare a variable with up to 200 numbers. Using IF s and ELSE s will cause the computer to check the variable with each number in series just as the code was written (e.i. IF-ELSE-IF-ELSE- IF-ELSE ). SWITCH instead tries to test the numbers in parallel therefore generating more efficient code Loops 9

10 Loop Structures At some point we will want to perform repetitive tasks (without typing a lot) C uses the for, do, and while keywords as part of it s looping structure These methods operate very much the same Roberto Muscedere 10 For, Do, and While loops all operate essentially the same. The only real difference is when the looping condition is evaluated Loops 10

11 Loop Control with FOR FOR requires 4 parameters 3 in brackets Separated by ; 4 th is the code to execute for each cycle of the loop Use BLOCK for multiple lines Optionally no BLOCK for a single line int i; /* Count from 0 to 9 */ for( i=0 ; i<10 ; i++ ) /* print i here */ Roberto Muscedere Loops 11

12 Loop Control with FOR 1 st parameter Initialisation Can be multiple separated with, 2 nd parameter Condition Relational expression which determines if the code should be executed (if condition is true) int i; /* Count from 0 to 9 */ for( i=0 ; i<10 ; i++ ) /* print i here */ Roberto Muscedere Loops 12

13 Loop Control with FOR 3 rd parameter Increment Can be multiple separated with, Executed after code to increment or change variables Code Order: Execute 1 st parameter Evaluates 2 nd parameter Execute Code Execute 3 rd parameter int i; /* Count from 0 to 9 */ for( i=0 ; i<10 ; i++ ) /* print i here */ Roberto Muscedere 13 Remember the code order: 1. Execute 1 st parameters 2. Evaluate 2 nd parameter 3. If false, stop for loop 4. Execute Code 5. Execute 3 rd parameters 6. Go to step Loops 13

14 Loop Control with FOR Example with two variables i and j are initialised in FOR expression i and j are changed in FOR expression int i, j; /* Count i from 0-9 */ /* Count j from 9 0 */ for( i=0, j=9 ; i<10 ; i++, j-- ) /* print i and j here */ Roberto Muscedere 14 The output of this program would be: Loops 14

15 Loop Control with FOR Either or all parameters can be omitted Don t have to initialise Can initialise outside the FOR structure Don t have to increment Can increment inside the code block Don t need a condition Assumed TRUE if blank int i, j; /* Count i from 0-9 */ i = 0; for( ;i<10; ) /* print i here */ i++; for( ;; ) /* loop forever */ Roberto Muscedere 15 You will probably never use these types of FOR loops, but it is good to be aware of their syntax when you are debugging/reading someone else s code. This is typically used when an internal condition is being evaluated to control the loop, or perhaps a link to the operating system where it would terminate your code Loops 15

16 Loop Control with FOR Can leave a LOOP with the BREAK command int i = 0, j = 9; Can skip the rest of the code block with the CONTINUE command /* Print i from 0-9 */ /* Print j only when i is greater than 5 */ for( ;; ) /* print i here */ i++; j--; if ( i>=10 ) break; if ( i<=6 ) continue; /* print j here */ Roberto Muscedere 16 Sometimes it is difficult to incorporate all of the conditions which drive a loop into a single statement. In these situations, we can use BREAK which will get us out of a loop immediately. In the event that we need to restart the loop, we have CONTINUE. It simply jumps back to the start of the loop. Any conditions are evaluated at this time, so the loop may in fact end if the terminating conditions are met. It is similar to a GOTO just like BREAK, but no line numbers are used. Some programmers don t like using BREAK or CONTINUE, but they are pretty easy to follow. Of course, as loops get larger, it may become a little more difficult to follow Loops 16

17 Loop Control with DO-WHILE DO requires 2 parameters 1 st is the code to execute for each cycle of the loop Use BLOCK for multiple lines Optionally no BLOCK for a single line 2 nd is the WHILE command containing the looping condition Loop back if TRUE ; after WHILE int i; /* Count from 0 to 9 */ i = 0; do /* print i here */ i++; while( i<10 ); Roberto Muscedere 17 In a DO-WHILE loop the code is executed first, then the condition is evaluated Loops 17

18 Example: DO-WHILE The condition is simply i When not zero, the condition is TRUE When zero, the condition is FALSE When i is not zero, continue the loop int i; /* Count from 9 to 0 */ i = 10; do i--; /* print i here */ while( i ); Roberto Muscedere 18 In this example, i is the condition. This is very common to use variables only as conditions in C programming. The primary reason for this is because it translates to assembly very well. In assembly, when a variable is loaded, a special flag or condition is set which is known as the ZERO FLAG. This is automatic. There is a jump or branch command which will either execute or not execute based on this flag. This is more efficient than the traditional compare because reading a variable, comparing a variable, and then jumping based on the condition requires one more step than using the ZERO FLAG Loops 18

19 Loop Control with WHILE WHILE requires 2 parameters 1 st is the looping condition (in brackets) Loop back if TRUE 2 nd is the code to execute for each cycle of the loop Use BLOCK for multiple lines Optionally no BLOCK for a single line No ; after BLOCK int i; /* Count from 0 to 9 */ i = 0; while( i<10 ) /* print i here */ i++; Roberto Muscedere 19 The WHILE loop evaluates the condition first, the executes the code if it is true; very similar to a FOR loop. This is almost similar to an IF, but remember that the code will loop back Loops 19

20 Differences between DO-WHILE and WHILE Loops DO-WHILE Code block is executed Condition is then evaluated Go back WHILE Condition is evaluated Code block is then executed Go back Roberto Muscedere 20 These are the key differences between DO-WHILE and WHILE. You can remember them easily by looking at the code. For DO-WHILE the condition is after the code, implying that the code executes first. In the case of the WHILE loop, the condition is before the code implying the condition executes first Loops 20

21 Loop Control with DO-WHILE The BREAK and CONTINUE commands can be used in DO-WHILE loops int i = 0, j = 9; /* Print i from 0-9 */ /* Print j only when i is greater than 5 */ Forever loop when using WHILE(1) do /* print i here */ i++; j--; if ( i>=10 ) break; if ( i<=6 ) continue; /* print j here */ while( 1 ); Roberto Muscedere 21 Placing a 1 as a condition in the while statement indicates that the condition is always TRUE. BREAK and CONTINUE statements operate the same as in FOR loops Loops 21

22 Loop Control with WHILE The BREAK and CONTINUE commands can be used in WHILE loops Forever loop when using WHILE(1) int i = 0, j = 9; /* Print i from 0-9 */ /* Print j only when i is greater than 5 */ while( 1 ) /* print i here */ i++; j--; if ( i>=10 ) break; if ( i<=6 ) continue; /* print j here */ Roberto Muscedere Loops 22

23 Example: WHILE The condition is simply i When not zero, the condition is TRUE When zero, the condition is FALSE When i is not zero, continue the loop int i; /* Print i from 9 to 0 */ i = 10; while( i ) i--; /* print i here */ Roberto Muscedere Loops 23

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15 Table of Contents 1 INTRODUCTION... 1 2 IF... 1 2.1 BOOLEAN EXPRESSIONS... 3 2.2 BLOCKS... 3 2.3 IF-ELSE... 4 2.4 NESTING... 5 3 SWITCH (SOMETIMES KNOWN AS CASE )... 6 3.1 A BIT ABOUT BREAK... 7 4 CONDITIONAL

More information

Introduction. C provides two styles of flow control:

Introduction. C provides two styles of flow control: Introduction C provides two styles of flow control: Branching Looping Branching is deciding what actions to take and looping is deciding how many times to take a certain action. Branching constructs: if

More information

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture 05 I/O statements Printf, Scanf Simple

More information

8. Control statements

8. Control statements 8. Control statements A simple C++ statement is each of the individual instructions of a program, like the variable declarations and expressions seen in previous sections. They always end with a semicolon

More information

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

Flow of Control. Flow of control The order in which statements are executed. Transfer of control 1 Programming in C Flow of Control Flow of control The order in which statements are executed Transfer of control When the next statement executed is not the next one in sequence 2 Flow of Control Control

More information

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed Programming in C 1 Flow of Control Flow of control The order in which statements are executed Transfer of control When the next statement executed is not the next one in sequence 2 Flow of Control Control

More information

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029 Programming Basics and Practice GEDB029 Decision Making, Branching and Looping Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029 Decision Making and Branching C language possesses such decision-making capabilities

More information

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control. C Flow Control David Chisnall February 1, 2011 Outline What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope Disclaimer! These slides contain a lot of

More information

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section :

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section : CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart ANS. Flowchart:- A diagrametic reperesentation of program is known as flowchart Symbols Q-2) Explain basic structure of c language

More information

LESSON 3. In this lesson you will learn about the conditional and looping constructs that allow you to control the flow of a PHP script.

LESSON 3. In this lesson you will learn about the conditional and looping constructs that allow you to control the flow of a PHP script. LESSON 3 Flow Control In this lesson you will learn about the conditional and looping constructs that allow you to control the flow of a PHP script. In this chapter we ll look at two types of flow control:

More information

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore Lecture - 20 Intermediate code generation Part-4 Run-time environments

More information

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials Fundamentals We build up instructions from three types of materials Constants Expressions Fundamentals Constants are just that, they are values that don t change as our macros are executing Fundamentals

More information

Flow Control. CSC215 Lecture

Flow Control. CSC215 Lecture Flow Control CSC215 Lecture Outline Blocks and compound statements Conditional statements if - statement if-else - statement switch - statement? : opertator Nested conditional statements Repetitive statements

More information

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University Unit 1 Programming Language and Overview of C 1. State whether the following statements are true or false. a. Every line in a C program should end with a semicolon. b. In C language lowercase letters are

More information

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition Learn about repetition (looping) control structures Explore how to construct and use: o Counter-controlled

More information

5. Control Statements

5. Control Statements 5. Control Statements This section of the course will introduce you to the major control statements in C++. These control statements are used to specify the branching in an algorithm/recipe. Control statements

More information

Static Methods. Why use methods?

Static Methods. Why use methods? Static Methods A method is just a collection of code. They are also called functions or procedures. It provides a way to break a larger program up into smaller, reusable chunks. This also has the benefit

More information

Module 4: Decision-making and forming loops

Module 4: Decision-making and forming loops 1 Module 4: Decision-making and forming loops 1. Introduction 2. Decision making 2.1. Simple if statement 2.2. The if else Statement 2.3. Nested if Statement 3. The switch case 4. Forming loops 4.1. The

More information

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition Learn about repetition (looping) control structures Explore how to construct and use: o Counter-controlled

More information

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur Lecture 15 Branching : IF ELSE Statement We are looking

More information

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-3 Flow Of Control Flow of control refers to the

More information

Repetition Structures

Repetition Structures Repetition Structures Chapter 5 Fall 2016, CSUS Introduction to Repetition Structures Chapter 5.1 1 Introduction to Repetition Structures A repetition structure causes a statement or set of statements

More information

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution. Repetition Structures Introduction to Repetition Structures Chapter 5 Spring 2016, CSUS Chapter 5.1 Introduction to Repetition Structures The Problems with Duplicate Code A repetition structure causes

More information

REPETITION CONTROL STRUCTURE LOGO

REPETITION CONTROL STRUCTURE LOGO CSC 128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING REPETITION CONTROL STRUCTURE 1 Contents 1 Introduction 2 for loop 3 while loop 4 do while loop 2 Introduction It is used when a statement or a block of

More information

Design of Decode, Control and Associated Datapath Units

Design of Decode, Control and Associated Datapath Units 1 Design of Decode, Control and Associated Datapath Units ECE/CS 3710 - Computer Design Lab Lab 3 - Due Date: Thu Oct 18 I. OVERVIEW In the previous lab, you have designed the ALU and hooked it up with

More information

Decision Making -Branching. Class Incharge: S. Sasirekha

Decision Making -Branching. Class Incharge: S. Sasirekha Decision Making -Branching Class Incharge: S. Sasirekha Branching The C language programs presented until now follows a sequential form of execution of statements. Many times it is required to alter the

More information

Chapter 2: Functions and Control Structures

Chapter 2: Functions and Control Structures Chapter 2: Functions and Control Structures TRUE/FALSE 1. A function definition contains the lines of code that make up a function. T PTS: 1 REF: 75 2. Functions are placed within parentheses that follow

More information

Figure 1 Common Sub Expression Optimization Example

Figure 1 Common Sub Expression Optimization Example General Code Optimization Techniques Wesley Myers wesley.y.myers@gmail.com Introduction General Code Optimization Techniques Normally, programmers do not always think of hand optimizing code. Most programmers

More information

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS Objectives By the end of this section you should be able to:

More information

Flow Control. So Far: Writing simple statements that get executed one after another.

Flow Control. So Far: Writing simple statements that get executed one after another. Flow Control So Far: Writing simple statements that get executed one after another. Flow Control So Far: Writing simple statements that get executed one after another. Flow control allows the programmer

More information

Learning to Program with Haiku

Learning to Program with Haiku Learning to Program with Haiku Lesson 4 Written by DarkWyrm All material 2010 DarkWyrm It would be incredibly hard to write anything useful if there weren't ways for our programs to make decisions or to

More information

3 The L oop Control Structure

3 The L oop Control Structure 3 The L oop Control Structure Loops The while Loop Tips and Traps More Operators The for Loop Nesting of Loops Multiple Initialisations in the for Loop The Odd Loop The break Statement The continue Statement

More information

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

C++ Programming Lecture 7 Control Structure I (Repetition) Part I C++ Programming Lecture 7 Control Structure I (Repetition) Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department while Repetition Structure I Repetition structure Programmer

More information

CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I CS 106 Introduction to Computer Science I 02 / 15 / 2016 Instructor: Michael Eckmann Questions? Comments? Loops to repeat code while loops for loops do while loops Today s Topics Logical operators Example

More information

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal COMSC-051 Java Programming Part 1 Part-Time Instructor: Joenil Mistal Chapter 5 5 Controlling the Flow of Your Program Control structures allow a programmer to define how and when certain statements will

More information

Programming Fundamentals

Programming Fundamentals Programming Fundamentals Computers are really very dumb machines -- they only do what they are told to do. Most computers perform their operations on a very primitive level. The basic operations of a computer

More information

Quiz 1: Functions and Procedures

Quiz 1: Functions and Procedures Quiz 1: Functions and Procedures Outline Basics Control Flow While Loops Expressions and Statements Functions Primitive Data Types 3 simple data types: number, string, boolean Numbers store numerical data

More information

StoryStylus Scripting Help

StoryStylus Scripting Help StoryStylus Scripting Help Version 0.9.6 Monday, June 29, 2015 One More Story Games, Inc. 2015 Contents Versions... 3 Scripting User Interface... 4 Script Triggers... 5 If-Then Scripting Language... 6

More information

Lecture #2 January 30, 2004 The 6502 Architecture

Lecture #2 January 30, 2004 The 6502 Architecture Lecture #2 January 30, 2004 The 6502 Architecture In order to understand the more modern computer architectures, it is helpful to examine an older but quite successful processor architecture, the MOS-6502.

More information

PIC 10A Flow control. Ernest Ryu UCLA Mathematics

PIC 10A Flow control. Ernest Ryu UCLA Mathematics PIC 10A Flow control Ernest Ryu UCLA Mathematics If statement An if statement conditionally executes a block of code. # include < iostream > using namespace std ; int main () { double d1; cin >> d1; if

More information

Control Structures in Java if-else and switch

Control Structures in Java if-else and switch Control Structures in Java if-else and switch Lecture 4 CGS 3416 Spring 2016 February 2, 2016 Control Flow Control flow refers to the specification of the order in which the individual statements, instructions

More information

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017 Control Structures Lecture 4 COP 3014 Fall 2017 September 18, 2017 Control Flow Control flow refers to the specification of the order in which the individual statements, instructions or function calls

More information

To become familiar with array manipulation, searching, and sorting.

To become familiar with array manipulation, searching, and sorting. ELECTRICAL AND COMPUTER ENGINEERING 06-88-211: COMPUTER AIDED ANALYSIS LABORATORY EXPERIMENT #2: INTRODUCTION TO ARRAYS SID: OBJECTIVE: SECTIONS: Total Mark (out of 20): To become familiar with array manipulation,

More information

Control Structures in Java if-else and switch

Control Structures in Java if-else and switch Control Structures in Java if-else and switch Lecture 4 CGS 3416 Spring 2017 January 23, 2017 Lecture 4CGS 3416 Spring 2017 Selection January 23, 2017 1 / 26 Control Flow Control flow refers to the specification

More information

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic. Coding Workshop Learning to Program with an Arduino Lecture Notes Table of Contents Programming ntroduction Values Assignment Arithmetic Control Tests f Blocks For Blocks Functions Arduino Main Functions

More information

CT 229 Java Syntax Continued

CT 229 Java Syntax Continued CT 229 Java Syntax Continued 29/09/2006 CT229 Lab Assignments One Week Extension for Lab Assignment 1. Due Date: Oct 8 th Before submission make sure that the name of each.java file matches the name given

More information

Unit 7. 'while' Loops

Unit 7. 'while' Loops 1 Unit 7 'while' Loops 2 Control Structures We need ways of making decisions in our program To repeat code until we want it to stop To only execute certain code if a condition is true To execute one segment

More information

CHAPTER : 9 FLOW OF CONTROL

CHAPTER : 9 FLOW OF CONTROL CHAPTER 9 FLOW OF CONTROL Statements-Statements are the instructions given to the Computer to perform any kind of action. Null Statement-A null statement is useful in those case where syntax of the language

More information

ENGR 1181 MATLAB 09: For Loops 2

ENGR 1181 MATLAB 09: For Loops 2 ENGR 1181 MATLAB 09: For Loops Learning Objectives 1. Use more complex ways of setting the loop index. Construct nested loops in the following situations: a. For use with two dimensional arrays b. For

More information

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions. Introduction In the programs that we have dealt with so far, all statements inside the main function were executed in sequence as they appeared, one after the other. This type of sequencing is adequate

More information

Control Flow and Loops. Steven R. Bagley

Control Flow and Loops. Steven R. Bagley Control Flow and Loops Steven R. Bagley Introduction Started to look at writing ARM Assembly Language Saw the structure of various commands Load (LDR), Store (STR) for accessing memory SWIs for OS access

More information

OER on Loops in C Programming

OER on Loops in C Programming OER on Loops in C Programming Prepared by GROUP ID 537 B. Bala Nagendra Prasad (bbalanagendraprasad@gmail.com C. Naga Swaroopa (swarupabaalu@gmail.com) L. Hari Krishna (lhkmaths@gmail.com) 1 Table of Contents

More information

Chapter 3 Structured Program Development

Chapter 3 Structured Program Development 1 Chapter 3 Structured Program Development Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 3 - Structured Program Development Outline 3.1 Introduction

More information

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode STUDENT OUTLINE Lesson 8: Structured Programming, Control Structures, if- Statements, Pseudocode INTRODUCTION: This lesson is the first of four covering the standard control structures of a high-level

More information

Contents. Slide Set 1. About these slides. Outline of Slide Set 1. Typographical conventions: Italics. Typographical conventions. About these slides

Contents. Slide Set 1. About these slides. Outline of Slide Set 1. Typographical conventions: Italics. Typographical conventions. About these slides Slide Set 1 for ENCM 369 Winter 2014 Lecture Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Winter Term, 2014 ENCM 369 W14 Section

More information

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control Chapter 2 Control, Quick Overview Control Selection Selection Selection is how programs make choices, and it is the process of making choices that provides a lot of the power of computing 1 Python if statement

More information

Control structures: if

Control structures: if Control structures: if Like a recipe tells the cook what to do, a program tells the computer what to do; in imperative languages like Java, lines in the program are imperative statements and you can imagine

More information

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming Intro to Programming Unit 7 Intro to Programming 1 What is Programming? 1. Programming Languages 2. Markup vs. Programming 1. Introduction 2. Print Statement 3. Strings 4. Types and Values 5. Math Externals

More information

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved. Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-3 Flow Of Control Flow of control refers to the

More information

CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I CS 106 Introduction to Computer Science I 06 / 04 / 2015 Instructor: Michael Eckmann Today s Topics Questions / comments? Calling methods (noting parameter(s) and their types, as well as the return type)

More information

CSC 220: Computer Organization Unit 10 Arithmetic-logic units

CSC 220: Computer Organization Unit 10 Arithmetic-logic units College of Computer and Information Sciences Department of Computer Science CSC 220: Computer Organization Unit 10 Arithmetic-logic units 1 Remember: 2 Arithmetic-logic units An arithmetic-logic unit,

More information

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to A PROGRAM IS A SEQUENCE of instructions that a computer can execute to perform some task. A simple enough idea, but for the computer to make any use of the instructions, they must be written in a form

More information

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Repe$$on CSC 121 Spring 2017 Howard Rosenthal Repe$$on CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Learn the following three repetition structures in Java, their syntax, their similarities and differences, and how to avoid common errors when

More information

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva All copyrights reserved - KV NAD, Aluva Dinesh Kumar Ram PGT(CS) KV NAD Aluva Overview Looping Introduction While loops Syntax Examples Points to Observe Infinite Loops Examples using while loops do..

More information

PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T-SQL)

PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T-SQL) Technology & Information Management Instructor: Michael Kremer, Ph.D. Class 3 Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T-SQL) AGENDA 3. Declaring Variables/Constants 4. Flow Control

More information

Intro. Scheme Basics. scm> 5 5. scm>

Intro. Scheme Basics. scm> 5 5. scm> Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if

More information

Control Structures. Important Semantic Difference

Control Structures. Important Semantic Difference Control Structures Important Semantic Difference In all of these loops we are going to discuss, the braces are ALWAYS REQUIRED. Even if your loop/block only has one statement, you must include the braces.

More information

Unit 3 Decision making, Looping and Arrays

Unit 3 Decision making, Looping and Arrays Unit 3 Decision making, Looping and Arrays Decision Making During programming, we have a number of situations where we may have to change the order of execution of statements based on certain conditions.

More information

QUIZ Friends class Y;

QUIZ Friends class Y; QUIZ Friends class Y; Is a forward declaration neeed here? QUIZ Friends QUIZ Friends - CONCLUSION Forward (a.k.a. incomplete) declarations are needed only when we declare member functions as friends. They

More information

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting Your factors.c and multtable.c files are due by Wednesday, 11:59 pm, to be submitted on the SoC handin page at http://handin.cs.clemson.edu.

More information

Topic Notes: MIPS Instruction Set Architecture

Topic Notes: MIPS Instruction Set Architecture Computer Science 220 Assembly Language & Comp. Architecture Siena College Fall 2011 Topic Notes: MIPS Instruction Set Architecture vonneumann Architecture Modern computers use the vonneumann architecture.

More information

from Appendix B: Some C Essentials

from Appendix B: Some C Essentials from Appendix B: Some C Essentials tw rev. 22.9.16 If you use or reference these slides or the associated textbook, please cite the original authors work as follows: Toulson, R. & Wilmshurst, T. (2016).

More information

The compiler is spewing error messages.

The compiler is spewing error messages. Appendix B Debugging There are a few different kinds of errors that can occur in a program, and it is useful to distinguish between them in order to track them down more quickly. Compile-time errors are

More information

C++ Programming: From Problem Analysis to Program Design, Third Edition

C++ Programming: From Problem Analysis to Program Design, Third Edition C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 5: Control Structures II (Repetition) Why Is Repetition Needed? Repetition allows you to efficiently use variables Can input,

More information

Lecture 5 Tao Wang 1

Lecture 5 Tao Wang 1 Lecture 5 Tao Wang 1 Objectives In this chapter, you will learn about: Selection criteria Relational operators Logical operators The if-else statement Nested if statements C++ for Engineers and Scientists,

More information

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Computer Programming Basic Control Flow - Loops Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Objectives To learn about the three types of loops: while for do To avoid infinite

More information

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG 1 Notice Prepare the Weekly Quiz The weekly quiz is for the knowledge we learned in the previous week (both the

More information

Fundamentals of Computer Programming Using C

Fundamentals of Computer Programming Using C CHARUTAR VIDYA MANDAL S SEMCOM Vallabh Vidyanagar Faculty Name: Ami D. Trivedi Class: FYBCA Subject: US01CBCA01 (Fundamentals of Computer Programming Using C) *UNIT 3 (Structured Programming, Library Functions

More information

C++ Programming Lecture 1 Software Engineering Group

C++ Programming Lecture 1 Software Engineering Group C++ Programming Lecture 1 Software Engineering Group Philipp D. Schubert Contents 1. More on data types 2. Expressions 3. Const & Constexpr 4. Statements 5. Control flow 6. Recap More on datatypes: build-in

More information

Chapter 04: Instruction Sets and the Processor organizations. Lesson 20: RISC and converged Architecture

Chapter 04: Instruction Sets and the Processor organizations. Lesson 20: RISC and converged Architecture Chapter 04: Instruction Sets and the Processor organizations Lesson 20: RISC and converged Architecture 1 Objective Learn the RISC architecture Learn the Converged Architecture 2 Reduced Instruction Set

More information

Software Design and Analysis for Engineers

Software Design and Analysis for Engineers Software Design and Analysis for Engineers by Dr. Lesley Shannon Email: lshannon@ensc.sfu.ca Course Website: http://www.ensc.sfu.ca/~lshannon/courses/ensc251 Simon Fraser University Slide Set: 9 Date:

More information

Chapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x );

Chapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x ); Chapter 4 Loops Sections Pages Review Questions Programming Exercises 4.1 4.7, 4.9 4.10 104 117, 122 128 2 9, 11 13,15 16,18 19,21 2,4,6,8,10,12,14,18,20,24,26,28,30,38 Loops Loops are used to make a program

More information

Chapter Goals. Contents LOOPS

Chapter Goals. Contents LOOPS CHAPTER 4 LOOPS Slides by Donald W. Smith TechNeTrain.com Final Draft Oct 30, 2011 Chapter Goals To implement while, for, and do loops To hand-trace the execution of a program To become familiar with common

More information

If you don t, it will return the same thing as == But this may not be what you want... Several different kinds of equality to consider:

If you don t, it will return the same thing as == But this may not be what you want... Several different kinds of equality to consider: CS61B Summer 2006 Instructor: Erin Korber Lecture 5, 3 July Reading for tomorrow: Chs. 7 and 8 1 Comparing Objects Every class has an equals method, whether you write one or not. If you don t, it will

More information

In this chapter you will learn:

In this chapter you will learn: 1 In this chapter you will learn: Essentials of counter-controlled repetition. Use for, while and do while to execute statements in program repeatedly. Use nested control statements in your program. 2

More information

Chapter 5: Control Structures

Chapter 5: Control Structures Chapter 5: Control Structures In this chapter you will learn about: Sequential structure Selection structure if if else switch Repetition Structure while do while for Continue and break statements S1 2017/18

More information

Computer Science Lab Exercise 1

Computer Science Lab Exercise 1 1 of 10 Computer Science 127 - Lab Exercise 1 Introduction to Excel User-Defined Functions (pdf) During this lab you will experiment with creating Excel user-defined functions (UDFs). Background We use

More information

Appendix: Common Errors

Appendix: Common Errors Appendix: Common Errors Appendix 439 This appendix offers a brief overview of common errors that occur in Processing, what those errors mean and why they occur. The language of error messages can often

More information

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. Internet & World Wide Web How to Program, 5/e Sequential execution Execute statements in the order they appear in the code Transfer of control Changing the order in which statements execute All scripts

More information

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Repe$$on CSC 121 Fall 2015 Howard Rosenthal Repe$$on CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Learn the following three repetition methods, their similarities and differences, and how to avoid common errors when using them: while do-while

More information

1007 Imperative Programming Part II

1007 Imperative Programming Part II Agenda 1007 Imperative Programming Part II We ve seen the basic ideas of sequence, iteration and selection. Now let s look at what else we need to start writing useful programs. Details now start to be

More information

Text Input and Conditionals

Text Input and Conditionals Text Input and Conditionals Text Input Many programs allow the user to enter information, like a username and password. Python makes taking input from the user seamless with a single line of code: input()

More information

Overview. Iteration 4 kinds of loops. Infinite Loops. for while do while foreach

Overview. Iteration 4 kinds of loops. Infinite Loops. for while do while foreach Repetition Overview Iteration 4 kinds of loops for while do while foreach Infinite Loops Iteration One thing that computers do well is repeat commands Programmers use loops to accomplish this 4 kinds of

More information

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead. Chapter 9: Rules Chapter 1:Style and Program Organization Rule 1-1: Organize programs for readability, just as you would expect an author to organize a book. Rule 1-2: Divide each module up into a public

More information

Lecture 5 8/24/18. Writing larger programs. Comments. What are we going to cover today? Using Comments. Comments in Python. Writing larger programs

Lecture 5 8/24/18. Writing larger programs. Comments. What are we going to cover today? Using Comments. Comments in Python. Writing larger programs What are we going to cover today? Lecture 5 Writing and Testing Programs Writing larger programs Commenting Design Testing Writing larger programs As programs become larger and more complex, it becomes

More information

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators Objectives Chapter 4: Control Structures I (Selection) In this chapter, you will: Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical (Boolean)

More information

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch Purpose: We will take a look at programming this week using a language called Scratch. Scratch is a programming language that was developed

More information

Get JAVA. I will just tell you what I did (on January 10, 2017). I went to:

Get JAVA. I will just tell you what I did (on January 10, 2017). I went to: Get JAVA To compile programs you need the JDK (Java Development Kit). To RUN programs you need the JRE (Java Runtime Environment). This download will get BOTH of them, so that you will be able to both

More information

While Loops CHAPTER 5: LOOP STRUCTURES. While Loops. While Loops 2/7/2013

While Loops CHAPTER 5: LOOP STRUCTURES. While Loops. While Loops 2/7/2013 While Loops A loop performs an iteration or repetition A while loop is the simplest form of a loop Occurs when a condition is true CHAPTER 5: LOOP STRUCTURES Introduction to Computer Science Using Ruby

More information

Loops In Python. In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.

Loops In Python. In this section of notes you will learn how to rerun parts of your program without having to duplicate the code. Loops In Python In this section of notes you will learn how to rerun parts of your program without having to duplicate the code. The Need For Repetition (Loops) Writing out a simple counting program (1

More information