programminginANSIC Chapter DecisionMakingand

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

treulsee
grade = 'C'; expn if ( score >= t6r0u)e
false
s1
s2
grade = 'Ds'n;
s
else
grade = 'E';
6
if Statement
3/25/2020
The value of the test expression may be any type. If it equals zero, it is false, otherwise true.
19
3/25/2020
if Statement – Program 3
#include <stdio.h>
main()
{
char ch;
Please input the character: M M is a letter!
printf ("Please input the character:\n");
printf ( "%5.2f, %5.2f \n", x, y ); else
printf ( "%5.2f, %5.2f \n", y, x ); }
3/25/2020
13
if Statement – Program 1
3/25/2020
Programming Exercises : Input 3 real numbers, and output them in ascending order. (Homework!)
printf ("Please input 2 numbers:\n");
scanf ( "%f %f", &x, &y );
if ( x > y )
{
temp = x;
x = y;
y = temp; }
printf("The 2 numbers are: %5.2f, %5.2f \n", x, y);
}
18
if Statement – Program 3
3/25/2020
Read in one character, and judge which kind of character it is: a figure, or a letter, or other. Step1: Read the character into variable ch. Step2: If (ch>='0' && ch<='9') , it is a figure. If (ch>='a' && ch<='z') || (ch>='A' && ch<='Z') , it is a letter. Otherwise it is an other character.
3
3 Forms of if Statement
3/25/2020
Form 1 – the most simple form
if ( test expression )
test exp
false (0)
statement;
true (not 0)
statement
if ( score >= 60 ) printf("He passed this examination!");
}
9
if Statement – Program 1
3/25/2020
Input 2 real numbers, and output them in ascending order.
Step1: Read 2 real numbers into variable x and y.
Step2: If x is greater than y, exchange them.
if ( exp1 ) s1; if ( score >= 90 ) else if ( exp2 ) s2;
grade = 'A';
else ……
else if ( scfoarlese>= 80 )
exp1
true else
gifre(axsdpce2o=re'fB>a'=l;se70…)…
else if ( expn ) sn; else s;
ch = getchar( ) ;
if ( ch >= '0' && ch <= '9' )
printf("%c is a figure!", ch);
else if ((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
printf("%c is a letter!", ch);
if ( score >= 60 ) printf("He passed this examination!");
else printf("He failed in this examination!");
5
3 Forms of if Statement
3/25/2020
Form 3 – the nested form
if ( a = 20 ) printf ( "%d", a );
else printf ( “Wrong" );
2Wrong
8
if Statement
3/25/2020
The statement following if or else may be a single
statement or a compound statement.
3/25/2020
Step1: Set the flag variable isleap as 0. Step2: Read in the year. Step3: If the test expression
(year%4==0 && year%100!=0)||(year%400==0) is true, set the flag variable isleap as 1. Step4: According to the value of isleap, output the year is a leap year or not.
14
if Statement – Program 2
3/25/2020
Read in one year, and judge whether it is a leap year or not. Step1: Read in the year. Step2: If the test expression (year%4==0 && year%100!=0)||(year%400==0) is true, then output it is a leap year, or else output it isn’t a leap year.
x>y
false
true
Step3: Output x and y.
exchange x and y
output x and y
10
3/25/2020
if Statement – Program 1
Please input 2 numbers:
main()
13.2 2.1
{
float x, y, temTph;e 2 numbers are: 2.10, 13.20
17
3/25/2020
if Statement – Program 2 Please input the year:
main()
{
int year, isLeap = 0;
2000 2000 is a leap year!
printf ("Please input the year:\n");
scanf ("%d", &year);
main() Error … 6: Misplaced else in function main
{ int x, y;
scanf( "%d,%d", &x, &y );
if ( x > y ) x=y; y=x;
else
Compile Error!
x++; y++;
printf ( "%d,%d\n", x, y);
if ( (year%4==0&&year%100!=0)||(year%400==0) ) printf ( "%d is a leap year!\n", year );
else printf ( "%d is not a leap year!\n", year );
}
16
if Statement – Program 2
Chapter 5 Decision Making and Branching
PROGRAMMING IN ANSI C
Questionቤተ መጻሕፍቲ ባይዱ
3/25/2020
Questions: How do we judge whether a student pass an examination according to his score? How do we decide his grade according to his score?
output y and x.
true x < y false
output x and y
output y and x
12
if Statement – Program 1
main() {
float x, y;
printf ("Please input 2 numbers:\n"); scanf ( "%f %f", &x, &y); printf("The 2 numbers are: "); if ( x < y )
if ( a==b && x==y ) printf ( "a=b, x=y" );
if ( 3 )
OK
printf ( "OK" );
7
if Statement
3/25/2020
The value of the test expression may be any type.
If it equals zero, it is false, otherwise true.
4
3 Forms of if Statement
3/25/2020
Form 2 – the most general form
if ( test expression ) true(not 0) statement 1;
else statement 2;
statement 1
test exp
false(0) statement 2
}
11
if Statement – Program 1
3/25/2020
Input 2 real numbers, and output them in ascending order.
Step1: Read 2 real numbers into variable x and y.
Step2: If x is less than y, output x and y, or else
if ( (year%4==0&&year%100!=0)||(year%400==0) )
isLeap = 1;
if ( isLeap )
printf ( "%d is a leap year!\n", year );
else
printf ( "%d is not a leap year!\n", year );
else printf("%c is an other character!",ch);
}
20
Nesting of if…else… Statement
3/25/2020
When a series of decision are involved, we may use more than one if…else… statement in nested form.
In human nature language: If…, then… In C: Decision-making statement (branch statement)
2
Chapter 5
3/25/2020
In this chapter, we will learn: Decision-making statement: if Conditional operator: ? : Multiway decision-making statement: switch Assisted control statement: break
15
3/25/2020
if Statement – Program 2
Please input the year:
main()
1900
{
int year;
1900 is not a leap year!
printf ("Please input the year:\n"); scanf ("%d", &year);
相关文档
最新文档