17.1.2001Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 7.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Computer programming Lecture 3. Lecture 3: Outline Program Looping [Kochan – chap.5] –The for Statement –Relational Operators –Nested for Loops –Increment.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Switch Statement switch (month) { case 9: case 4: case 6: case 11: days = 30; break; case 2: days = 28; if (year % 4 == 0) days = 29; break; default: days.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Chapter 5: Control Structures II (Repetition)
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
C Programming Lecture 12. The Compound Statement b A compound statement is a series of declarations and statements surrounded by braces. b A compound.
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Programming with C# Iteration LECTURE 3. Summary of last lecture SequenceSelectionif and switch statementsCastingRandom.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
do - while  while: Execute the body of the loop at least once
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 5.
Nested LOOPS.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
Control Structures Repetition or Iteration or Looping Part II.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Iterations Very Useful: Ability to repeat a block of code Example:
ECE 103 Engineering Programming Chapter 18 Iteration Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material developed.
Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture
Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
COMP Loop Statements Yi Hong May 21, 2015.
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.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Problem Solving and Program Design in C Chap. 5 Repetition and Loop Statement Chow-Sing Lin.
Computer C programming Chapter 3. CHAPTER 3 Program Looping –The for Statement –Nested for Loops –for Loop Variants –The while Statement –The do Statement.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
CHAPTER 4 REPETITION STRUCTURES 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 A.AlOsaimi.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
Computer Programming -1-
REPETITION CONTROL STRUCTURE
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Lecture 6 Repetition Richard Gesick.
Lecture 7: Repeating a Known Number of Times
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Chapter 2.2 Control Structures (Iteration)
Looping.
Lecture 4A Repetition Richard Gesick.
Chapter 13 Control Structures
CS1100 Computational Engineering
While Loop Design ENGI 1020 Fall 2018.
Chapter 6 Decision Making and Looping
Loops in C.
Iteration: Beyond the Basic PERFORM
REPETITION STATEMENTS
More Loops Topics Counter-Controlled (Definite) Repetition
ECE 103 Engineering Programming Chapter 18 Iteration
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 7

Sudeshna Sarkar, IIT Kharagpur 2 main () { int sum=0; int input, inner, outer; printf(“Input an integer : “); scanf (“%d”, &input) ; for (outer=1; outer <= input; outer++) for (inner=0; inner < outer; inner++) sum += inner; printf (“The result is %d\n”, sum) ; }

Sudeshna Sarkar, IIT Kharagpur 3 Some Loop Pitfalls while (sum <= NUM) ; sum = sum+2; for (i=0; i<=NUM; i++); sum = sum+i; for (i=1; i!=10; i=i+2) sum = sum+i; double x; for (x=0.0; x<10.0; x=x+0.2) printf(“%.18f”, x);

Sudeshna Sarkar, IIT Kharagpur 4 Doubles and floats What you expect: What you may get:

Sudeshna Sarkar, IIT Kharagpur 5 Use ints as loop counters int i; double x; for (i=0; i<50; i=i+1) { x = (double)i/5.0; printf (“%.18f”, x); }

Sudeshna Sarkar, IIT Kharagpur 6 Iteration Summary General Pattern : initialize test do stuff update go back to re-test, re-do stuff, re-update,... while and for are equally general in C use for when initialize/test/update are simple, especially when counting.

Sudeshna Sarkar, IIT Kharagpur 7 Event Driven Programming General Pattern : Program starts, sets itself up. Waits for some event or command to happen mouse click, key click, timer, menu selection etc. Program performs operation (“handles” the command) Program goes back to waiting.

Sudeshna Sarkar, IIT Kharagpur 8 Simple Command Interpreter Read in “commands” and execute them. Input - single characters a - execute command Add by calling Add() s - execute command Sub by calling Sub() q - quit Pseudocode for main loop: get next command if a, execute command Add() if b, execute command Sub() if q, signal quit

Sudeshna Sarkar, IIT Kharagpur 9 Command Interpreter Loop Control repeat until quit signal use variable “done” to indicate when done set done to false while not done body statements if quit command, set done to true

Sudeshna Sarkar, IIT Kharagpur 10 Command Interpreter program #define FALSE 0 #define TRUE 1 int main (void) { char command; int done = FALSE; while (!done) { printf (“Input command:”); scanf(“%c”,&command); switch (command) { case ‘A’: case ‘a’: Add(); break; case ‘S’: case ‘s’: Sub(); break; case ‘Q’: case ‘q’: done=TRUE; }

Sudeshna Sarkar, IIT Kharagpur 11 Exercise a Write a C program which accepts as input a single integer k, then writes a pattern consisting of a single 1 on the first line, two 2s on the 2nd line, three 3s on the 3rd line, until it writes k occurrences of k on the last line. For example, if the input is 4, the output should be:

Sudeshna Sarkar, IIT Kharagpur 12 Exercise b Write a C program which accepts as input a single integer k, then generates the following pattern of k lines: For example, if the input is 5, the output should be:

Sudeshna Sarkar, IIT Kharagpur 13 Test if a number is prime prime = 1; for (i=2; i<num; i++) { if (num%i == 0) prime=0; } if (prime == 1) printf (“%d” is a prime number\n”);

Sudeshna Sarkar, IIT Kharagpur 14 Test if a number is prime prime = 1; limit = sqrt ((double)num); for (i=2; i<limit; i++) { if (num%i == 0) { prime=0; break; } if (prime == 1) printf (“%d” is a prime number\n”);

Sudeshna Sarkar, IIT Kharagpur 15 Break and continue These two statements are used in loop control “break” exits the innermost current loop (for, while, do-while) and to exit from a switch Control will be transferred out of the loop “continue” starts the next iteration of the loop (for, while, do-while) used to bypass the remainder of the current pass through a loop

Sudeshna Sarkar, IIT Kharagpur 16 do { scanf (“%f”, &x); if (x<0) { printf(“Error, neg x”); break; }... /*process non-neg x */ } while (x<=100); for (count=0;count<n;count++) {... while ((c=getchar()) != ‘\n’) { if (c==‘*’) break;... }

Sudeshna Sarkar, IIT Kharagpur 17 do { scanf (“%f”, &x); if (x<0) { printf(“Neg value forx”); continue; }... /*process non-neg x */ } while (x<=100);

Sudeshna Sarkar, IIT Kharagpur 18 Ex: Write a loop that will calculate the sum of an AP series upto n terms Sum= a + (a+d) +(a+2d) (a+ (n-1)d) sum = a; for (i=1; i<n; i++) { sum = sum +a+ i*d; } printf (‘%d”, sum); sum = a; term = a; for (i=1; i<n; i++) { term = term + d; sum = sum + term; } printf (‘%d”, sum);

Sudeshna Sarkar, IIT Kharagpur 19 Exercise c Write a C program that takes as input a positive integer n, and prints all prime numbers between 2 and n.

Sudeshna Sarkar, IIT Kharagpur 20 Exercise d Write a C program that calculates the sum of the first n odd numbers: *n-1

Sudeshna Sarkar, IIT Kharagpur 21 The sine of x can be calculated approximately by summin the first n terms of the infinite series: sin x = x - x 3 /3! + x 5 /5! – x 7 /7! +... where x is expressed in radians (  radians = 180 degrees). Write a C program that will read in a value for x and will calculate its sine. (i) sum the first n terms (ii)continue adding successive terms till the value of the next term becomes smaller (in magnitude) than 10 -5

Sudeshna Sarkar, IIT Kharagpur 22 scanf (“%f”, &x); x = x*PI/180.0; sineval = x; term = x; for (i=1; i<n; i++) { term = (-1)*term*x*x/(2*i*(2*i+1)); sineval = sineval + term; }

Sudeshna Sarkar, IIT Kharagpur 23 scanf (“%f”, &x); x = x*PI/180.0; sineval = x; term = x; for (i=1; term< ; i++) { term = (-1)*term*x*x/(2*i*(2*i+1)); sineval = sineval + term; } printf (“The value of sine is %f to %d terms\n”,sineval, i);