Chapter 6 Decision Making and Looping

Slides:



Advertisements
Similar presentations
Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Algorithm & Flow Charts Decision Making and Looping Presented By Manesh T Course:1090 CS.
Computer programming Lecture 3. Lecture 3: Outline Program Looping [Kochan – chap.5] –The for Statement –Relational Operators –Nested for Loops –Increment.
CS201 - Repetition loops.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
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.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement.
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
Introduction to Computer Programming in c
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Chapter 4 Program Control Statements
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
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 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Repetition Statements while and do while loops
Looping Construct or Statements. Introduction of looping constructs In looping,a sequence of statements are executed until some condition for termination.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Decision Making and Branching (cont.)
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.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
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
Algorithm & Flow Charts Decision Making and Looping
The following statements are for y = -1; if ( x ) if ( x>0 ) y = 1; else y = 0; A. y= -1 x0 B. y= 0 x0 C. y= 1 x
Flow Control. Comments u Comments: /* This is a comment */ –Use them! –Comments should explain: v special cases v the use of functions (parameters, return.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
‘C’ Programming Khalid Jamal.
CSE 220 – C Programming Loops.
Chapter 6: Loops.
REPETITION CONTROL STRUCTURE
生查子 ~ 歐陽修 去年元夜時,花市燈如晝, 月上柳梢頭,人約黃昏後; 今年元夜時,月與燈依舊, 不見去年人,淚濕春衫袖。
C Program Controls + Flow Structure
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 II (Repetition)
Chapter 2.2 Control Structures (Iteration)
Looping.
Loop Control Structure.
MSIS 655 Advanced Business Applications Programming
CS1100 Computational Engineering
Loops in C.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Repetition Control Structure
Program Control Topics While loop For loop Switch statement
Chapter 2.2 Control Structures (Iteration)
Chapter 6 Control Statements: Part 2
Computing Fundamentals
Computer programming Lecture 3.
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
CSC215 Lecture Control Flow.
More Loops Topics Counter-Controlled (Definite) Repetition
Chapter 4: Loops and Iteration
Presentation transcript:

Chapter 6 Decision Making and Looping PROGRAMMING IN ANSI C

Let's find out the law of such question: 12/2/2018 Question Questions: How do we calculate the sum from 1 to 5? How do we calculate the sum from 1 to 100? main() { int sum=0; sum=sum+1; sum=sum+2; sum=sum+3; …… sum=sum+100; printf("sum=%d",sum); } main() { int sum=0; sum=sum+1; sum=sum+2; sum=sum+3; sum=sum+4; sum=sum+5; printf("sum=%d",sum); } Let's find out the law of such question: When i=1,2,3,…,100 do: sum = sum + i; Loop construct!

12/2/2018 Flow chart In a looping, a sequence of statements is repetitively executed until the loop test condition can’t be satisfied. A loop statement consists of 2 segments: The loop control statement The loop body end begin i=1, sum=0; i<=100 Y N i=i+1; output: sum sum = sum+i;

Chapter 6 In this chapter, we will learn: while statement 12/2/2018 Chapter 6 In this chapter, we will learn: while statement do...while statement for statement Assisted control statements: break and continue Looping consists of if and goto

12/2/2018 while Statement The form of while statement: while ( test condition ) loop body test condition loop body true (not 0) false (0) The test condition is evaluated first, and if it is true, the loop body is executed.

12/2/2018 while Statement If the loop body consists of more than one statement, we should use compound statement. Program to calculate the sum from 1 to 100. end begin i=1, sum=0; i<=100 Y N i=i+1; output: sum sum = sum+i; main() { int i=1, sum=0; while ( i <= 100 ) { sum = sum + i; i++; } printf("%d", sum); test condition loop body

while Statement Program to calculate the sum from 1 to 100. 12/2/2018 while Statement Program to calculate the sum from 1 to 100. We can use ctrl+break to stop the execution of program. infinite loop end begin i=1, sum=0; i<=100 Y N i=i+1; output: sum sum = sum+i; main() { int i=1, sum=0; while ( i <= 100 ) sum = sum + i; i++; printf("%d", sum); } main() { int i=1, sum=0; while ( i <= 100 ) { sum = sum + i; i++; } printf("%d", sum);

while Statement – 2 Segments 12/2/2018 while Statement – 2 Segments while ( test condition ) loop body if (sum==50) printf("i=%d",i); { sum = sum+i; i = i+1; } printf("sum=%d", sum=sum+i); ; statment type examples expression statement printf("sum=%d", sum=sum+i); void statement ; control statement if (sum==50) printf("i=%d",i); compound statement { sum=sum+i; i=i+1; }

while Statement – 2 Segments 12/2/2018 while Statement – 2 Segments The loop body can be any type statement. The loop body should be a single statement or a compound statement. The loop body must contain some statements making the loop tend to end. Avoid infinite loop. while ( test condition ) loop body

12/2/2018 do...while Statement The form of do...while statement: do loop body while ( test condition ); test condition loop body true false The loop body is executed first, and then the test condition is evaluated. In do...while statement, the loop body is always executed at least once.

12/2/2018 do...while Statement do...while statement can change to while statement: test condition loop body true false loop body test condition loop body true false

12/2/2018 do...while Statement do...while statement can change to while statement: do loop body while ( test condition ); loop body while ( test condition ) loop body

12/2/2018 do...while Statement Like while statement, if the loop body consists of more than one statement, we should use compound statement. Program to calculate the sum from 1 to 100. end begin i=1, sum=0; i<=100 Y N i=i+1; output: sum sum = sum+i; main() { int i=1, sum=0; do { sum=sum+i; i++; } while ( i <= 100 ); printf("%d",sum); loop body test condition

do...while Statement – 2 Segments 12/2/2018 do...while Statement – 2 Segments do loop body while ( test condition ); The loop body can be any type statement. The loop body should be a single statement or a compound statement. The loop body must contain some statements makeing the loop tend to end. Avoid infinite loop. Pay attention to this semicolon!

while & do...while Statement 12/2/2018 while & do...while Statement Compare while with do...while statement. 101 1 5050 1 5050 101 101 main() { int i, sum=0; scanf ( "%d", &i ); while ( i <= 100 ) { sum = sum + i; i++; } printf ( "%d", sum ); main() { int i, sum=0; scanf ( "%d", &i ); do { sum = sum + i; i++; } while ( i <= 100 ); printf ( "%d", sum ); } while —— the test condition is evaluated first do...while —— the loop body is evaluated first while —— the times of execution of loop body≥0 do……while —— the times of execution of loop body≥1

12/2/2018 for Statement exp2 exp1 true false loop body exp3 The form of for statement: for ( [exp1]; [exp2]; [exp3] ) loop body exp1, exp2 and exp3 are any type expressions, and they all can be omitted. But the separator semicolon can't be omitted. for ( ; ; ) is equivalent to while (1) – infinite loop for statement can be changed to while statement

12/2/2018 for Statement The form of for statement: for ( [exp1]; [exp2]; [exp3] ) loop body The general application form of for statement: for ( initialization; test condition; increment ) loop body e.g. for( sum = 0, i = 1; i <= 100; i ++ ) sum = sum + i; is equivalent to: sum = 0; i = 1; while ( i <= 100 ) { sum = sum + i; i ++; }

12/2/2018 for Statement The form of for statement: for ( [exp1]; [exp2]; [exp3] ) loop body exp1 and exp3 can be comma expression. e.g. for( sum = 0, i = 1; i <= 100; sum += i, i++ ); is equivalent to: sum = 0; for( i = 1; i <= 100; i++ ) sum += i;

for Statement Program to calculate the sum from 1 to 100. main() 12/2/2018 for Statement Program to calculate the sum from 1 to 100. end begin i=1, sum=0; i<=100 Y N i=i+1; output: sum sum = sum+i; main() { int i, sum; sum = 0; for ( i = 1; i <= 100; i++ ) sum = sum + i; printf ( "%d", sum ); } i = 1; for ( ; i <= 100; ) { sum = sum + i; i++; } for ( i = 1; i <= 100 ; sum += i, i++ ); i = 1; for ( ; i <= 100; i++ ) sum = sum + i;

O Nesting of Loops external loop 12/2/2018 Nesting of Loops external loop Nesting of loops means: A loop body contains another complete loop construct. These 3 loop constructs can nest each other, without limitation of the layers. for ( ; ; ) { …… do while() } while ( ); } …... for ( ; ; ) { …… do } while ( ); while() } …... O while ( ) { …… do } while ( ); …... } while ( ) { …… } …... do { …… } while ( ); …... internal loop

12/2/2018 break Statement Form: break; Function: Exit from the loop containing it, causing this loop to be terminated. Exit form the switch statement containing it, causing this switch statement to be terminated. One break can exit only one single loop, the nearest loop. The break can only be used in a loop or a switch statement.

break Statement break; for while do exp1 false false exp exp2 …… true 12/2/2018 break Statement break; exp2 …… break; false true for exp1 exp3 false exp …… break; true while do …… break; exp false true while

break Statement Program to calculate the sum from 1 to 100. main() { 12/2/2018 break Statement Program to calculate the sum from 1 to 100. end begin i=1, sum=0; i<=100 Y N i=i+1; output: sum sum = sum+i; main() { int i, sum = 0; for ( i = 1; ; i++ ) { if ( i > 100 ) break; sum = sum + i; } printf ( "%d", sum ); i = 1; for ( ; ; ) { if ( i > 100 ) break; sum = sum + ( i++ ); }

continue Statement Form: continue; Function: 12/2/2018 continue Statement Form: continue; Function: Skip the following statements in this loop, causing the test condition of the next loop to be judged. The continue can only be used in loops.

continue Statement continue; exp2 …… continue; …... false true for 12/2/2018 continue Statement exp2 …… continue; …... false true for exp1 exp3 continue; true exp false while …… continue; false true do …… continue;…... exp while

12/2/2018 continue Statement Output the numbers, between 100 and 200, which can't be divided exactly by 3. main() { int n; for ( n = 100; n <= 200; n++ ) { if ( n % 3 == 0) continue; printf ( "%5d", n ); }

12/2/2018 Loops – Program 1 Output the former 40 numbers of the Fibonacci sequence. Fibonacci sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, …… F1 = 1 (n = 1) F2 = 1 (n = 2) Fn = Fn-1 + Fn-2 (n≥3) f1 = 1, f2 = 1 for i = 1 to 20 Output: f1, f2 f1 = f1 + f2 f2 = f2 + f1 f1 f2 ? ? ? f2 f1 f1…

Loops – Program 1 main() { long f1 = 1, f2 = 1; int i; 12/2/2018 Loops – Program 1 main() { long f1 = 1, f2 = 1; int i; for ( i = 1; i <= 20; i++ ) { printf ( "%12ld %12ld", f1, f2 ); if ( i % 2 == 0 ) printf ( "\n" ); f1 = f1 + f2; f2 = f2 + f1; } i=1; while ( i <= 20 ) { printf ( "%12ld %12ld", f1, f2 ); if ( i % 2 == 0 ) printf( "\n" ); f1 = f1 + f2; f2 = f2 + f1; i ++; } i=1; do { printf ( "%12ld %12ld", f1, f2 ); if ( i % 2 == 0 ) printf ( "\n" ); f1 = f1 + f2; f2 = f2 + f1; i ++; } while ( i <= 20 );

Loops – Program 2 Judge whether an integer is a prime number or not. 12/2/2018 Loops – Program 2 Judge whether an integer is a prime number or not. Make m be divided by all the numbers between 2 and the square root of m. If m can be divided exactly by a certain number in those, m is not a prime number, otherwise it is. Read in m i=2 when i ≤ k m%i==0? true false break; i = i + 1 i > k m is a prime m isn't a prime k = (int) sqrt(m)

12/2/2018 If m can be divided exactly by the number between 2 and sqrt(m), m is not a prime number, so the loop will be terminated by break statement, and at that time, i must be less or equal to k. Otherwise m is a prime number, and after the last loop, i is equal to k+1. Loops – Program 2 #include <math.h> main() { int m, i, k; scanf ( "%d", &m ); k = sqrt ( m ); for ( i = 2; i <= k; i++ ) if ( m % i == 0 ) break; if ( i > k ) printf( "%d is a prime number.", m); else printf( "%d is not a prime number.", m); }

Loops – Program 2 #include <math.h> main() { int m, i, k; 12/2/2018 Loops – Program 2 #include <math.h> main() { int m, i, k; scanf ( "%d", &m ); k = sqrt ( m ); i = 2; while ( i <= k ) { if( m % i == 0 ) break; i ++; } if ( i > k ) printf( "%d is a prime number.", m); else printf( "%d is not a prime number.", m); } Can't judge 1, 2, 3 ! for ( i = 2; i <= k; i++ ) if ( m % i == 0 ) { printf ( "%d is not a prime number", m ); break; } else if ( i == k ) printf ( "%d is a prime number", m ); do { if( m % i == 0 ) break; i ++; } while ( i <= k );

Loops – Program 3 Output all the primes between 100 and 200. 12/2/2018 Loops – Program 3 Output all the primes between 100 and 200. Try to use while and do…while statement to rewrite this program. #include <math.h> main() { int m, i, k, n = 0; for ( m = 101; m <= 200; m = m + 2 ) { k = sqrt ( m ); for ( i = 2; i <= k; i++ ) if ( m % i == 0 ) break; if ( i > k ) { printf ( "%5d", m ); n = n + 1; } if ( n % 10 == 0) printf ( "\n" ); }

12/2/2018 Loops – Program 4 Read in a positive integer, and output it in reversed order. For example: read in 12345, output 54321. #include <math.h> main() { int n; printf ( "Input a positive integer:" ); scanf ( "%d", &n ); while ( n != 0 ) { printf ( "%d", n % 10 ); n = n / 10; /* number is decreased by 10 times */ }

Loops – Program 5 Output the multiplication table. 12/2/2018 Loops – Program 5 Output the multiplication table. "i" represents the line: (1≤i≤9) "j" represents the column: (1≤j≤i) j i …… 5×5=25 5×4=20 5×3=15 5×2=10 5×1=5 4×4=16 4×3=12 4×2=8 4×1=4 3×3=9 3×2=6 3×1=3 2×2=4 2×1=2 1×1=1 …… 5×5=25 5×4=20 5×3=15 5×2=10 5×1=5 4×4=16 4×3=12 4×2=8 4×1=4 3×3=9 3×2=6 3×1=3 2×2=4 2×1=2 1×1=1

Loops – Program 5 main() { int i, j; for ( i = 1; i < 10; i++ ) 12/2/2018 Loops – Program 5 main() { int i, j; for ( i = 1; i < 10; i++ ) for ( j = 1; j <= i; j++ ) printf ( "%d*%d=%d\t", j, i, i*j ); } printf ( "\n" ); Try to use other nesting of loops to rewrite this program.

Loops – Program 6 Please input a number: Please input a number: 12/2/2018 Loops – Program 6 Please input a number: Please input a number: Read in integer n, and output n factorial (n!). 8 5 8!=-25216 5!=120 8!=40320 0!=120 main() { int n; int m=1; printf ( "Please input a number:" ); scanf ( "%d", &n ); while ( n >= 1 ) { m *= n; n -- ; } printf ( "%d!=%d", n, m ); long printf ( "%d!=", n ); printf ( "%d", m ); printf ( "%ld", m );

12/2/2018 goto Statement Used to: Jump unconditionally from one point to another in a program. General forms: goto label; label: statement; …… or …… label: statement; goto label; …… ……

goto Statement About label: 12/2/2018 goto Statement About label: A label must be a valid identifier, don’t use an integer as a label. A label can only mark executable statement, and can’t mark declaration statement. A label is unique in a function. Don’t use the same label to mark different statements.

if and goto Program to calculate the sum from 1 to 100. main() { 12/2/2018 if and goto Program to calculate the sum from 1 to 100. end begin i=1, sum=0; i<=100 Y N i=i+1; output: sum sum = sum+i; main() { int i=1, sum=0; loop: if ( i <= 100 ) sum += i; i++; goto loop; } printf ( "%d", sum );

goto Statement goto Statement can: Form looping with if statement; 12/2/2018 goto Statement goto Statement can: Form looping with if statement; Jump out of looping. while() { …… { goto label; …… } label: statement

goto Statement It is a good practice to avoid using goto statement! 12/2/2018 goto Statement It is a good practice to avoid using goto statement! goto statements: Making the compiler generate less efficient code; Making the program logic more complicated; Making the program more unreadable; Even breaking down the whole system.

Homework Review Questions P174 12/2/2018 Homework Review Questions P174 6.1, 6.2, 6.9, 6.11~6.13, 6.14(a) (Write down in your exercise book.) Programming Exercises