Introduction to C Programming CE00312-1 Lecture 4 Further Control Structures in C.

Slides:



Advertisements
Similar presentations
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
Advertisements

Chapter 5 Repetition and Loop Statements Instructor: Alkar & Demirer.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 CSE1301 Computer Programming Lecture 10: Iteration (Part 1)
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
1 CSE1301 Computer Programming Lecture 10: Iteration (Part 1)
Some loop programs 1: Read in 10 integers and output their sum
09 Non-deterministic iteration1June Non-deterministic iteration CE : Fundamental Programming Techniques.
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
1 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
C programming: Variables, Expressions part II. Data Types of Arithmetic Expressions Relational Expressions Logical Expressions Multiple Assignments Compound.
Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Introduction to Computing Lecture 07: Repetition and Loop Statements (Part.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
1 Flowchart notation and loops Implementation of loops in C –while loops –do-while loops –for loops Auxiliary Statements used inside the loops –break –continue.
CS140: Intro to CS An Overview of Programming in C (part 3) by Erin Chambers.
Count and add list of numbers From user input and from file.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
Introduction to Loops Iteration Repetition Counting Loops Also known as.
BY ILTAF MEHDI (MCS, MCSE, CCNA)1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI (MCS, MCSE, CCNA)2 Chapter No: 04 “Loops”
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
1 CSC103: Introduction to Computer and Programming Lecture No 9.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
Dr. Sajib Datta Sep 8,  char type technically is an integer type  Computer uses numeric codes to represent characters, and store characters.
ECE Application Programming
CSE 220 – C Programming Loops.
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Chapter 4 C Program Control Part I
Lecture 7: Repeating a Known Number of Times
CSE1320 Loop Dr. Sajib Datta
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
CS1010 Programming Methodology
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
CSI 121 Structure Programming Language Lecture 10: Iteration (Part 1)
Control Structures Lecture 7.
Looping.
Arrays, For loop While loop Do while loop
- Additional C Statements
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Chapter 8 The Loops By: Mr. Baha Hanene.
Exam 1 Date: Feb. 2nd, 2015 during class time (50 minutes) Coverage
Loops in C.
Repetition and Loop Statements
Incremental operators
UMBC CMSC 104 – Section 01, Fall 2016
Computer Programming Techniques Semester 1, 1998
Assist.Prof.Dr. Nükhet ÖZBEK Ege University
do/while Selection Structure
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
ECE 103 Engineering Programming Chapter 18 Iteration
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Chap 7. Advanced Control Statements in Java
EECE.2160 ECE Application Programming
More Loops Topics Counter-Controlled (Definite) Repetition
ICS103: Programming in C 5: Repetition and Loop Statements
More Loops Topics Counter-Controlled (Definite) Repetition
Chapter 13 Control Structures
Presentation transcript:

Introduction to C Programming CE Lecture 4 Further Control Structures in C

Program Control - Iteration Executing a series of one or more statements a number of times Also referred to as repetition or looping Non-deterministic No pre-set number of iterations Use While Do While Deterministic Known number of iterations Use For

Non-deterministic Loop: While statement Format while (condition) statement; where the statement can be a compound statement e.g. sum=0; scanf(“%d”,&x); while (x!=-9999) { sum=sum+x; scanf(“%d”,&x); }

Read Ahead Technique Input data: –9999 Unwinding the iteration: Read num 1 Process num Read num4 Process num Read num5 Process num Read num6 Process num Read num-9999

The iterated step is: Process num Read num Which leads to: Read num While num != Process num Read num

Example: While Loop 1. Define Problem Problem: Read a series of integers terminated by –9999. Count the number of integers that are greater than 150 and find the largest and smallest numbers greater than 150.Print a message if none of the integers are greater than 150. Example Input: Expected Output: Number of values>150=2 Max=400 Min=200

2. Design Initialise:- count=0:max=0:min=99999 Read first number Input values and process While number <> loop If number > 150 then Increment count Check if new largest If number>=max then max=number Check if new smallest If number <=min then min=number Read next number End loop Print results If count>0 then Print count, max, min Else Print “No values ”

3. Code #include int main(void) /*program description*/ { int num,max,min,count; count=0; min=99999; max=0; scanf(“%d”, &num); while (num !=-9999) { if (num>150) { ++count; if (num>max) max=num; if (num<min) min=num; } scanf(“%d”,&num); } if (count>0) { printf(“Number of values>150= %d\n”, count); printf(“Max = %d\n”,max); printf(“Min = %d\n”,min); } else printf(“No values over 150”); return 0; }

Non-deterministic loop – Do While loop General Form do statement; while (expression); Unlike the while statement, the expression in the ‘do while’ comes after ‘statement’ (the loop body) which is thus executed at least once. Note that it is very easy to make mistakes with the do while loop, although it may compile and appear to produce valid code.

Deterministic Loop: For statement For statement format for (initialising list; condition; altering list) statement; initialising list: Executed once altering list: Executed at the end of each iteration condition: Evaluated and checked at the start of each iteration i.e. a leading decision

Example:For loop for (count=1;count<=10;count=count+2) printf(“%3d”,count); Gives output: Equivalent While loop: count=1; while (count<=10) { printf(“%3d”,count); count=count+2; }

Increment and Decrement Operators count=count+1; -> ++count; count++; count=count-1; ->--count; count--; count=count+5: -> count+=5; Increment can occur before or after statement actioned count=3; x=++count; == count: 4, x: 4 count=3; x=count++; == count:4, x:3 Similarly with decrement operators.

Using increment and decrement operators in for loops for (count=0;count<=10;++count) printf(”%3d”,count); Prints : for (count=0;count<=10;count++) printf(”%3d”,count); Prints : Classic Error: for (i=0;i<10;++i); printf(“%d”, i);

Example 1 Deterministic Loop Problem: Print a multiplication table of size N. e.g where N= The structure of the data determines the structure of the program

Design Data: Repetition of rows Repetition of values (row*column) Design Read N Perform N times Print a row Perform N times Print a value

Code #include /* Reads an integer N and prints the N times multiplication table */ int main(void) { int row, col, n; printf(“\nPlease enter the value of N: “); scanf(“%d”, &n); printf(“\n\n”); for (row=1;row<=n;row++) { for (col=1;col<=n;++col) printf(“%6d”, row*col); printf(“\n”); } return 0; }

Deterministic Loops – Example 2 Problem: Write a program to encode lower case letters in a message by performing a cyclic shift one place to the right. Example Input: The cat sat on the mat Example Output: Uif dbu tbu po uif nbu

Code #include /* Character processing example */ int main(void) { char c; while (scanf(“%c”, &ch) != EOF) if (ch == ‘z’) printf(“%c”, ‘a’); else if (ch >= ‘a’ && ch <=’y’) printf(“%c”, ch+1); else printf(“%c”, ch); return 0; }

End of File Condition All files have an end of file marker When scanf() attempts to read this marker, it returns –1 The following code copies integers until the end of file is found EOF can also be generated by ctrl-D while (scanf(“%d”, &x) !=-1) printf(“%d”, x); stdio.h contains #define EOF –1 hence: while (scanf(“%d”, &x) !=EOF) printf(“%d”, x); End of file markers avoid having to select a data terminator result in neater code.

Example of buffered input #include int main(void) { int num, sum, count; float mean; sum=0; count=0; while (scanf(“%d”, &num) !=EOF) { sum=sum+num; ++count; } printf(“%f\n”, (float)sum/(float)count); return 0; }

Buffered Input Using scanf() Problem Consider the following segment of code: char ch1, ch2; printf(“\n Input first value: “); scanf(“%c”, &ch1); printf(“\n Input second value: “); scanf(“%c”, &ch2); At run time: Prompt: Input first value User response: Q [RETURN] Program Action: ch1=’Q’, ch2=10 (ASCII [RETURN] = 10) Why?

Solution char ch1, ch2; printf(“\n Input first value: “); scanf(“%c%c”, &ch1, &rtn); printf(“\n Input second value: “); scanf(“%c”, &ch2); NOTE: if ch2 is a non-character type e.g. an integer, then there isn’t a problem. Care must be taken when processing characters. Consider: #define EOLN 10 …… while (scanf(“%c”, &ch1)!=EOLN) printf(“%c”,ch1);