Senem Kumova Metin & Ilker Korkmaz 1

Transcription

1 Senem Kumova Metin & Ilker Korkmaz 1

2 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 of loops, based on where the program tests the condition: Pretest Loops : while, for Post-test Loops : do-while Senem Kumova Metin & Ilker Korkmaz 2

3 With each iteration, the program tests the condition first before executing the loop s block. If the condition tests to true, the loop continues and executes the block; if the condition tests to false, the loop terminates. With a pretest loop, there is a chance the loop may never execute in the run of a program! 3

4 With each iteration, the program executes the loop s block first and tests against a condition. If the condition tests to true, the loop continues and executes another iteration; if the condition tests to false, the loop terminates. With a post-test loop, the loop will always execute at least once! 4

16 A for loop is a pretest loop that includes three expressions in its header: Loop initialization statement Limit test expression Loop update statement Although not recommended, C allows us to update the loop control statement from within the loop body. The for loop is often used as a counter-controlled loop since we can accurately predict the maximum number of iterations 16

21 We can nest any statement, even another for loop, inside the body of a parent for loop. When we nest a child for loop, it iterates all of it s cycles for each iteration of the parent. #include <stdio.h> int main(){ int i,j; for (i=0 ; i < 10 ; i++) { for (j=0; j < 10; j++) printf("%d %d\n", i, j); return 0; 21

28 Unlike break, the continue statement doesn t terminate a loop, but transfers control to the testing expression (in while and do while loops) or the update expression (in for loops). Like the break statement, avoid using the continue statement. 28

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

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

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

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

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

Agenda COMPSCI 210 Part II Decisions & Iterations Based on slides @ McGraw-Hill Agenda: Decision-making statements: he if statement he switch statement Iterations he for statement he while statement he

Solutions to Assessment: Basic Programming Constructs Question 1: Specify the minimum number of comparisons required to find the largest number among a set of 3 integers X, Y and Z. We already know how

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

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

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

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,

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

The Queue Data Structure in C++ By Eric Suh http://www.cprogramming.com/tutorial/computersciencetheory/queue.html Queues are data structures that, like the stack, have restrictions on where you can add

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.

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

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.

PE1 Worksheet Problem solving 1) What are the four stages of problem solving by programming? What shall be done in each stage? Stage 1: Stage 2: Stage 3: Stage 4: 2) What is the top-down design strategy

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

switch Multiple-Selection Statement (This feature is rarely useful, although it s perfect for programming the iterative song The Twelve Days of Christmas!) If no match occurs, the default case is executed,

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

Learning to Program Lecture P1: Introduction to C Programming is learned with practice and patience. Don t expect to learn solely from these lectures. Do exercises. Experiment and write lots of code. printf("this

Group 1 Question 1. What is the correct value to return to the operating system upon the successful completion of a program? A. -1 B. 1 C. 0 D. Programs do not return a value. Question 2. What is the only

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

Introduction to C Programming CS 40: INTRODUCTION TO U NIX A ND L I NUX O P E R AT ING S Y STEMS Objectives Introduce C programming, including what it is and what it contains, which includes: Command line

Part A Model Answer for Basic Programming in C 1. a ANS. An algorithm is a procedure or formula for solving a problem. OR It is a step by step solution of a well-defined problem. // Algorithm to the biggest

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

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

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

Oxford University Computing Services Programming in C Languages l9.2/2 Typographical Conventions Listed below are the typographical conventions used in this guide. Names of keys on the keyboard are enclosed

Introduction to Programming in C++: Algorithms, Flowcharts and Pseudocode by Brent Daviduck The following material was developed by Brent Daviduck of the Computer Systems Technology program at Red Deer

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

Pythagorean Triples Due on 2014-09-16, 23:59 IST Level: Easy Three numbers form a Pythagorean triple if the sum of squares of two numbers is equal to the square of the third. For example, 3, 5 and 4 form

Control Statements: Part 2, Solutions 5 Let s all move one place on Lewis Carroll The wheel is come full circle William Shakespeare How many apples fell on Newton s head before he took the hint! Robert

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

Java Review (Essentials of Java for Hadoop) Have You Joined Our LinkedIn Group? What is Java? Java JRE - Java is not just a programming language but it is a complete platform for object oriented programming.

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