Chapter 5 Decision Making and Branching

Slides:



Advertisements
Similar presentations
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Selective Structures.
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
Chapter 4 Making Decisions
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23.
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
Week 3 – Selection Structures UniMAP SemPGT C PROGRAMMING1.
Conditional Statement
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Computer programming Lecture 4. Lecture 4: Outline Making Decisions [chap 6 – Kochan] –The if Statement –The if-else Construct –Logical Operators –Boolean.
UniMAP Sem II-09/10EKT120: Computer Programming1 Week 3 – Selection Structures.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
Chapter 5: Structured Programming
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
Chapter Making Decisions 4. Relational Operators 4.1.
Week 4 Program Control Structure
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
Making Decisions in c. 1.if statement Imagine that you could translate a statement such as “If it is not raining, then I will go swimming” into the C.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
Lesson #4 Logical Operators and Selection Statements.
Lesson #4 Logical Operators and Selection Statements.
Decision making If.. else statement.
CSE1301 Sem Selection Lecture 25 Lecture 9: Selection.
The if…else Selection Statement
Chapter 4 C Program Control Part I
Selection (also known as Branching) Jumail Bin Taliba by
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Decisions Chapter 4.
Chapter 4: Making Decisions.
Week 3 C Program Structures (Selection Structures)
ECE Application Programming
Week 3 – Selection Structures
Chapter 2.1 Control Structures (Selection)
DKT121: Fundamental of Computer Programming
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Introduction to Programming
Selection Statements Chapter 5
Chapter 4: Control Structures I (Selection)
Decision Making.
C Programming Variables.
Chapter 6 Decision Making and Looping
CSC215 Lecture Control Flow.
Chapter 7 Arrays PROGRAMMING IN ANSI C.
Chapter 4 Managing Input and Output Operations
Week 3 – Program Control Structure
Control Structures Lecture 6.
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
EECE.2160 ECE Application Programming
CSC215 Lecture Control Flow.
ICS103: Programming in C 4: Selection Structures
Chapter 13 Control Structures
Presentation transcript:

Chapter 5 Decision Making and Branching PROGRAMMING IN ANSI C

11/30/2018 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: Decision-making statement (branch statement) 决策、判断

Chapter 5 In this chapter, we will learn: 11/30/2018 Chapter 5 In this chapter, we will learn: Decision-making statement: if Conditional operator: ? : Multiway decision-making statement: switch Assisted control statement: break

11/30/2018 3 Forms of if Statement Form 1 – the most simple form if ( test expression ) statement; test exp statement true (not 0) false (0) if ( score >= 60 ) printf("He passed this examination!");

3 Forms of if Statement Form 2 – the most general form 11/30/2018 3 Forms of if Statement Form 2 – the most general form if ( test expression ) statement 1; else statement 2; test exp statement 1 true(not 0) false(0) statement 2 if ( score >= 60 ) printf("He passed this examination!"); else printf("He failed in this examination!");

3 Forms of if Statement Form 3 – the nested form 11/30/2018 3 Forms of if Statement Form 3 – the nested form if ( exp1 ) s1; else if ( exp2 ) s2; else …… else if ( expn ) sn; else s; if ( score >= 90 ) grade = 'A'; else if ( score >= 80 ) grade = 'B'; else if ( score >= 70 ) grade = 'C'; else if ( score >= 60) grade = 'D'; else grade = 'E'; exp1 s1 true false exp2 expn s2 …… sn s

11/30/2018 if Statement The value of the test expression may be any type. If it equals zero, it is false, otherwise true. if ( a==b && x==y ) printf ( "a=b, x=y" ); if ( 3 ) printf ( "OK" ); OK

11/30/2018 if Statement The value of the test expression may be any type. If it equals zero, it is false, otherwise true. if ( a = 2 ) printf ( "%d", a ); else printf ( “Wrong" ); if ( a = 0 ) printf ( "%d", a ); else printf ( “Wrong" ); Wrong 2

11/30/2018 if Statement The statement following if or else may be a single statement or a compound statement. Error … 6: Misplaced else in function main main() { int x, y; scanf( "%d,%d", &x, &y ); if ( x > y ) x=y; y=x; else x++; y++; printf ( "%d,%d\n", x, y); } Compile Error!

11/30/2018 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

if Statement – Program 1 Please input 2 numbers: 13.2 2.1  main() 11/30/2018 if Statement – Program 1 Please input 2 numbers: 13.2 2.1  The 2 numbers are: 2.10, 13.20 main() { float x, y, temp; 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); }

11/30/2018 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 less than y, output x and y, or else output y and x. x < y output x and y true false output y and x

if Statement – Program 1 main() { float x, y; 11/30/2018 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 ) printf ( "%5.2f, %5.2f \n", x, y ); else printf ( "%5.2f, %5.2f \n", y, x ); }

