The following statements are for y = -1; if ( x ) if ( x>0 ) y = 1; else y = 0; A. y= -1 x<0 y= 0 x=0 y= 1 x>0 B. y= 0 x<0 y= -1 x=0 y= 1 x>0 C. y= 1 x<0.

Slides:



Advertisements
Similar presentations
Computer programming Lecture 3. Lecture 3: Outline Program Looping [Kochan – chap.5] –The for Statement –Relational Operators –Nested for Loops –Increment.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Chapter 5: Control Structures II (Repetition)
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
Chapter 5: Control Structures II (Repetition)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 2: Control Flow.
Repetition (Loops) Want to do some repetitive sequence of actions: print vertical line of *s * Corresponding program: printf(“*\n”);
Introduction to Computer Programming in c
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
Chapter 5: Control Structures II (Repetition)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 5: Structured Programming.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
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.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
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.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 5: Structured Programming
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Spring 2005, Gülcihan Özdemir Dağ Lecture 5, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 5 Outline 5.0 Revisiting.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Chapter 4: Control Structures II
Chapter 5: Control Structures II
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
CISC105 – General Computer Science Class 4 – 06/14/2006.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Engineering Computing I Chapter 3 Control Flow. Chapter 3 - Control Flow The control-flow of a language specify the order in which computations are performed.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Chapter 4 – C Program Control
CSE 220 – C Programming Loops.
REPETITION CONTROL STRUCTURE
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Quick Test What do you mean by pre-test and post-test loops in C?
Control Structures Lecture 7.
INC 161 , CPE 100 Computer Programming
TOPIC 4: REPETITION CONTROL STRUCTURE
- Additional C Statements
Chapter 6 Decision Making and Looping
Chapter 4 - Program Control
Loops in C.
Computer programming Lecture 3.
More Loops Topics Counter-Controlled (Definite) Repetition
Chapter 4 - Program Control
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

The following statements are for y = -1; if ( x ) if ( x>0 ) y = 1; else y = 0; A. y= -1 x<0 y= 0 x=0 y= 1 x>0 B. y= 0 x<0 y= -1 x=0 y= 1 x>0 C. y= 1 x<0 y= -1 x=0 y= 0 x>0 D. y= -1 x<0 y= 1 x=0 y= 0 x>0

What is the output of the following statements int x=10, y=20, z=30; if ( x > y ) z = x; x = y; y = z; A.10,20,30 B.20,30,30 C.20,30,10 D.20,30,20

What is the function of the following statements main() { int x, y; printf (“Input x,y:”); scanf(“%d %d”,&x,&y); if( x % y ) printf (“No\n”); else printf (“Yes\n”); }

Chapter6 Decision Making and Looping Outline 6.1Introduction 6.2 The while Statement 6.3The do Statement 6.4The for Statement 6.5 Jumps in Loops

Objectives ♣To master the use of while, for, do loops. ♣To be able to choose the correct type of loop for a given problem. ♣To be able to construct nested loops. ♣To master the statements of break and continue ♣To master some algorithms

6.1 Introduction sum = 0; n = 1; loop: sum = sum + n*n; if ( n = =10 ) goto print; else { n = n+1; goto loop; } print: printf (“sum=%d”,sum);

6.1 Introduction ♣Two segments of a loop ♥body of the loop ♥control statement ♣Depending on the position of the control statement in the loop, a control structure may be classified ♥entry-controlled loop ♥exit-controlled loop

flow chart of Entry control loop False True test condition ? Body of the loop Entry

Flow chart of exit control loop Entry False True test condition ? Body of the loop

6.1 Introduction ♣ A looping process, in general, would include the following four steps: ♥ (1) Setting and initialization of a condition variable. ♥ (2) Execution of the statements in the loop. ♥ (3) Test for a specified value of the condition variable for execution of the loop. ♥ (4) Incrementing or updating the condition variable.

6.1 Introduction ♣Counter-controlled loops: ♥We know in advance exactly how many times the loop will be executed. ♥We use a counter as control variable. The counter must be initialized before loop, tested and updated within the loop. ♣Sentinel-controlled loops ♥A special value called sentinel value is used to change the loop control expression from true to false.

6.1 Introduction ♣ The C language provides for three constructs for performing loop operations. They are: ♣ (1) The while statement. ♣ (2) The do statement. ♣ (3) The for statement.

6.2 The while statement ♣The basic format of the while statement is while ( test condition) { body of the loop } ♣The while is an entry-controlled loop statement.

6.2 The while statement ♣Evaluate the sum of the square of 1 to 10 with while statement ♥flow chart ♥program ♣Input a character from the keyboard until the character is equal to ‘Y’ character=‘ ‘; /* character=getchar(); */ while (character != ‘Y’) { character=getchar(); putchar(character); }

