




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 慢性硬脑膜下血肿护理查房
- 电工培训常用低压电器
- 小学阅读方法指导
- 妊娠合并尿路感染护理
- 中学食堂建设项目立项报告
- 公务员法实施细则培训
- 文化馆改造提升项目招商引资报告
- 2025年浙江客运资格证理论考试仿真考试
- 2025年下载出租车从业资格证
- 乒乓球教学设计五篇课件
- 电梯维保服务投标方案
- 航空航天工程行业技术发展与创新趋势
- 2023年资产负债表模板
- 01SS105给排水常用仪表及特种阀门安装图集
- 变电一次设备标准缺陷库
- 【VCGE06】昌平区2020-2021学年第二学期高二年级期末质量抽测
- 三北防护林课件
- 三年级小学英语阅读理解
- 小学四年级英语答题卡(Word版可以编辑修改)
- 2023年02月江苏省药品监督管理局审评核查南京分中心公开招聘编外人员15人参考题库+答案详解
- 马工程《中国法制史》课本期末重点笔记整理
评论
0/150
提交评论