11/30/2018 if Statement – Program 1 Programming Exercises : Input 3 real numbers, and output them in ascending order. (Homework!)

11/30/2018 if Statement – Program 2 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.

if Statement – Program 2 Please input the year: 1900 main() 11/30/2018 if Statement – Program 2 Please input the year: 1900 1900 is not a leap year! main() { int year; printf ("Please input the year:\n"); scanf ("%d", &year); 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 ); }

if Statement – Program 2 Step1: Set the flag variable isleap as 0. 11/30/2018 if Statement – Program 2 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.

if Statement – Program 2 Please input the year: 2000 11/30/2018 if Statement – Program 2 Please input the year: 2000 2000 is a leap year! main() { int year, isLeap = 0; printf ("Please input the year:\n"); scanf ("%d", &year); 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 ); }

11/30/2018 if Statement – Program 3 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.

if Statement – Program 3 Please input the character: 11/30/2018 if Statement – Program 3 Please input the character: M M is a letter! #include <stdio.h> main() { char ch; printf ("Please input the character:\n"); 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); else printf("%c is an other character!",ch); }

Nesting of if…else… Statement 11/30/2018 Nesting of if…else… Statement When a series of decision are involved, we may use more than one if…else… statement in nested form. if (exp1) if (exp2) statement1; else statement2; else if (exp3) statement3; else statement4;

Nesting of if…else… Statement 11/30/2018 Nesting of if…else… Statement “else” matching principle: An else is always linked to the closest non-terminated if. if (exp1) if (exp2) statement1; else statement2; if (exp1) { if (exp2) statement1; } else statement2;

if Statement – Program 4 According to the score, decide the grade. 11/30/2018 if Statement – Program 4 According to the score, decide the grade. score >= 80: grade A; score >= 60 && score < 80: grade B; score < 60 : grade C;

if Statement – Program 4 Please input the score: 75.5 The grade is B! 11/30/2018 if Statement – Program 4 Please input the score: 75.5 The grade is B! main() { float score; printf ( "Please input the score:\n" ); scanf ( "%f", &score ); if ( score >= 80 ) printf ( "The grade is: A!\n" ); else if ( score >= 60 ) printf ( "The grade is: B!\n" ); else printf ( "The grade is: C!\n" ); }