♣The segment is equivalent the following statements while ((character=getchar()) != ‘Y’) { putchar(character); } character=‘ ‘; /* character=getchar(); */ while (character != ‘Y’) { character=getchar(); putchar(character); }

A program to evaluate the equation y = x n ♥counter loop or sentinel loop? ♥How many times will the loop be executed? ♥Initialization the control variable. ♥Initialization y ♥Test control variable ♥Update control variable ♥Flow chart Example 6.1

What is the output of the following program #include int main( ) { int n=0; while( n++ < =3) printf (″%d\t″,n); return 0; }

What is the output of the following program #include int main( ) { int n=0; while( n < =3) printf (″%d\t″,n); n++; return 0; }

What is the output of the following program if we input #include main() { int n,s = 0; scanf("%d",&n); while(n) { s += n % 10; n /= 10; } printf("s=%d\n",s); }

What is the output of the following program if we input abcde? #include int main( ) { char c; int s=0; c=' '; while (c!='?') { c=getchar(); putchar(c); s++; } printf("\ns=%d\n",s); return 0; }

6.3 The do statement ♣The do statement takes the form: ♣The do statement construct provides an exit- controlled loop and therefore the body of the loop is always executed at least once. do { body of the loop } while ( test condition); Entry False True test condition ? Body of the loop

6.3 The do statement ♣Rewrite the program that evaluates the sum of the square of 1 to 10 with do-while statement ♣Input a character from the keyboard until the character is equal to ‘Y’

6.4 The for statement ♣The for loop is another entry-controlled loop that provides a more concise loop control structure. The general form of the for loop is ♣The execution of the for statement for ( initialization; test-condition; increment ) { body of the loop }

Flowchart False True Test- condition Body of the loop Initialization of the control variables increment Next statement for ( initialization; test-condition; increment ) { body of the loop } The execution of the for statement only once

6.4 The for statement ♣Rewrite the program of sum of the square of 1 to 10 with for statement

for(x=2;i<=13; x+=2) { printf(“%d\n”,x); } for(x=5;i<=22; x+=7) printf(“%d\n”,x); for(x=6;i<=5; x+=7) printf(“%d\n”,x);

6.4 The for statement ♣The for statement and its equivalent of while and do statements are shown below. forwhiledo for( n=1;n<=10;++n) { …… } n = 1; while( n<=0 ) { …… n=n+1; } n = 1; do { …… n=n+1; }while(n<=10);

6.4 The for statement ♣Print the “powers of 2” table for the power 0 to 20, both positive and negative. ♣We will print ♥p=2 n ♥q=2 -n = 1/p ♥consider data type of p and q ♠p: long int ♠q: double ♥print the header of table ♥use for loop to program Example 6.3

6.4 The for statement ♣The initialization and increment section may have more than one part. For example, for( n=1, m=50 ; n<=m; n=n+1, m=m-1) { p=m/n; printf(“%d %d %d\n”, n, m, p); }

6.4 The for statement ♣One or more sections in for loop can be omitted, if necessary. For example, ♣Note, the semicolons separating the sections must remain. m = 5; for( ; m!=100 ; ) { printf(“%d\n”, m); m = m + 5; }

Nesting of loops ♣A nested loop is one loop inside another loop. for( ; ; ) { …… while( ) { …… } …… } do { …… while( ) { …… } …… }while( ); for( ; ; ) { …… for( ; ; ) { …… } …… }

Nesting of loops CorrectWrong

Nesting of loops ♣A common use for nested loops is to display data in rows and columns. ♣One loop can handle, say, all the columns in a row, and the second loop handles the rows.

main( ) { int i,j; for ( i=0 ; i<3 ; i++) { printf(“i= %d:”,i); for( j=0 ;j<4 ; j++) printf(“j=%-4d”,j ); printf(“\n”); } }

♣A program multiplication table from 1*1 to 12*10 Example 6.2

♣A class of n students take an annual examination in m subjects. Write a program to read the marks obtained by each student in various subjects and to computer and print the total marks obtained by each of them. ♣Input m, n ♣Reading m marks of ith student ( 1<=i <=n) ♣Evaluate the total of ith student ♣print the total of ith student Example 6.4

T F Input n,m i <= n begin i =1 Evaluate total of ith student input m marks of ith student end print total of ith student i = i+1 Flowchart

main() { int m, n,i, j ; float mark, total; printf(“ Input number of student\n”); scanf(“%d”,&n); pritnf(“Input number of subject\n”); scanf(“%d”,&m); for(i =1 ; i<=n; i++) { /* input m marks of ith student evaluate total of ith student */ printf(“total = %f\n”,total); } }

input m marks of ith student T j=1 j <= m j++ input mark of jth subject F next statement

Evaluate sum of ith student T total=0 j <= m j++ total = total + mark F next statement

