




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 沈阳航空航天大学北方科技学院《网页制作与网站开发实验》2023-2024学年第二学期期末试卷
- 山西中医药大学《净水处理工艺与工程》2023-2024学年第二学期期末试卷
- 西安科技大学《国际营销概论》2023-2024学年第二学期期末试卷
- 新疆师范大学《现代传感技术》2023-2024学年第二学期期末试卷
- 辽宁师范大学海华学院《经济学世界经济》2023-2024学年第二学期期末试卷
- 2024年高纯金属及氧化物资金需求报告代可行性研究报告
- 三年级法制教育基础认知
- 2025年浙江杭州大江东国有资本投资管理有限公司招聘笔试参考题库含答案解析
- 娱乐空间设计市场调研
- 2025年陕西省中小企业融资担保有限公司招聘笔试参考题库附带答案详解
- 艺术留学作品集合同模板
- 2024-2025年上海中考英语真题及答案解析
- 《马说》复习课件
- GB/T 19510.213-2023光源控制装置第2-13部分:LED模块用直流或交流电子控制装置的特殊要求
- 2024年桥式起重机司机(中级)职业技能考试题库(职校培训)
- 工程建设公司QC小组道路沥青混凝土面层裂缝的控制成果汇报书
- 提升教师专业素养与综合能力的培训
- 人教版小学道德与法治《众志成城》教学设计
- 12、口腔科诊疗指南及技术操作规范
- JB-T 4149-2022 臂式斗轮堆取料机
- 文创产品设计-第四章-文创产品设计的基本流程
评论
0/150
提交评论