if Statement – Program 4 main() { float score; char grade; 11/30/2018 if Statement – Program 4 main() { float score; char grade; printf ( "Please input the score:\n" ); scanf ( "%f", &score ); if ( score >= 80) grade = 'A'; else if ( score >= 60 ) grade = 'B'; else grade = 'C'; printf ( "The grade is: %c!\n", grade ); }

if Statement – Program 4 main() { float score; char grade; 11/30/2018 if Statement – Program 4 main() { float score; char grade; printf ( "Please input the score:\n" ); scanf ( "%f", &score ); if ( score >= 80) grade = 'A'; if ( score >= 60 && score < 80) grade = 'B'; if ( score < 60) grade = 'C'; printf ( "The grade is: %c!\n", grade ); }

if Statement – Program 4 main() { float score; char grade; 11/30/2018 if Statement – Program 4 main() { float score; char grade; printf ( "Please input the score:\n" ); scanf ( "%f", &score ); if ( score > 100 || score < 0 ) printf( "The score is wrong!\n" ); else { if ( score >= 80) grade = 'A'; else if ( score >= 60 ) grade = 'B'; else grade = 'C'; printf ( "The grade is: %c!\n", grade ); }

Conditional Operator ? : 11/30/2018 Conditional Operator ? : Ternary operator: conditional exp ? exp1 : exp2 conditional exp return the value of exp1 true(not 0) false(0) of exp2

Conditional Operator ? : 11/30/2018 Conditional Operator ? : Ternary operator: conditional exp ? exp1 : exp2 if ( a > b ) max = a; else max = b; if ( a > b ) printf("%d", a); else printf("%d", b); max = a > b ? a : b; printf( "%d", a > b ? a : b);

Conditional Operator ? : 11/30/2018 Conditional Operator ? : Ternary operator: conditional exp ? exp1 : exp2 How to output the value of a + |b| ? if ( b > 0 ) printf ( "%f", a + b ); else printf ( "%f", a – b ); printf( "%f", b > 0 ? a + b : a – b ); printf( "%f", a + ( b > 0 ? b : - b ) );

Conditional Operator ? : 11/30/2018 Conditional Operator ? : Ternary operator: conditional exp ? exp1 : exp2 Precedence : higher than assignment operators lower than || Associativity: Right to left Conditional expression can be nested. x > 0 ? 1 : x < 0 ? –1 : 0

Conditional Operator ? : 11/30/2018 Conditional Operator ? : Ternary operator: conditional exp ? exp1 : exp2 The types of conditional exp and exp1 and exp2 may not be the same. The type of the conditional expression is the same with the highest size type. x == 0 ? 'a' : 'b' x > y ? 1 : 1.5

Conditional Operator ? : - Program 11/30/2018 Conditional Operator ? : - Program Read in a character, if it is an uppercase, output its lowercase equivalent. Otherwise output the read-in character. Step1: Read a character into variable ch. Step2: If ch is an uppercase, convert it into its lowercase equivalent. Step3: Output ch.

Conditional Operator ? : - Program 11/30/2018 Conditional Operator ? : - Program main() { char ch; printf ( "Please input the charactor:\n" ); scanf ( "%c", &ch ); if ( ch >= 'A' && ch <= 'Z' ) ch = ch + 32; printf ( "The charactor is: %c\n", ch ); } printf ( "The charactor is: %c\n", ( ch >= 'A' && ch <= 'Z' ) ? ch + 32 : ch ); ch = ( ch >= 'A' && ch <= 'Z' ) ? ch +32 : ch ;

11/30/2018 switch Statement One general if statement controls 2 branches. In order to deal with more than 2 branches, we can use the nesting structure of if statement. However, the more branches, the more nesting level, the longer program lines, the more difficultly to read. C supplies a multiway decision statement: switch.

switch Statement The general form: switch (exp) { 11/30/2018 switch Statement exp statement group1; value1 default case statement group; value2 …… valuen statement group2; statement groupn; The general form: switch (exp) { case value1: statement group1; case value2: statement group2; ……. case valuen: statement groupn; default: statement group; }

switch Statement Good!Pass!Fail!Data error! if grade=='B' 11/30/2018 switch Statement Good!Pass!Fail!Data error! switch grade “Excellent!” 'A' 'B' 'C' default case “Good!” “Pass!” “Data error” “Fail!” 'D' if grade=='B' According to the grade, output information. Grade A: output "Excellent!". Grade B: output "Good!". Grade C: output "Pass!". Grade D: output "Fail!". Others: output "Data error!" switch ( grade ) { case 'A': printf("Excellent!"); case 'B': printf("Good!"); case 'C': printf("Pass!"); case 'D': printf("Fail!"); default: printf("Data error!"); }

switch Statement Good! if grade=='B' 11/30/2018 switch Statement Good! switch grade “Excellent!” 'A' 'B' 'C' default case “Good!” “Pass!” “Data Error” “Fail!” 'D' if grade=='B' According to the grade, output information. Grade A: output "Excellent!". Grade B: output "Good!". Grade C: output "Pass!". Grade D: output "Fail!". Others: output "Data error!" switch ( grade ) { case 'A': printf("Excellent!"); case 'B': printf("Good!"); case 'C': printf("Pass!"); case 'D': printf("Fail!"); default: printf("Data error!"); } switch ( grade ) { case 'A': printf("Excellent!"); break; case 'B': printf("Good!"); case 'C': printf("Pass!"); case 'D': printf("Fail!"); default: printf("Data error!"); }

should be integer or character type 11/30/2018 switch Statement The general form: switch (exp) { case value1: statement group1; case value2: statement group2; ……. case valuen: statement groupn; default: statement group; } should be integer or character type switch ( 2.8 ) { case 1: printf("1"); break; case 2: printf("2"); case 3: printf("3"); default: printf("0"); } 2 If it is not integer or character type expression, it will be converted to integer type. For all that, you should not use real number expression as possible.

should be integer or character type 11/30/2018 switch Statement int a = 1; switch (2) { case 1: printf("1"); break; case 1+a: printf("2"); break; case 3: printf("3"); break; default: printf("0"); } switch (2) { case 1: printf("1"); break; case 1+1: printf("2"); break; case 3: printf("3"); break; default: printf("0"); } switch (2) { case 1: printf("1"); break; case 2.0: printf("2"); break; case 3: printf("3"); break; default: printf("0"); } switch (2) { case 1: printf("1"); break; case 2: printf("2"); break; case 3: printf("3"); break; default: printf("0"); } The general form: 2 2 switch (exp) { case value1: statement group1; case value2: statement group2; ……. case valuen: statement groupn; default: statement group; } should be integer or character type Error...: Constant expression required in function ... must be constants or constants expressions, and can't be real number

should be integer or character type 11/30/2018 switch Statement switch (2) { case 1: printf("1"); break; case 2: printf("2"); break; case 1+1: printf("3"); break; default: printf("0"); } The general form: switch (exp) { case value1: statement group1; case value2: statement group2; ……. case valuen: statement groupn; default: statement group; } should be integer or character type Error...: Duplicate case in function ... must be constants or constants expressions, and can't be real number Each of these values must be unique within a switch statement.

11/30/2018 switch Statement In "if" statement, the statement after "if" or "else" must be a single statement or a compound statement. The general form: switch (exp) { case value1: statement group1; case value2: statement group2; ……. case valuen: statement groupn; default: statement group; } switch (x+y) { case 1: printf("1"); printf("\n"); break; case 2: printf("2"); printf("\n"); default: printf("0"); } can be zero or more statements, and needn't put braces around these statements. Pay attention to the usage of "break".

switch Statement switch (x+y) { case 1: case 2: case 3: printf("1"); 11/30/2018 switch (x+y) { case 1: case 2: case 3: printf("1"); printf("\n"); break; default: printf("0"); } switch Statement The general form: switch (exp) { case value1: statement group1; case value2: statement group2; ……. case valuen: statement groupn; default: statement group; } multiple "case" may share one statement group.

switch Statement switch ( 0 ) { case 1: printf("1"); break; 11/30/2018 switch Statement switch ( 0 ) { case 1: printf("1"); break; default: printf("0"); case 2: printf("2"); case 3: printf("3"); } switch ( 0 ) { case 1: printf("1"); break; case 2: printf("2"); case 3: printf("3"); default: printf("0"); } 02 The general form: switch (exp) { case value1: statement group1; case value2: statement group2; ……. case valuen: statement groupn; default: statement group; } If present, it will be executed when the expression does not match with any of the case values. is an optional case, and can be placed anywhere but usually placed at the end.

switch Statement – Program 1 11/30/2018 switch Statement – Program 1 According to the score, decide the grade. score >= 90: grade A; score >= 70 && score < 90: grade B; score >= 60 && score < 70: grade C; score < 60 : grade D; Step 1: read in score (float type). Step 2: According to (int) score / 10 , decide and output the grade.

switch Statement – Program 1 11/30/2018 switch Statement – Program 1 main() { float score; printf ("Please input the score:\n"); scanf ("%f", &score); switch ( (int) score / 10 ) { case 9: case 10: printf("A\n"); break; case 7: case 8: printf("B\n"); break; case 6: printf("C\n"); break; default: printf("D\n"); }

switch Statement – Program 2 11/30/2018 switch Statement – Program 2 It is permitted to nest switch statements. Test the whether the input word is "is" or "it" or "am" or "at". Step1: Declare 2 char variables – c1 and c2. Step2: Read the 2 characters of the input word into c1 and c2. Step3: Judge the word and output the conclusion. If c1=='i', judge c2 If c2=='s', the word is "is"; If c2=='t', the word is "it"; Otherwise, the word isn't any one. If c1=='a', judge c2 If c2=='m', the word is "am"; If c2=='t', the word is "at";

switch Statement – Program 2 11/30/2018 main() { char c1, c2; printf ("Please input the word:"); scanf ("%c%c", &c1, &c2); switch ( c1 ) { case 'i': switch(c2) { case 's': printf("is\n"); break; case 't': printf("it\n"); break; default: printf("error\n");} break; case 'a': switch(c2) { case 'm': printf("am\n"); break; case 't': printf("at\n"); break; default: printf("error\n"); } } switch Statement – Program 2

switch Statement – Program 2 11/30/2018 main() { char c1, c2; printf ("Please input the word:"); scanf ("%c%c", &c1, &c2); switch ( c1 ) { case 'i': switch(c2) { case 's': printf("is\n"); break; case 't': printf("it\n"); break; default: printf("error\n");} case 'a': switch(c2) { case 'm': printf("am\n"); break; case 't': printf("at\n"); break; default: printf("error\n"); } } switch Statement – Program 2 Execute this program, if the we input "it", what will be outputted? it at error

switch Statement – Program 2 11/30/2018 main() { char c1, c2; printf ("Please input the word:"); scanf ("%c%c", &c1, &c2); switch ( c1 ) { case 'i': if (c2=='s') printf("is\n"); else if (c2=='t') printf("it\n"); else printf("error\n "); break; case 'a': if (c2=='m') printf("am\n"); else if (c2=='t') printf("at\n"); default: printf("error\n"); } } switch Statement – Program 2

switch Statement – Program 2 11/30/2018 main() { char c1, c2; printf ("Please input the word:"); scanf ("%c%c", &c1, &c2); if ( c1=='i' ) switch(c2) { case 's': printf("is\n"); break; case 't': printf("it\n"); break; default: printf("error\n");} else if ( c1=='a' ) { case 'm': printf("am\n"); break; case 't': printf("at\n"); break; else printf("error\n"); } switch Statement – Program 2

11/30/2018 Homework Review Questions P139 5.1~5.6, 5.8~5.10 (Write down in your exercise book) Programming Exercises Tip: sqrt()