input m marks of ith student, evaluate total of ith student for(j=1;j<=m;j++) { printf(“Please input mark of %d subject:”, j); scanf(“%f”,&mark); total += mark; }

Selecting a loop ♣Analyze the problem and see whether it required a pre-test or post-test loop. ♣If it requires a post-test loop, then we can use only one loop, do while. ♣If it requires a pre-test loop, then we have two choices: for and while. ♣Decide whether the loop termination requires counter- based control or sentinel-based control. ♣Use for loop if the counter-based control is necessary. ♣Use while loop if the sentinel-based control is required. ♣Note that both the counter-controlled and sentinel- controlled loops can be implemented by all the three control structures.

What would be the function of the following segments? sum=0; for(count=1; count<=100; count++) { scanf(“%d”,&number); sum += number; if(sum>=3000) count=100; }

What would be the output of the following segments? main() { int k=0 ; char c=‘B' ; switch(c) { case 'A': k++ ; break ; case 'B': k-- ; case 'C': k+=2 ; break ; case 'D': k=k%2 ; break ; default: k=k/3 ; } printf("k=%d\n",k) ; }

What would be the output of the following segments? main() { int k=0 ; char c=‘B' ; switch(c) { case 'A': k++ ; break ; case 'B': k-- ; case 'C': k+=2 ; break ; case 'D': k=k%2 ; break ; default: k=k/3 ; } printf("k=%d\n",k) ; } exit from switch.

6.5 Jumps in Loops ♣The break statement provides an early exit from for, while, and do, just as from switch. ♣When a break statement is encountered inside a loop, the loop is immediately exited and the program continues with the statement immediately following the loop. ♣When the loops are nested, the break would only exit from the loop containing it.

6.5 Jumps in Loops break True False True test condition1? statements1 Entry test condition2 False statements2

What is the output of the following statements for (i=1; i<11; i++) { if(i%3==0) break; printf("%d",i); }

♣Write a program to determine whether a given number is a prime number. Example

T F T F begin input m i=2 i<=sqrt(m) i++ m%i m is a primem is not a prime end

T F i>sqrt(m) T F F T begin input m i=2 i<=sqrt(m) i++ m%i m is a primem is not a prime end

#include main() { int m, i; printf ("Enter a number:"); scanf ("%d",&m); for(i=2;i<=sqrt(m);i++) { if(m%i==0) break; } if(i>sqrt(m)) printf("%d is a prime number\n",m); else printf("%d is not prime number.\n",m); }

#include main() { int m, i, flag=1; printf ("Enter a number:"); scanf ("%d",&m); for (i=2;i<=sqrt(m);i++) { if(m%i==0) { flag=0; break; } if(flag) printf("%d is a prime number\n",m); else printf("%d is not prime number.\n",m); }

Skipping a Part of a Loop ♣Like the break statement, C supports another similar statement called the continue statement. ♣The continue tells the compiler, “skip the following statements and continue with the next iteration”.

Skipping a Part of a Loop continue True False True test condition1? statements Entry test condition2 False statements

while (……) { …… if ( condition ) continue; …… } ……

for (i=1;i<=100; i++) { if (i%5==0) continue; printf(“% d ”,i); }

#include main() { int i, n; for ( i=1 ; i <=5 ; i++ ) { printf(“Please enter n:”); scanf(“%d”,&n); if(n<0) break; printf(“n=%d\n”,n); } printf(“Program is over!\n”); } What is the function of the following statements

#include main() { int i, n; for ( i=1 ; i <=5 ; i++ ) { printf(“Please enter n:”); scanf(“%d”,&n); if(n<0) continue; printf(“n=%d\n”,n); } printf(“Program is over!\n”); } What is the function of the following statements

♣The program evaluates the square root of numbers and prints the results. The process stops when the number 9999 is typed in. If the number is a negative, the program prints a message saying that the number is negative and count the number of negative. ♣The final output includes the number of positive values evaluated and the number of negative items. Example 6.7

#include main() { int count,negative; double number,sqroot; printf("Enter 9999 to stop\n"); count=0; negative=0; while(count<=100) { printf("Enter a number: "); scanf("%lf",&number); if(number==9999) break; if(number<0) { printf("Number is negative\n\n"); negative++; continue;/* skip rest of the loop */ } sqroot=sqrt(number); printf("Number=%lf\n Square root=%lf\n\n",number,sqroot); count++; } printf("Number of items done=%d\n",count); printf("\n\nNegative items=%d\n",negative); printf("End of data\n"); }

Just Remember ♣Read “just remember” on page 167

Case Study ♣Table of binomial coefficients 二项式系数 ♣Histogram 柱状图 ♣Minimum Cost 最小代价 ♣Plotting of two functions 两个函数图

Assignment ♣Programming Exercises ♥6.1 ♥6.2 ♥6.4 ♥6.8 ♥6.10 ♥6.14 (a) or (b) or (c)