




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 甲方小店铺租赁合同范本
- 燃油安全安装合同协议书
- 美甲店加盟协议合同范本
- 物流合同配送协议书模板
- 股权合作协议补充协议书
- 签约非遗传承人合同范本
- 自建洗车房自用合同范本
- 苗木培育承包合同协议书
- 法律居间代理协议书模板
- 猫舍合作合同协议书范本
- 顶管专项施工方案审查意见
- ZAPI(萨牌)控制器ACE2-重要参数以及调试步骤
- 道路绿化养护投标方案(技术方案)
- GB/T 11064.16-2023碳酸锂、单水氢氧化锂、氯化锂化学分析方法第16部分:钙、镁、铜、铅、锌、镍、锰、镉、铝、铁、硫酸根含量的测定电感耦合等离子体原子发射光谱法
- 2023年云南文山州州属事业单位选调考试试卷真题
- dd5e人物卡可填充格式角色卡夜版
- 浅谈中华优秀传统文化融入中职教育研究
- 生产管理制度文本普通货运
- 舞蹈概论课程教学大纲
- 数字媒体艺术概论
- 内科学讲义(唐子益版)
评论
0/150
提交评论