Senem Kumova Metin & Ilker Korkmaz 1

Save this PDF as:
 WORD  PNG  TXT  JPG

Size: px
Start display at page:

Download "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

5 from Figure 6-2 in Forouzan & Gilberg, p

6 from Figure 6-5 in Forouzan & Gilberg, p

7 from Figure 6-11 in Forouzan & Gilberg, p

8 while( expression ) statement /* loop body */ OR try to use like this: while(expression ) { /* loop body*/ statement1; statement2; // ; Senem Kumova Metin & Ilker Korkmaz 8

9 #include <stdio.h> int main() { int i; printf("enter -1 to quit:\n"); scanf("%d", &i); while (i!= -1) { printf("%d was read\n", i); scanf("%d", &i); return 0; Senem Kumova Metin & Ilker Korkmaz 9

10 Enter -1 to quit: 1 1 was read 2 2 was read 3 3 was read 4 4 was read 5 5 was read -1 Press any key to continue... Senem Kumova Metin & Ilker Korkmaz 10

11 from Figure 6-15 in Forouzan & Gilberg, p

12 Loop Body is executed at LEAST ONCE do { /* loop body*/ statements;... while (expression); Senem Kumova Metin & Ilker Korkmaz 12

13 #include <stdio.h> int main() { int i = 0; do { printf("%d\n", i); i++; /* increment i */ while(i <= 10); return 0; Senem Kumova Metin & Ilker Korkmaz 13

14 Press any key to continue... Senem Kumova Metin & Ilker Korkmaz 14

15 from Figure 6-13 in Forouzan & Gilberg, p. 321

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

17 from Figure 6-13 in Forouzan & Gilberg, p

18 for (initialize; check; update) { statements; /* loop body */ Senem Kumova Metin & Ilker Korkmaz 18

19 from Figure 6-14 in Forouzan & Gilberg, p

20 #include <stdio.h> int main() { int i = 0; while(i <= 10) { printf("%d\n",i); i++; return 0; The while loop #include <stdio.h> int main() { int i; for(i = 0; i <= 10;i++) { printf("%d\n", i); printf("%d\n",i); return 0; The for loop Senem Kumova Metin & Ilker Korkmaz 20

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

22 #include <stdio.h> int main(){ int i; for (i=0 ; ; i++) { // while(1) printf("%d\n", i); return 0; Senem Kumova Metin & Ilker Korkmaz 22

23 expression1, expression2 e, expression3 Expression 1 is evaluated at first, then expression2 is evaluated for(sum=0, i=1; i<=n; ++i) { sum+=i; can be written as for(sum=0, i=1; i<=n; sum+=i, ++i) but not as for(sum=0, i=1; i<=n; ++i, sum+=i) Senem Kumova Metin & Ilker Korkmaz 23

24 We can use the break statement to preemptively end a loop (ending a for loop early, for example, based on some nested if structure). However, this is poor program design and NOT recommended. 24

25 Break causes an exit from the innermost enclosing loop or switch statement. double x; while(1) { // an infinite loop scanf( %lf,&x); if(x<0.0) break; // exit loop if x is negative printf( %f\n,sqrt(x)); // break jumps to here Senem Kumova Metin & Ilker Korkmaz 25

26 #include <stdio.h> int main(){ int i; for(i = 0; i < 10000; i++) { if(getchar() == 's') break; printf("%d\n", i); return 0; Senem Kumova Metin & Ilker Korkmaz 26

27 from Program 6-16 in Forouzan & Gilberg, p

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

29 Continue by-passes the related loop iteration. double x; while(1) { // an infinite loop scanf( %lf,&x); if(x<0.0) continue ; //by-pass below statement printf( %f\n,sqrt(x)); Senem Kumova Metin & Ilker Korkmaz 29

30 from Program 6-18 in Forouzan & Gilberg, p

31 goto keyword is followed by a label, which is basically some identifier placed elsewhere in the program Do not use goto in your programs! Senem Kumova Metin & Ilker Korkmaz 31

32 int main() { int attempt, number = 46; looptag: /* a label */ printf("guess a number from 0-100\n"); scanf("%d", &attempt); if (number == attempt) { printf("you guessed correctly!\n"); else { printf("let me ask again...\n"); goto looptag; /* Jump to the label*/ return 0; Senem Kumova Metin & Ilker Korkmaz 32

33 33

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

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

More information

MIT Aurangabad FE Computer Engineering

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

More information

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

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

More information

Control structures: Conditionals

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

More information

C Programming Lecture IV

C Programming Lecture IV C Programming Lecture IV Instructor Özgür ZEYDAN http://cevre.beun.edu.tr/ break and continue Statements break statement is used to terminate any type of loop such as while loop, do while loop and for

More information

Two-way selection. Branching and Looping

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.

More information

C AND C++ PROGRAMMING

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

More information

C Control Structures. Chapter 13

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

More information

14 MAKING DECISIONS IN C

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

More information

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

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

More information

COMPSCI 210 Part II Decisions & Iterations

COMPSCI 210 Part II Decisions & Iterations 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

More information

Solutions to Assessment: Basic Programming Constructs

Solutions to Assessment: Basic Programming Constructs 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

More information

Thinking in C. Darin Brezeale. March 25, 2010

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

More information

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

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

More information

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

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

More information

Programming and Data Structures

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

More information

Printing (to terminal): printf

Printing (to terminal): printf Printing (to terminal): printf general syntax: printf("a har: %, a new line:\n", val); printf("an int: %d, a tab:\t", ival); printf("a long: %ld, a vertial tab:\v", lval); printf("a float: %f, another

More information

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

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)

More information

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

More information

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

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

More information

MGM s JNEC Question Bank Subject: Computer Engineering

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

More information

Introduction to Programming Block Tutorial C/C++

Introduction to Programming Block Tutorial C/C++ Michael Bader Master s Program Computational Science and Engineering C/C++ Tutorial Overview From Maple to C Variables, Operators, Statements Functions: declaration, definition, parameters Arrays and Pointers

More information

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

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

More information

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

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

More information

C Programming Dr. Hasan Demirel

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

More information

Going from Python to C

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,

More information

Organization of Programming Languages CS3200 / 5200N. Lecture 08

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

More information

PROGRAMMING IN C AND DATA STRUCTURES

PROGRAMMING IN C AND DATA STRUCTURES Code: 14PCD13/23 IA Marks: 25 Hrs/Week: 04 Exam Hrs: 03 Total Hrs: 52 Exam Marks:100 Objectives: The objectives of this course is to make students to learn basic principles of Problem solving, implementing

More information

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

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

More information

Tutorial No. 8 - Solution (Strings and User Defined Functions)

Tutorial No. 8 - Solution (Strings and User Defined Functions) Tutorial No. 8 - Solution (Strings and User Defined Functions) 1. Explain strcat( ), strcpy( ),strncmp( ) and strstr( ) string manipulation function. [6] strcat() Function: The strcat function joins two

More information

Chapter 8. Statement-Level Control Structures

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

More information

6. Control Structures

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,

More information

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

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

More information

Punctuation in C. Identifiers and Expressions. Identifiers. Variables. Keywords. Identifier Examples

Punctuation in C. Identifiers and Expressions. Identifiers. Variables. Keywords. Identifier Examples Identifiers and Expressions CSE 130: Introduction to C Programming Spring 2005 Punctuation in C Statements are terminated with a ; Groups of statements are enclosed by curly braces: { and } Commas separate

More information

CSI 333 Lecture 2 Introduction to C: Part I 2 1 / 16

CSI 333 Lecture 2 Introduction to C: Part I 2 1 / 16 CSI 333 Lecture 2 Introduction to C: Part I 2 1 / 16 Basics of C Remark: Skim Chapters 1 through 6 of Deitel & Deitel. You will notice the following: C is (more or less) a subset of Java. (So, you are

More information

1. Constants. 2. Variables. 3. Reserved words or key words. 4. Constants. Character set in C

1. Constants. 2. Variables. 3. Reserved words or key words. 4. Constants. Character set in C Character set in C We should use only the following characters in writing a C program. These characters can be combined to create C words. Alphabet: A, B, C, D.. Z, a, b, c, d..z Numeric digits: 0, 1,

More information

Engineering Problem Solving with C++, Etter/Ingber

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

More information

The Queue Data Structure in C++ By Eric Suh

The Queue Data Structure in C++ By Eric Suh 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

More information

Looping Exercises Deciding Which Loop to Use

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.

More information

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

More information

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

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

More information

Control structures in C

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

More information

6.1 Branching (or Selection)

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

More information

Computer Problem Solving using C

Computer Problem Solving using C File No: 2 Computer Problem Solving using C Programming Loops Loops cause program to execute the certain block of code repeatedly until test condition is false. Loops are used in performing repetitive

More information

Problem Solving With C++

Problem Solving With C++ Dept. of Geomatics Eng. University of Tehran Cell: 0912 132 5823 Email: marajabi@ut.ac.ir http://www.marajabi.com Unfinished Business Casts (force an expression to be of a specific type) #include

More information

Chapter 4 C Program Control

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.

More information

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

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

More information

PE1 Worksheet. 3) What are the three control structures for writing an algorithm in pseudocode?

PE1 Worksheet. 3) What are the three control structures for writing an algorithm in pseudocode? 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

More information

A Linked List Example

A Linked List Example A Linked List Example http://gd.tuwien.ac.at/languages/c/programming-bbrown/c_094.htm /* linked list example */ #include #include #include #include #include

More information

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

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

More information

switch Multiple-Selection Statement

switch Multiple-Selection Statement 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,

More information

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

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

More information

6.087 Lecture 3 January 13, 2010

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:

More information

Lecture P1: Introduction to C

Lecture P1: Introduction to C 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

More information

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

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,

More information

CMSC 106 Introduction to C Programming

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

More information

Which of the following is the correct operator to compare two variables? A. := B. = C. equal D. ==

Which of the following is the correct operator to compare two variables? A. := B. = C. equal D. == 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

More information

The main features of First Generation are:

The main features of First Generation are: MODEL SOLUTION AS -4016 B.Te ch (First Semester) course A INTRODUCTION TO COMPUTER PROGRAMMING ANS 1 : i. Electrically Erasable Programmable Read Only Memory ii. Four Generation iii. c iv. 8 v. High Level

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

Introduction to C Programming S Y STEMS

Introduction to C Programming S Y STEMS 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

More information

Python while loops. Girls Programming Network School of Information Technologies University of Sydney. Mini-lecture 6

Python while loops. Girls Programming Network School of Information Technologies University of Sydney. Mini-lecture 6 Python while loops Girls Programming Network School of Information Technologies University of Sydney Mini-lecture 6 Loops while loops Summary 2 Outline 1 Loops 2 while loops 3 Summary Loops while loops

More information

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

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

More information

Programming for MSc Part I

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.

More information

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

More information

Computer Programming. Loops(2) do-while, for

Computer Programming. Loops(2) do-while, for Computer Programming Loops(2) do-while, for Topics Repeating Statements (review) while loop (review) do-while loop for loop Infinite loops Examples Repeating Statements In many cases same operations are

More information

Model Answer for Basic Programming in C

Model Answer for Basic Programming in C 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

More information

Lecture 2 Notes: Flow of Control

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

More information

Chapter 8 Statement-Level Control Structures Chapter 8 Topics

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

More information

Chapter 1 Introduction to The C Language

Chapter 1 Introduction to The C Language Chapter 1 Introduction to The C Language 1.1 Introduction 1.6 Machine Languages, Assembly Languages and High-level Languages 1.7 The History of C 1.8 The C Standard Library 1.9 The Key Software Trend:

More information

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

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

More information

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

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

More information

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

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

More information

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

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

More information

Reading. C Programming Language. Basic syntax Whitespaces. Whitespaces (cont d) #include. Basic syntax Comments

Reading. C Programming Language. Basic syntax Whitespaces. Whitespaces (cont d) #include. Basic syntax Comments Reading C Programming Language Types, operators, expressions Control flow, functions Basic IO K&R Chapter 2 Types, Operators, and Expressions K&R Chapter 3 Control Flow K&R Chapter 7 Basic I/O NEWS Assignment

More information

If the else statement is used with a for loop, the else statement is executed when the loop has exhausted iterating the list.

If the else statement is used with a for loop, the else statement is executed when the loop has exhausted iterating the list. Loops in Python For Loop If the else statement is used with a for loop, the else statement is executed when the loop has exhausted iterating the list. The for loop in Python has the ability to iterate

More information

VB.NET Programming Fundamentals

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

More information

Oxford University Computing Services. Programming in C

Oxford University Computing Services. Programming in C 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

More information

Java iteration statements

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

More information

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

More information

Informatica e Sistemi in Tempo Reale

Informatica e Sistemi in Tempo Reale Informatica e Sistemi in Tempo Reale Introduction to C programming Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa October 25, 2010 G. Lipari (Scuola Superiore Sant Anna)

More information

Week 4 Assessment. Q : Consider the following code segment. Assume n to be a positive integer.

Week 4 Assessment. Q : Consider the following code segment. Assume n to be a positive integer. Week 4 Assessment Question 1 : Q : Consider the following code segment. Assume n to be a positive integer. for(i=1; i

More information

C Programming, Chapter 1: C vs. Java, Types, Reading and Writing

C Programming, Chapter 1: C vs. Java, Types, Reading and Writing C Programming, Chapter 1: C vs. Java, Types, Reading and Writing T. Karvi August 2013 T. Karvi C Programming, Chapter 1: C vs. Java, Types, Reading and Writing August 2013 1 / 1 C and Java I Although the

More information

Introduction to Programming in C++: Algorithms, Flowcharts and Pseudocode

Introduction to Programming in C++: Algorithms, Flowcharts and Pseudocode 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

More information

Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Third Edition

Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Third Edition Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition Objectives In this chapter, you will learn about: Representing algorithms Examples of algorithmic problem

More information

IDL Lab #5: The Other IDL Statements

IDL Lab #5: The Other IDL Statements IDL Lab #5: The Other IDL Statements Name: IDL Lab #5: A Sub-component of FOR 504 Advanced Topics in Remote Sensing The objectives of this laboratory exercise are to introduce the student to The other

More information

BSc (Hons) Computer Science with Network Security, BSc (Hons) Business Information Systems, BSc (Hons) Mathematics & BSc (Hons) Software Engineering

BSc (Hons) Computer Science with Network Security, BSc (Hons) Business Information Systems, BSc (Hons) Mathematics & BSc (Hons) Software Engineering BSc (Hons) Computer Science with Network Security, BSc (Hons) Business Information Systems, BSc (Hons) Mathematics & BSc (Hons) Software Engineering Cohort: BCNS/07/FT- BIS/07/FT- BIS/06/FT -BM/07/FT BSE/07/FT/PT-

More information

Counting with While Loops

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

More information

JavaScript: Control Statements II

JavaScript: Control Statements II JavaScript: Control Statements II 6.2 Essentials of Counter-Controlled Repetition 6.3 for Repetition Statement 6.4 Examples Using the for Statement 6.5 switch Multiple-Selection Statement 6.6 do while

More information

C Control Statements : Branching and jumps

C Control Statements : Branching and jumps C Control Statements : Branching and jumps Using Bloodshed Dev-C++ Heejin Park Hanyang University 2 Introduction The if Statement Adding else to the if Statement Let's Get Logical A Word-Count Program

More information

Object Oriented Software Design

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

More information

Three numbers form a Pythagorean triple if the sum of squares of two numbers is equal to the square of the third.

Three numbers form a Pythagorean triple if the sum of squares of two numbers is equal to the square of the third. 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

More information

5 Let s all move one place on. Lewis Carroll

5 Let s all move one place on. Lewis Carroll 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

More information

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

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

More information

A Rudimentary Intro to C programming

A Rudimentary Intro to C programming A Rudimentary Intro to C programming Wayne Goddard School of Computing, Clemson University, 2008 Part 1: Variables and Data-types 1 Getting Started............................... A1 2 Some Basics.................................

More information

Statement-Level Control Structures

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

More information

Introduction to Computer Science. Programming in C. Dr inŝ. Lidia Jackowska - Strumiłło. Computer Engineering Department TECHNICAL UNIVERSITY OF LODZ

Introduction to Computer Science. Programming in C. Dr inŝ. Lidia Jackowska - Strumiłło. Computer Engineering Department TECHNICAL UNIVERSITY OF LODZ Introduction to Computer Science Programming in C Dr inŝ. Lidia Jackowska - Strumiłło Computer Engineering Department TECHNICAL UNIVERSITY OF LODZ C language features C is a very effective programming

More information

Chapter 4 C Program Control

Chapter 4 C Program Control Chapter 4 C Program Control 1 Outline 4.1 Introduction 4.2 The Essentials of Repetition 4.3 Counter-Controlled Repetition 4.4 The for Repetition Statement 4.5 The for Statement: Notes and Observations

More information

printf() Standard Library Function printf()

printf() Standard Library Function printf() printf() Standard Library Function Used to write text to the "standard output" Normally a computer monitor or printer Often the UART in embedded systems SIM Uart1 window in MPLAB SIM printf() Standard

More information

Java Review (Essentials of Java for Hadoop)

Java Review (Essentials of Java for Hadoop) 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.

More information

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

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

More information