# COMPSCI 210 Part II Decisions & Iterations

Save this PDF as:

Size: px
Start display at page:

## Transcription

1 Agenda COMPSCI 210 Part II Decisions & Iterations Based on McGraw-Hill Agenda: Decision-making statements: he if statement he switch statement Iterations he for statement he while statement he do-while statement Nested Loops break, continue Problem Solving in C extbook: Chapter 13 Control Structures Exercise: Write a program to print all the powers of 2 II-06-2 Control Structures Conditional making a decision about which code to execute, based on evaluated expression if if- switch If if (condition) action; Condition is a C expression, which evaluates to RUE (non-zero) or ALSE (zero). Action is a C statement, which may be simple or compound (a block). condition action Iteration executing code multiple times, ending based on evaluated expression while for do-while if (x <= 10) y = x * x + 5; if (x <= 10) { y = x * x + 5; z = (2 * y) / 3; if (x <= 10) y = x * x + 5; z = (2 * y) / 3; compound statement; both executed if x <= 10 only first statement is conditional; second statement is always executed II-06-3 II

2 More If Examples if (0 <= age && age <= 11) kids += 1; if (month == 4 month == 6 month == 9 month == 11) printf( he month has 30 days.\n ); if (x = 2) y = 5; always true, so action is always executed! his is a common programming error (= instead of ==), not caught by compiler because it s syntactically correct. Generating Code for If Statement ; if (x == 2) y = 5; LC3 Code: NO_RUE into R0 ADD R0, R0, #-2 ; subtract 2 BRnp NO_RUE ; if non-zero, x is not 2 AND R1, R1, #0 ; store 5 to y ADD R1, R1, #5 SR R1, R5, #-1 0x0000 R5 0x y x If s can be nested, if (x == 3) if (y!= 6) { z = z + 1; w = w + 2; is the same as if ((x == 3) && (y!= 6)) { z = z + 1; w = w + 2; II-06-5 II-06-6 If- if (condition) action_if; action_; action_if condition action_ Generating Code for If-Else LC3 Code if (x) { y++; z--; { y--; z++; LDR R0, R5, #0 BRz ELSE ; x is not zero LDR R1, R5, #-1 ; load y ADD R1, R1, #1 ; incr y SR R1, R5, #-1 LDR R1, R5, #-2 ; load z ADD R1, R1, #-1 ; decr z SR R1, R5, #-2 BR ; skip code ; x is zero 0x0000 R5 0x z y x Else allows choice between two mutually exclusive actions without re-testing condition. ELSE LDR R1, R5, #-1 ; decr y ADD R1, R1, #-1 SR R1, R5, #-1 LDR R1, R5, #-2 ; incr z ADD R1, R1, #1 SR R1, R5, #-2 II-06-7 II

3 Matching Else with If Else is always associated with closest unassociated if. if (x!= 10) if (y > 3) z = z / 2; z = z * 2; is the same as if (x!= 10) { if (y > 3) z = z / 2; z = z * 2; is NO the same as if (x!= 10) { if (y > 3) z = z / 2; z = z * 2; Indentation can improve readability o force the clause to match the first if clause, you must add a pair of braces: Chaining If s and Else s he conditions are evaluated in order until one that evaluates to true is found. If none of them are true then the block is executed. he final is optional if (month == 4 month == 6 month == 9 month == 11) printf( Month has 30 days.\n ); if (month == 1 month == 3 month == 5 month == 7 month == 8 month == 10 month == 12) printf( Month has 31 days.\n ); if (month == 2) printf( Month has 28 or 29 days.\n ); printf( Don t know that month.\n ); II-06-9 II Switch Alternative to long if- chain. If break is not used, then case "falls through" to the next. No two case statements can have the same value. Value must be an an integer value: type long, int, or char he default statement is optional switch (expression) { case const1: action1; case const2: action2; default: action3; evaluate expression = const1? = const2? action3 action1 action2 Switch Example /* same as month example for if- */ switch (month) { case 4: case 6: case 9: case 11: printf( Month has 30 days.\n ); case 1: case 3: /* some cases omitted for brevity*/ printf( Month has 31 days.\n ); case 2: printf( Month has 28 or 29 days.\n ); default: printf( Don t know that month.\n ); II II

4 More About Switch Case expressions must be constant. /* illegal if i is a variable */ case i: If no break, then next case is also executed. switch (a) { case 1: printf( A ); case 2: printf( B ); default: printf( C ); If a is 1, prints ABC. If a is 2, prints BC. Otherwise, prints C. Writing Loops Loops Repeated execution of one or more statements until a terminating condition occurs Pre-test and post-test loops ypes of loops: Pre-test loops while for Post-test loop do while II II While Executes loop body as long as test evaluates to RUE (non-zero). while (test) loop_body; test loop_body Generating Code for While x = 0; while (x < 10) { printf( %d, x); x = x + 1; AND R0, R0, #0 SR R0, R5, #0 ; x = 0 ; test Note: est is evaluated before executing loop body When false, the loop body is not executed at all LOOP ADD R0, R0, #-10 BRzp ; loop body <printf> ADD R0, R0, #1 ; incr x SR R0, R5, #0 BR LOOP ; test again II II

5 Do-While Executes loop body as long as test evaluates to RUE (non-zero). loop_body Generating Code for Do-While do { printf( %d, x); x = x + 1; while (x < 10); do loop_body; while (test); Note: est is evaluated after executing loop body he loop body is executed at least once. Note: Don't forget to put a semicolon after the test. test LOOP <printf> ADD R0, R0, #1 ; incr 1 SR R0, R5, #0 ; store x ADD R0, R0, #-10 ;test x BRzp BR LOOP ; test again II II Infinite Loops he following loop will never terminate: x = 0; while (x < 10) printf( %d, x); or Executes loop body as long as test evaluates to RUE (non-zero). for (init; end-test; re-init) statement Loop body does not change condition, so test never fails. his is a common programming error that can be difficult to find. Initialization and re-initialization code included in loop statement. Note: est is evaluated before executing loop body. init test loop_body II re-init II

6 Generating Code for or for (i = 0; i < 10; i++) printf( %d, i); ; init AND R0, R0, #0 SR R0, R5, #0 ; i = 0 ; test his is the same as the while example! LOOP LDR R0, R5, #0 ; load i ADD R0, R0, #-10 BRzp ; loop body LDR R0, R5, #0 ; load i <printf> ; re-init ADD R0, R0, #1 ; incr i SR R0, R5, #0 BR LOOP ; test again Example or Loops /* -- what is the output of this loop? -- */ for (i = 0; i <= 10; i ++) printf("%d ", i); /* -- what does this one output? -- */ letter = 'a'; for (c = 0; c < 26; c++) printf("%c ", letter+c); /* -- what does this loop do? -- */ numberofones = 0; for (bitnum = 0; bitnum < 16; bitnum++) { if (inputvalue & (1 << bitnum)) numberofones++; /* -- what is the output of this loop? -- */ int i = 1; while (i < 3) { II II Nested Loops Loop body can (of course) be another loop. /* print a multiplication table */ for (mp1 = 0; mp1 < 10; mp1++) { for (mp2 = 0; mp2 < 10; mp2++) { printf( %d\t, mp1*mp2); printf( \n ); he test for the inner loop depends on the counter variable of the outer loop. for(i = 0; i < row; i ++) { for (j = 0; j <= i; j++) { printf("*"); printf("\n"); * ** *** for (outer = 1; outer <= input; outer++) { for (inner = 0; inner < outer; inner++) { sum += inner; Braces aren t necessary, but they make the code easier to read. Input an integer: 4 he result is 10 II or vs. While In general: or loop is preferred for counter-based loops. Explicit counter variable Easy to see how counter is modified each loop While loop is preferred for sentinel-based loops. est checks for sentinel value. Either kind of loop can be expressed as the other, so it s really a matter of style and readability. II

7 Break and Continue used only in switch statement or iteration statement passes control out of the smallest (loop or switch) statement containing it to the statement immediately following usually used to exit a loop before terminating condition occurs (or to exit switch statement when case is done) continue; used only in iteration statement terminates the execution of the loop body for this iteration loop expression is evaluated to see whether another iteration should be performed if for loop, also executes the re-initializer II Examples on break/continue for(i = 0; i < row; i ++) { for (j = 0; j < row; j++) { if ( i+j >= row ) printf("*"); printf("\n"); *** ** * for (i= 1; i <= row; i++ ) { if( i % 2 == 0) continue; // Go back to for printf("i = %d\n", i ); i = 1 i = 3 i for output 1 1<=4 i=1 2 2<=4 Continue, jump to next iteration 3 3<=4 i=1 i=3 4 4<=4 Continue, jump to next iteration 5 alse II Example What does the following loop do? for (i = 0; i <= 20; i++) { if (i%2 == 0) continue; printf("%d ", i); How about this one? What happens if break instead of continue? for (i = 0; i <= 20; i++) { if (i%2 == 0) printf("%d ", i); What would be an easier way to write this? II

### C Control Structures. Chapter 13

C Control Structures Chapter 13 Cyrus Bazeghi Conditional Control Structures making a decision about which code to execute, based on evaluated expression if Iterative if-else switch executing code multiple

### 14 MAKING DECISIONS IN C

14 MAKING DECISIONS IN C 14.1 INTRODUCTION So far we have seen that in C programs the instructions are executed in the same order in which they appear in the program. Each instruction is executed once

### Control structures in C

Control structures in C Lecture Topics Conditional constructs Iterative constructs Examples Style Lecture materials Textbook 13.3-13.5 Homework None Machine problem MP1.1 due February 2 at 5pm submitted

### Indicates the end of data when the number of data is not known in advance Must be distinct from the data but be of the same type

Program Control Essentials of repetition Loop control variable Counts the number of repetitions in the counter-controlled loop Controls the execution of loop Sentinel value Indicates the end of data when

### Two-way selection. Branching and Looping

Control Structures: are those statements that decide the order in which individual statements or instructions of a program are executed or evaluated. Control Structures are broadly classified into: 1.

### Introduction to C. Control Flow. Instructor: Yin Lou 01/26/2011. Introduction to C CS 2022, Spring 2011, Lecture 2

Introduction to C Control Flow Instructor: Yin Lou 01/26/2011 Statements Statements := ; x = 0; ++i; printf("%d", x); Blocks := x = 0; ++i; printf("%d", x);

### Outline. Repetition (Loops) Types of Loops. Repetitions with Loops. Terminating Loops. PreTest vs. PostTest Loops. II.

Repetition (Loops) Outline Want to do some repetitive sequence of actions: print vertical line of s Corresponding program: printf( \n ); printf( \n ); printf( \n ); printf( \n ); printf( \n ); II. Program

### C AND C++ PROGRAMMING

C AND C++ PROGRAMMING Bharathidasan University A Courseware prepared by University Informatics Centre Part I - Programming in C Getting Started This courseware is intended to be an introduction to C programming

### Senem Kumova Metin & Ilker Korkmaz 1

Senem Kumova Metin & Ilker Korkmaz 1 A loop is a block of code that can be performed repeatedly. A loop is controlled by a condition that is checked each time through the loop. C supports two categories

### Chapter 4 C Program Control

Chapter 4 C Program Control Objectives of this chapter: Repetitions will be considered in greater detail for.. repetition do while repetition Also multiple selection switch case statement will be learned.

### Selection: if Statement

: if Statement The simplest selection structure in C++ is the if statement. Syntactically: 1 if The Boolean expression must be enclosed in parentheses, and

### Adjusted/Modified by Nicole Tobias. Chapter 5: Control Structures II (Repetition)

Adjusted/Modified by Nicole Tobias Chapter 5: Control Structures II (Repetition) Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct and use

### Pseudocode. Pseudocode. Guide for Pseudocode. Computers in Engineering Pseudocode and C Language Review. Example Pseudocode.

Computers in Engineering Pseudocode and C Language Review Pseudocode Pseudocode is an artificial and informal language that helps you develop algorithms Pseudocode is similar to everyday English; it is

### Chapter 11 Introduction to Programming in C

Programming Levels Chapter 11 Introduction to Programming in C Based on slides McGraw-Hill Additional material 2004/2005/2006 Lewis/Martin High-Level Languages Low-Level Languages Application Languages

### Control Structure. Pseudocode: Input number If number is even Then Print even Else print odd. Flowchart: Begin. End. Print Odd. number even?

Control Structure Normally, a program is executed in a sequential manner.however, in many cases, a program has to choose among alternative statements C++ provides constructs that enable the programmer

### Statement-Level Control Structures

Statement-Level Control Structures Introduction Selection Statements Iterative Statements Unconditional Branching Levels of Control Flow Within expressions Among program units Among program statements

### Conditional and Looping Construct

Chapter 3 Conditional and Looping Construct After studying this lesson, students will be able to: Understand the concept and usage of selection and iteration statements. Know various types of loops available

### QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall-2015 1

QUIZ-II Write a program that mimics a calculator. The program should take as input two integers and the operation to be performed. It should then output the numbers, the operator, and the result. (For

### VB.NET Programming Fundamentals

Chapter 3 Objectives Programming Fundamentals In this chapter, you will: Learn about the programming language Write a module definition Use variables and data types Compute with Write decision-making statements

### Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

1. Explain C tokens Tokens are basic building blocks of a C program. A token is the smallest element of a C program that is meaningful to the compiler. The C compiler recognizes the following kinds of

### Branching. We ll continue to look at the basics of writing C++ programs including. Overview

Branching Overview We ll continue to look at the basics of writing C++ programs including Boolean Expressions (need to know this before we can using branching and looping!) Branching J.S. Bradbury CSCI

### MIT Aurangabad FE Computer Engineering

MIT Aurangabad FE Computer Engineering Unit 1: Introduction to C 1. The symbol # is called a. Header file c. include b. Preprocessor d. semicolon 2. The size of integer number is limited to a. -32768 to

### Engineering Problem Solving with C++, Etter/Ingber

Engineering Problem Solving with C++, Etter/Ingber Chapter 3 Control Structures 1 Control structures Algorithm Development Conditional Expressions Selection Statements Repetition Statements Structuring

### Reading Assignment. Main Program in C. K.N. King Chapter 2. K.N. King Chapter 3. K.N. King Chapter 4. K.N. King Chapter 7. Our first C program

Reading Assignment Main Program in C In C the main program is a function called main The body of the function is enclosed in left ( ) and right ( ) curly braces. K.N. King Chapter 2 K.N. King Chapter 3

### Chapter 8 Statement-Level Control Structures Chapter 8 Topics

Chapter 8 Statement-Level Control Structures Chapter 8 Topics Introduction Selection Statements Iterative Statements Unconditional Branching Chapter 8 Statement-Level Control Structures Introduction A

### 9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

9 Control Statements 9.1 Introduction The normal flow of execution in a high level language is sequential, i.e., each statement is executed in the order of its appearance in the program. However, depending

### What does the following code print? (Assume the program compiles without error). Trick question! Note the semicolon at the end of the second line.

All multiple choice questions are equally weighted. You can generally assume that code shown in the questions is intended to be syntactically correct, unless something in the question or one of the answers

### 6. Control Structures

- 35 - Control Structures: 6. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may bifurcate, repeat code or take decisions. For that purpose,

### Chapter 8. Statement-Level Control Structures

Chapter 8 Statement-Level Control Structures Chapter 8 Topics Introduction Selection Statements Iterative Statements Unconditional Branching Guarded Commands Conclusions 8-2 1 Levels of Control Flow Within

### NRI Institute of Technology Principles of Programming Languages III B.Tech I Sem (R09)-CSE

Unit-4 Expressions and Assignment Statements Introduction Expressions are the fundamental means of specifying computations in a programming language. To understand expression evaluation, need to be familiar

### Java iteration statements

Java iteration statements Iteration statements are statements which appear in the source code only once, but it execute many times. Such kind of statements are called loops. Almost all the programming

### Thinking in C. Darin Brezeale. March 25, 2010

Thinking in C Darin Brezeale March 25, 2010 NOTE: This is definitely a work in progress. 1 Introduction One of the most difficult parts of learning to program is knowing how to deconstruct a problem in

### Software II: Principles of Programming Languages

Software II: Principles of Programming Languages Lecture 8 Statement-Level Control Structures Control Statements: Evolution FORTRAN I control statements were based directly on IBM 704 hardware Much research

### Organization of Programming Languages CS3200 / 5200N. Lecture 08

Organization of Programming Languages CS3200 / 5200N Razvan C. Bunescu School of Electrical Engineering and Computer Science bunescu@ohio.edu Control Flow Control flow = the flow of control, or execution

### Reading Assignment Control Flow Statements switch while for break continue Scopes of Declaration Scopes and Visibility Good Style: smallest scope

Reading Assignment Control Flow Statements Scopes of Declaration K.N. King, Chapter 8 Assert function Grouping: and Decision making: if, switch Loop building: while, do, for Loop ending: break, continue

### Basic C Syntax. Comp-206 : Introduction to Software Systems Lecture 10. Alexandre Denault Computer Science McGill University Fall 2006

Basic C Syntax Comp-206 : Introduction to Software Systems Lecture 10 Alexandre Denault Computer Science McGill University Fall 2006 Next Week I'm away for the week. I'll still check my mails though. No

### What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

What is a Loop? CSC Intermediate Programming Looping A loop is a repetition control structure It causes a single statement or a group of statements to be executed repeatedly It uses a condition to control

### Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

JAVA OPERATORS GENERAL Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators

### Going from Python to C

Going from Python to C Darin Brezeale December 8, 2011 Python is a high-level, interpreted language. C has many of the same types of programming constructs as in Python: arrays, loops, conditionals, functions,

### Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Embedded Systems A Review of ANSI C and Considerations for Embedded C Programming Dr. Jeff Jackson Lecture 2-1 Review of ANSI C Topics Basic features of C C fundamentals Basic data types Expressions Selection

### 6.096 Lab 1. Due: 7 January 12:30:00. September 20, 2011

6.096 Lab 1 Due: 7 January 1:30:00 September 0, 011 1 Additional Material 1.1 Constants A constant is an expressions with a fixed value. Kinds of constants: Literals: used to express particular values

### MPATE-GE 2618: C Programming for Music Technology. Unit 1.3

MPATE-GE 2618: C Programming for Music Technology Unit 1.3 More operators e ++ and -- are increment and decrement operators ++i or i++ is equivalent to i = i + 1; --i or i-- is equivalent to i = i - 1;

### Today s Lecture. SYS-1A4Y Programming - Languages and Software Construction. Conditional Execution. Conditional Execution.

SYS-1A4Y Programming - Languages and Software Construction Dr Gavin Cawley Lecture 9 - Selection Today s Lecture In the previous lectures: Using objects Strings NumberFormat In this lecture: Relational

### Boolean Variables and Expressions

Boolean Variables and Expressions 1 Recall Standard C++ supports a simple data type specialized for representing logical values. bool type variables can have either of two values: or The identifiers and

### A relational operator is used to make comparison between two expressions. All these operators are binary and require two operands.

Control Structures in C++ Index Control Structures in C++ 3.1 Relational Operators A relational operator is used to make comparison between two expressions. All these operators are binary and require two

### 6.087 Lecture 3 January 13, 2010

6.087 Lecture 3 January 13, 2010 Review Blocks and Compound Statements Control Flow Conditional Statements Loops Functions Modular Programming Variable Scope Static Variables Register Variables 1 Review:

### 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

### While Loop. 6. Iteration

While Loop 1 Loop - a control structure that causes a set of statements to be executed repeatedly, (reiterated). While statement - most versatile type of loop in C++ false while boolean expression true

### UNIT-1. C Programming & Data Structures. Introduction to Computers: Computing Environment: Types of Computing Environments:

Introduction to Computers: C Programming & Data Structures UNIT-1 A computer system consists of hardware and software. Computer hardware is the collection of physical elements that comprise a computer

### C Programming Dr. Hasan Demirel

C How to Program, H. M. Deitel and P. J. Deitel, Prentice Hall, 5 th edition (3 rd edition or above is also OK). Introduction to C Programming Dr. Hasan Demirel Programming Languages There are three types

### while for ! do while ! set an accumulator variable to 0 ! add the next number to it inside the loop

Week 7: Advanced Loops while Loops in C++ (review) while (expression) statement statement may be a compound statement (a block: {statements) Gaddis: 5.7-12 CS 1428 Fall 2014 Jill Seaman 1 for if expression

### Figure 1.12 Entering, Translating, and Running a High-Level Language Program Pearson Education, Inc. All rights reserved.

Figure 1.12 Entering, Translating, and Running a High-Level Language Program 1 2007 Pearson Education, Inc. All rights reserved. 2 Figure 2.7 General Form of a C Program 2007 Pearson Education, Inc. All

### FORM 1 (Please put your name, section # (10am- 001/2pm-002, and form # on the scantron!!!!) CS 161 Exam I:

FORM 1 (Please put your name, section # (10am- 001/2pm-002, and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each): 1. The following statement is legal: cout >> Hello, my name is

### Model Paper Computer Science Objective. Paper Code Time Allowed: 20 minutes

Note: This is Model Paper for guidance of students & teachers. Q. Model Paper Computer Science Objective Intermediate Part II ( th Class) Examination Session -4 and onward Total marks: 7 Paper Code Time

### MGM s JNEC Question Bank Subject: Computer Engineering

MGM s JNEC Question Bank Subject: Computer Engineering 1. All of the following are examples of computer input units EXCEPT: a) Scanner b) Speaker c) Bar code reader d) Keyboard Answer: b 2. Which of the

### INDEX. C programming Page 1 of 10. 5) Function. 1) Introduction to C Programming

INDEX 1) Introduction to C Programming a. What is C? b. Getting started with C 2) Data Types, Variables, Constants a. Constants, Variables and Keywords b. Types of Variables c. C Keyword d. Types of C

### Intro to Python. P. TenHoopen - WMLUG. Intro to Python. Slide 1. January 2009

P. TenHoopen - WMLUG Slide 1 What is Python? Python is a cross-platform object-oriented programming language invented by Guido van Rossum. It is an interpreted language but there is support for compiling

### Lecture 2 Notes: Flow of Control

6.096 Introduction to C++ January, 2011 Massachusetts Institute of Technology John Marrero Lecture 2 Notes: Flow of Control 1 Motivation Normally, a program executes statements from first to last. The

### C++ Basics. C++ Basics: Names: Identifiers. Names: Identifiers. Display 2.1 A C++ Program (1 of 2) Display 2.1 A C++ Program (2 of 2)

C++ Basics C++ Basics: 2.1 Variables and Assignments Variables and Assignments Input and Output Data Types and Expressions Simple Flow of Control Program Style A C++ variable can hold a number or other

### Selection Statements

Chapter 5 Selection Statements 1 Statements So far, we ve used return statements and expression ess statements. e ts. Most of C s remaining statements fall into three categories: Selection statements:

### JavaScript: Control Statements I

1 7 JavaScript: Control Statements I 7.1 Introduction 2 The techniques you will learn here are applicable to most high-level languages, including JavaScript 1 7.2 Algorithms 3 Any computable problem can

### Iterative Statements. 1. How is iteration controlled? 2. Where is the control mechanism in the loop? Logical expression or counter.

Iterative Statements The repeated execution of a statement or compound statement is accomplished either by iteration or recursion General design issues for iteration control statements: 1. How is iteration

### 4.2 The while Loop. ***Same as Fig 3.6 in intro5e p91

Chapter 4 Loops Objectives To use while, do-while, and for loop statements to control the repetition of statements ( 4.2-4.4). To understand the flow of control in loop statements ( 4.2-4.4). To use Boolean

### cin >> num1 >> num2 >> num3 >> num4 >> num5; sum = num1 + num2 + num3 + num4 + num5; average = sum / 5.

Lesson Outcomes At the end of this chapter, student should be able to: Understand the requirement of a loop Understand the Loop Control Variable (LCV) Use increment (++) and decrement (--) operators Program

### Control structures: Conditionals

Control structures: Conditionals Leo Ferres Department of Computer Science Universidad de Concepción leo@inf.udec.cl April 5, 2011 1a 1 Controlling the flow of a program: if... then... else In order to

### The if-statement. Simple and compound statements. The if-statement comes in two forms: Simple statements:

1 2 Simple and compound s The if- Simple s: E.g.: expression; Various jumps : break, goto, continue, return. k = a * p + 3; printf("k = %d\n", k); 1 + 2; ; The if- comes in two forms: or E.g.: if (expression)

### Fundamental Programming Structures in C

Fundamental Programming Structures in C M. Campbell 1993 Deakin University Aim Learning objectives Content After working through this module you should be able to create, compile and run a simple C program.

### Programming for MSc Part I

Herbert Martin Dietze University of Buckingham herbert@the-little-red-haired-girl.org July 24, 2001 Abstract The course introduces the C programming language and fundamental software development techniques.

### Counting with While Loops

Simple Loops Iteration Often, we will want a piece of code to run multiple times. This is called iteration or looping. A specific piece of code that is run multiple times is called a loop. While Loop The

### Chapter 4 Loops. 4.1 Introduction. 4.2 The while Loop

Chapter 4 Loops 4.1 Introduction Loops are structures that control repeated executions of a block of statements. Java provides a powerful control structure called a loop, which controls how many times

### Introduction to C++ Information about the exam. and Revision notes. Introduction to C++ Week 10 Dr Alex Martin 2013 Slide 1

Introduction to C++ Information about the exam and Revision notes Introduction to C++ Week 10 Dr Alex Martin 2013 Slide 1 Info About the Exam The exam is "open-book" format. Your are allowed to refer to

### The switch Statement. Multiple Selection. Multiple Selection (cont.)

The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading Section 4.7, 4.12 Multiple Selection So far, we have only seen binary selection. if (

### Chapter 6 Repetition

Chapter 6 Repetition 6-1 Repetition in C++ The final control/logic structure is repetition: Repetition repeating a block of code until a condition is met There are three repetition statements available

### Programming Language: Syntax. Introduction to C Language Overview, variables, Operators, Statements

Programming Language: Syntax Introduction to C Language Overview, variables, Operators, Statements Based on slides McGraw-Hill Additional material 2004/2005 Lewis/Martin Modified by Diana Palsetia Syntax

### z = x + y * z / 4 % 2-1

1.Which of the following statements should be used to obtain a remainder after dividing 3.14 by 2.1? A. rem = 3.14 % 2.1; B. rem = modf(3.14, 2.1); C. rem = fmod(3.14, 2.1); D. Remainder cannot be obtain

### Statements and Control Flow

Contents 1. Introduction 2. Types and Variables 3. Statements and Control Flow 4. Reading Input 5. Classes and Objects 6. Arrays 7. Methods 8. Scope and Lifetime 9. Utility classes 10. Introduction to

### Tutorial No. 5 - Solution (Decision Making and Branching)

Tutorial No. 5 - Solution (Decision Making and Branching) 1. Explain if...if ladder with flowchart [7] The if ladder is a way of putting together ifs together when multipath decisions are involved. A multipath

### CS 241 Data Organization Coding Standards

CS 241 Data Organization Coding Standards Brooke Chenoweth University of New Mexico Spring 2016 CS-241 Coding Standards All projects and labs must follow the great and hallowed CS-241 coding standards.

### Object Oriented Software Design

Object Oriented Software Design Introduction to Java - II Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa October 28, 2010 G. Lipari (Scuola Superiore Sant Anna) Introduction

### for Loop Lesson 1 Outline

Outline 1. Outline 2. A while Loop That Counts #1 3. A while Loop That Counts #2 4. A while Loop That Counts #3 5. A while Loop That Counts #4 6. Count-Controlled Loops #1 7. Count-Controlled Loops #2

### CMSC 106 Introduction to C Programming

CMSC 106 Introduction to C Programming Math Library, Escape Sequences, Conditionals CMSC 106 - Slides developed by Jan Plane with some modifications by Nelson Padua-Perez Additional Operators Increment

### Sources: On the Web: Slides will be available on:

C programming Introduction The basics of algorithms Structure of a C code, compilation step Constant, variable type, variable scope Expression and operators: assignment, arithmetic operators, comparison,

### Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Boolean Expressions, Conditions, Loops, and Enumerations Relational Operators == // true if two values are equivalent!= // true if two values are not equivalent < // true if left value is less than the

### The if-else statement

Control Structures Processing and Java There is no difference between Processing syntax and Java syntax Processing has it s own IDE and provides lots of methods to do drawing and animation The main IDEs

### Looping Exercises Deciding Which Loop to Use

Looping Exercises Deciding Which Loop to Use At this time you have learned four looping constructs (also called repetition statements). Each of these four loops are ideal to use in different situations.

### Computer Programming I

Computer Programming I Levels: 10-12 Units of Credit: 1.0 CIP Code: 11.0201 Core Code: 35-02-00-00-030 Prerequisites: Secondary Math I, Keyboarding Proficiency, Computer Literacy requirement (e.g. Exploring

### This loop prints out the numbers from 1 through 10 on separate lines. How does it work? Output: 1 2 3 4 5 6 7 8 9 10

Java Loops & Methods The while loop Syntax: while ( condition is true ) { do these statements Just as it says, the statements execute while the condition is true. Once the condition becomes false, execution

### Object Oriented Software Design

Object Oriented Software Design Introduction to Java - II Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa September 14, 2011 G. Lipari (Scuola Superiore Sant Anna) Introduction

1 2 Introduction to Java Applications 2.2 First Program in Java: Printing a Line of Text 2 Application Executes when you use the java command to launch the Java Virtual Machine (JVM) Sample program Displays

### ()A Crash Course in Programming with C++ and the Ubuntu O.S. June 8, / 47

A Crash Course in Programming with C++ and the Ubuntu O.S. Dr. Daniel A. Ray MCS Dept UVa-Wise June 8, 2010 ()A Crash Course in Programming with C++ and the Ubuntu O.S. June 8, 2010 1 / 47 Outline What

### Writing Control Structures

Writing Control Structures Copyright 2006, Oracle. All rights reserved. Oracle Database 10g: PL/SQL Fundamentals 5-1 Objectives After completing this lesson, you should be able to do the following: Identify

### 6.1 Branching (or Selection)

Chapter 6 Branching 6.1 Branching (or Selection) 6.2 Relational and Logical Operators 6.3 The if Statement 6.4 The if-else Statement 6.5 The if-else if-else Statement 6.6 The Nested-if Statement 6.7 The

### 3/13/2012. ESc101: Multiple statement execution using loops. Loops are used when instructions are repeated. while statement can be used for a loop

ESc101: Multiple statement execution using loops Instructor: Krithika Venkataramani Semester 2, 2011-2012 The content of most of these slides are taken from the lecture slides of Prof. Arnab Bhattacharya

### Introduction to C Programming

Introduction to C Programming C HOW TO PROGRAM, 6/E 1992-2010 by Pearson Education, Inc. All Rights Reserved. 2.1 Introduction The C language facilitates a structured and disciplined approach to computer

### Data Structures. Arrays. Array Example. Array Initialization. 57:017, Computers in Engineering Review of Arrays, Sorting, Searching

57:07, Computers in Engineering Review of Arrays, Sorting, Searching Data Structures Our focus thus far has mainly been on control structures An array is a data structure consisting of data items of the

### Programming and Data Structures

Programming and Data Structures Tutorial sheet: 2 Topics: Decision Making, Looping and Branching Q 1. (a) What is wrong with the following loop while ( n

### M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE NOTE: IMPORTANT INSTRUCTIONS: 1. Question Paper in English and Hindi and Candidate can choose any one language. 2. In case of discrepancies in

### Final Exam Review. CS 1428 Fall Jill Seaman. Final Exam

Final Exam Review CS 1428 Fall 2011 Jill Seaman 1 Final Exam Friday, December 9, 11:00am to 1:30pm Derr 241 (here) Closed book, closed notes, clean desk Comprehensive (covers entire course) 25% of your