




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、Computer programmingLecture 3Lecture 3: Outline Program Looping Kochan chap.5 The for Statement Relational Operators Nested for Loops Increment Operator Program Input for Loop Variants The while Statement The do Statement The break Statement The continue StatementExecuting a program Program = list o
2、f statements Entrypoint: the point where the execution starts Control flow: the order in which the individual statements are executed Statement1 Statement2 Statement3 Statement4 Statement5 Statement6 Statement7 Statement8Structure of a C program#include int main (void) int value1, value2, sum; value
3、1 = 50; value2 = 25; sum = value1 + value2; printf (The sum of %i and %i is %in,value1, value2, sum); return 0;Entry point of a C programSequential flow of controlControlling the program flow Forms of controlling the program flow: Executing a sequence of statements Repeating a sequence of statements
4、 (until some condition is met) (looping) Using a test to decide between alternative sequences (branching) Statement1 Statement2 Statement3 Statement4 Statement5 Statement6 Statement7 Statement8Program Looping Looping: doing one thing over and over Program loop: a set of statements that is executed r
5、epetitively for a number of times Simple example: displaying a message 100 times:printf(“hello !n”);printf(“hello !n”);printf(“hello !n”);printf(“hello !n”);printf(“hello !n”);Repeat 100 timesprintf(“hello !n”);Program looping: enables you to develop concise programs containing repetitive processes
6、that could otherwise require many lines of code ! The need for program looping#include int main (void) int triangularNumber;triangularNumber = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8;printf (The eighth triangular number is %in, triangularNumber);return 0;What if we have to compute the 200-th (1000-th, etc) tr
7、iangular number ?Example problem: computing triangular numbers. (The n-th triangular number is the sum of the integers from 1 through n)In C: 3 different statements for looping: for, while, doExample 200th triangular number n=1triangularNumber = triangularNumber + nn=200n=n+1yesnotriangularNumber =
8、0Print triangularNumberStatement before loopinit_expressionloop_conditionstatementloop_expressionStatement after loopExample - for /* Program to calculate the 200th triangular numberIntroduction of the for statement */#include int main (void)int n, triangularNumber;triangularNumber = 0;for ( n = 1;
9、n = 200; n = n + 1 )triangularNumber = triangularNumber + n;printf (The 200th triangular number is %in, triangularNumber);return 0;The for statement for ( init_expression; loop_condition; loop_expression )program statementinit_expressionProgram statementloop_conditionLoop expressionyesno12345The for
10、 statementfor ( n = 1; n = 200; n = n + 1 ) triangularNumber = triangularNumber + n;12345yesnoHow for works The execution of a for statement proceeds as follows:1. The initial expression is evaluated first. This expression usually sets a variable that will be used inside the loop, generally referred
11、 to as an index variable, to some initial value.2. The looping condition is evaluated. If the condition is not satisfied (the expression is false has value 0), the loop is immediately terminated. Execution continues with the program statement that immediately follows the loop.3. The program statemen
12、t that constitutes the body of the loop is executed.4. The looping expression is evaluated. This expression is generally used to change the value of the index variable 5. Return to step 2.Infinite loopsIts the task of the programmer to design correctly the algorithms so that loops end at some moment
13、 !/ Program to count 1+2+3+4+5#include int main (void)int i, n = 5, sum =0;for ( i = 1; i = n; n = n + 1 )sum = sum + i; printf (“%i %i %in, i , sum, n); return 0;What is wrong here ?Does the loop end?Relational operatorsOperatorMeaning=Is equal to!=Is not equal toIs less thanIs greater than=Is grea
14、ter or equalThe relational operators have lower precedence than all arithmetic operators: a b + c is evaluated as a (b + c)ATTENTION ! Do not confuse: the “is equal to” operator = and the “assignment” operator =ATTENTION when comparing floating-point values ! Only comparisons make sense !Example for
15、 with a body of 2 / Program to generate a table of triangular numbers#include int main (void)int n, triangularNumber;printf (TABLE OF TRIANGULAR NUMBERSnn);printf ( n Sum from 1 to nn);printf (- -n);triangularNumber = 0;for ( n = 1; n = 10; +n ) triangularNumber += n;printf ( %i %in, n, triangularNu
16、mber);return 0;The body of the loop consists in a block of 2 statementsIncrement operator Because addition by 1 is a very common operation in programs, a special operator was created in C for this. Increment operator: the expression +n is equivalent to the expression n = n + 1. Decrement operator: t
17、he expression -n is equivalent to the expression n = n 1 Increment and decrement operators can be placed in front (prefix) or after (postfix) their operand. The difference between prefix and postfix: Example: if n=4: a=n+ leads to a=4, n=5 a=+n leads to a=5, n=5 Program input #include int main (void
18、)int n, number, triangularNumber;printf (What triangular number do you want? );scanf (%i, &number);triangularNumber = 0;for ( n = 1; n in a later chapter !Reads integer from keyboardIts polite to display a message before Nested loops#include int main (void)int n, number, triangularNumber, counte
19、r;for ( counter = 1; counter = 5; +counter ) printf (What triangular number do you want? );scanf (%i, &number);triangularNumber = 0;for ( n = 1; n = number; +n )triangularNumber += n;printf (Triangular number %i is %inn, number, triangularNumber);return 0;Remember indentations!for loop variants
20、Multiple expressions (comma between) for(i=0 , j=10 ; ij ; i+ , j-) Omitting fields (semicolon have to be still) i=0; for( ; i10 ; i+ ) Declaring variables for(int i=0 ; i=10 ; i+ )The while statementwhile ( expression )program statementwhile ( number 0“); printf (“Give a new number: “); scanf(“%i“,
21、 &number);The while statementwhile ( expression )program statementstatementLoop_expressionyesnoLoop with the test in the beginning !Body might never be executed !Example: A program to find the greatest common divisor of two nonnegative integer values Example - while /* Program to find the greate
22、st common divisorof two nonnegative integer values */#include int main (void)int u, v, temp;printf (Please type in two nonnegative integers.n);scanf (%i%i, &u, &v);while ( v != 0 ) temp = u % v;u = v;v = temp;printf (Their greatest common divisor is %in, u);return 0;Example: A program to pri
23、nt out the digits of a number in reverse order Example - while / Program to reverse the digits of a number#include int main (void)int number, right_digit;printf (Enter your number.n);scanf (%i, &number);while ( number != 0 ) right_digit = number % 10;printf (%i, right_digit);number = number / 10
24、;printf (n);return 0;Example while not quite OK ! / Program to reverse the digits of a number#include int main (void)int number, right_digit;printf (Enter your number.n);scanf (%i, &number);while ( number != 0 ) right_digit = number % 10;printf (%i, right_digit);number = number / 10;printf (n);r
25、eturn 0;What happens if you enter number=0 ?The do statementdoprogram statementwhile ( loop_expression );statementloop_expressionyesnoLoop with the test at the end !Body is executed at least once !Example do while / Program to reverse the digits of a number#include int main ()int number, right_digit
26、;printf (Enter your number.n);scanf (%i, &number);do right_digit = number % 10;printf (%i, right_digit);number = number / 10;while ( number != 0 );printf (n);return 0;Which loop to choose ? Criteria: Who determines looping Entry-condition loop - for, while Exit-condition loop - do Criteria: Numb
27、er of repetitions: Indefinite loops -while Counting loops - for In C, you can actually rewrite any while as a for and viceversa !Example: while vs for #include int main (void)int count = 1;while ( count = 5 ) printf (%in, count);+count;return 0;#include int main (void)int count;for ( count=1; count=5; count+ ) printf (%in, count);return 0;The break Statement Can be used in order to immediately exiting from a loop After a break
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025典當最高額抵押借款合同范本
- 醫(yī)院管理中不良事件的監(jiān)控與預防
- 如何提升行政法學知識的試題及答案
- 區(qū)塊鏈技術基礎及安全保障探討
- 企業(yè)文化微信宣傳課件
- 財務信息共享的優(yōu)勢與挑戰(zhàn)計劃
- 社交能力提升的月度目標計劃
- 經濟法概論考試題型分析與試題及答案
- 衛(wèi)生資格考試核心能力培養(yǎng)試題及答案
- 醫(yī)療大數據助力決策智能化升級
- 腎切除手術的護理查房
- 2025華陽新材料科技集團有限公司招聘(500人)筆試參考題庫附帶答案詳解
- 2024年美睫技術考核試題及答案
- 兒童言語康復試題及答案
- 解析:湖北省十一校2024-2025學年高三第二次聯考數學試卷(原卷版)
- 2024年江蘇省常州外國語學校中考一模語文試題(含答案)
- 畢業(yè)設計(論文)-可調節(jié)辦公椅分析與設計
- 2025年全球及中國潛孔鉆機行業(yè)發(fā)展現狀調研及投資前景分析報告
- 多模態(tài)數據交互式可視化-全面剖析
- 門診外配處方管理制度
- 中國國際大學生創(chuàng)新大賽(2025)職教賽道重點培育項目申報表
評論
0/150
提交評論