【正文】
ad 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. 4/17/2023 16 if Statement – Program 2 main() { int year。 else x++。 else if ( score = 80 ) grade = 39。 else statement 2。E39。 x = y。 scanf (%d, year)。) , it is a letter. Otherwise it is an other character. 4/17/2023 20 if Statement – Program 3 include main() { char ch。)) printf(%c is a letter!, ch)。 if ( score = 80 ) printf ( The grade is: A!\n )。 printf ( Please input the score:\n )。 else if ( score = 60 ) grade = 39。a39。 ) ? ch +32 : ch 。: printf(Excellent!)。C39。: printf(Fail!)。 4/17/2023 39 switch Statement ? The general form: switch (exp) { case value1: statement group1。t be real number switch (2) { case 1: printf(1)。 break。 printf(\n)。 break。 switch ( (int) score / 10 ) { case 9: case 10: printf(A\n)。t any one. ? If c1==39。s39。t39。t39。 } } Execute this program, if the we input it, what will be outputted? it at error 4/17/2023 50 switch Statement – Program 2 main() { char c1, c2。m39。 break。 default: printf(error\n)。 :09:1900:09:19April 17, 2023 1意志堅強的人能把世界放在手中像泥塊一樣任意揉捏。 2023年 4月 上午 12時 9分 :09April 17, 2023 1業(yè)余生活要有意義,不要越軌。 , April 17, 2023 很多事情努力了未必有結(jié)果,但是不努力卻什么改變也沒有。 case 39。i39。 break。: printf(at\n)。s39。m39。 scanf (%c%c, c1, c2)。 ? If c2==39。 ? score 60 : grade D。 default: statement group。 case 2: printf(2)。 } should be integer or character type must be constants or constants expressions, and can39。 case value2: statement group2。 break。C39。A39。 value2 …… valuen statement group2。 } ch = ( ch = 39。 else printf ( %f, a – b )。 scanf ( %f, score )。C39。 ? score 60 : grade C。)||(ch=39。) || (ch=39。 if ( (year%4==0year%100!=0)||(year%400==0) ) printf ( %d is a leap year!\n, year )。 } Compile Error! Error … 6: Misplaced else in function main 4/17/2023 10 if Statement – Program 1 ? 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. ? Step3: Output x and y. x y exchange x and y true false output x and y 4/17/2023 11 if Statement – Program 1 main() { float x, y, temp。C39。Chapter 5 Decision Making and Branching PROGRAMMING IN ANSI C 2 Question ? 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? ? In human nature language: If…, then… ? In C: Decisionmaking statement (branch statement) 4/17/2023 3 Chapter 5 ? In this chapter, we will learn: ? Decisionmaking statement: if ? Conditional operator: ? : ? Multiway decisionmaking statement: switch ? Assisted control statement: break 4/17/2023 4 3 Forms of if Statement ? Form 1 – the most simple form if ( test expression ) statement。 else if ( score = 60) grade = 39。 printf (Please input 2 numbers:\n)。 else printf ( %d is not a leap year!\n, year )。A39。A39。 4/17/2023 24 if Statement – Program 4 main() { float score。 printf ( The grade is: %c!\n, grade )。 if ( score 100 || score 0 ) printf( The score is wrong!\n )。 printf( %f, b 0 ? a + b : a – b )。A39。 …… statement groupn。 39。: printf(Pass!)。 break。 ……. case valuen: statement groupn。t be real number switch (2) { case 1: printf(1)。 printf(\n)。 } is an optional case, and can be placed anywhere but usually placed at the end. If present, it will be executed when the expression does not match with any of the case values. switch ( 0 ) { case 1: printf(1)。 ? Step 1: read in score (float type). ? Step 2: According to (int) score / 10 , decide and output the grade. 4/17/2023 46 switch Statement