Computer Science Department FTSM Control Structure: Selection (Part 2) Knowledge: Understand various concepts of selection control structure Skill: Be.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

Computer Science Department FTSM Control Structure: Selection (Part 1) Knowledge: Understand various concepts of selection control structure Skill: Be.
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
Computer Science Department FTSM Variables and Constants Knowledge: Understand the concept of storage location representation as identifiers Skill: Identify.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
LOOP (Part 2) for while do-while 1. TK1913-C Programming2 TK1913-C Programming 2 Loop : for Loop : for Condition is tested first Loop is controlled by.
Visual C++ Programming: Concepts and Projects
Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop.
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Programming in C++ Lecture Notes 2 – Choice Statements Andreas Savva.
Operators and Expressions
Computer Science Department FTSM Input and Output Knowledge: Understand various types of input and output syntaxes Skill: Develop a program to read/capture.
Decisions – Chapter 3 IPC144 Week 2. IPC144 Introduction to Programming Using C Week 2 – Lesson 2 (Pages 12 to 18 in IPC144 Textbook)
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Computer Science Department Relational Operators And Decisions.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
IF-ELSE IF-ELSE STATEMENT SWITCH-CASE STATEMENT Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 5: Structured Programming.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
1 If statement and relational operators –, >=, ==, != Finding min/max of 2 numbers Finding the min of 3 numbers Forming Complex relational expressions.
Flow of Control Part 1: Selection
If statement The "if statement" is used to break the sequential flow of execution. Enforces branching in execution according to the result of an expression.
1 st Semester Module3 Condition Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Chapter 5: Structured Programming
Introduction to Computing Lecture 05: Selection (continued) Introduction to Computing Lecture 05: Selection (continued) Assist.Prof.Dr. Nükhet ÖZBEK Ege.
CS Class 05 Topics  Selection: switch statement Announcements  Read pages 74-83, ,
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Chapter 5: Structured Programming
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Week 4 Program Control Structure
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C 1 A.AlOsaimi King Saud University College of Applied studies and Community Service Csc 1101.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Decision making If.. else statement.
CSE1301 Sem Selection Lecture 25 Lecture 9: Selection.
Chapter 4 – C Program Control
Chapter 5: Structured Programming
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Condition Statements.
Conditionals, Loops, and Other Kinds of Statements
Looping.
الكلية الجامعية للعلوم التطبيقية
- Additional C Statements
C Programming Variables.
CSC215 Lecture Control Flow.
Control Structure: Selection (Part 2)
Week 3 – Program Control Structure
Dale Roberts, Lecturer IUPUI
CprE 185: Intro to Problem Solving (using C)
Lecture 4: Selection Control Structure
Presentation transcript:

Computer Science Department FTSM Control Structure: Selection (Part 2) Knowledge: Understand various concepts of selection control structure Skill: Be able to develop a program containing selection control structure

TK1913-C Programming2 TK1913-C Programming 2 if Statements if if - else if – else - if

TK1913-C Programming3 TK1913-C Programming 3 if Statement The structure is similar to single selection (flowchart) Syntax: if (expression) statement; or if (expression) { statement1;statement2;} Syntax: if (expression) statement; or if (expression) { statement1;statement2;} Don’t forget the curly brackets !! Don’t forget the brackets !!

TK1913-C Programming4 TK1913-C Programming 4 if Statement if The similarity between single selection structure and if statement: Single Selection: if start step 1 step 2 … step k end_if Single Selection: if start step 1 step 2 … step k end_if if Statement: if ( ) { statement 1 statement 2 … statement k } if Statement: if ( ) { statement 1 statement 2 … statement k }

TK1913-C Programming5 TK1913-C Programming 5 if Statement Example: int num1, num2, min; printf(“Key-in 2 numbers: “); scanf(“%d%d”, &num1, &num2); min = num1; if (num1 > num2) min = num2; printf(“Smallest: %d\n”, min); Example: int num1, num2, min; printf(“Key-in 2 numbers: “); scanf(“%d%d”, &num1, &num2); min = num1; if (num1 > num2) min = num2; printf(“Smallest: %d\n”, min); Key-in 2 numbers: _ num2 ? num1 ? min ? Key-in 2 numbers: _ Key-in 2 numbers: Smallest: 15 _ 20 > 15?

TK1913-C Programming6 TK1913-C Programming 6 if Statement Example: int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark > 80) { printf(“Category: Excellent\n”); printf(“Congratulations!”); } Example: int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark > 80) { printf(“Category: Excellent\n”); printf(“Congratulations!”); } Mark: _ mark ? 92 > 80? 92 Mark: 92 _ Mark: 92 Category: Excellent Mark: 92 Category: Excellent Congratulations!

TK1913-C Programming7 TK1913-C Programming 7 if Statement Example: void main() { int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Pass\n”); printf(“Your mark is %d”, mark); } Example: void main() { int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Pass\n”); printf(“Your mark is %d”, mark); } What will the output be if the mark is 65?

TK1913-C Programming8 TK1913-C Programming 8 if Statement Example: void main() { int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Pass\n”); printf(“Your mark is %d”, mark); } Example: void main() { int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Pass\n”); printf(“Your mark is %d”, mark); } What will the output be if the mark is 35?

TK1913-C Programming9 TK1913-C Programming 9 if - else Statement The structure is similar to double selection (flowchart) Syntax: if (expression) statement;elsestatement; or if (expression) { statement1;statement2; } else statement3; Syntax: if (expression) statement;elsestatement; or if (expression) { statement1;statement2; } else statement3;

TK1913-C Programming10 TK1913-C Programming 10 if - else Statement or if (expression) { statement1;statement2; } else { statement3;statement4;} or if (expression) { statement1;statement2; } else { statement3;statement4;}

TK1913-C Programming11 TK1913-C Programming 11 if – else Statement if - else The similarity between double selection structure and if - else statement: Double Selection: if start step 1 … step k end_if else start step 1 … step n end_else Double Selection: if start step 1 … step k end_if else start step 1 … step n end_else if Statement: if { statement 1 … statement k } else { statement 1 … statement n } if Statement: if { statement 1 … statement k } else { statement 1 … statement n }

TK1913-C Programming12 TK1913-C Programming 12 if - else Statement Example: if (num1 < num2) min = num1; else min = num2; printf(“Smallest: %d\n”, min); Example: if (num1 < num2) min = num1; else min = num2; printf(“Smallest: %d\n”, min); num2 15 num1 10 min ? 10 < 15? _ 10 Smallest: 10 _

TK1913-C Programming13 TK1913-C Programming 13 if - else Statement Example: if (num1 < num2) min = num1; else min = num2; printf(“Smallest: %d\n”, min); Example: if (num1 < num2) min = num1; else min = num2; printf(“Smallest: %d\n”, min); num2 15 num1 20 min ? 20 < 15? _ 15 Smallest: 15 _

TK1913-C Programming14 TK1913-C Programming 14 if - else Statement Example: if (num1 < num2) { min = num1; max = num2; } else { min = num2; max = num1; } printf(“Min = %d, Max = %d\n”, min, max); Example: if (num1 < num2) { min = num1; max = num2; } else { min = num2; max = num1; } printf(“Min = %d, Max = %d\n”, min, max); num2 125 num1 700 min ?? 700 < 125? _ max ?? Min = 125, Max = 700 _

TK1913-C Programming15 TK1913-C Programming 15 if – else Statement Example: void main() { int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Pass\n”); else printf(“Fail\n”); printf(“Your mark is %d”, mark); } Example: void main() { int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Pass\n”); else printf(“Fail\n”); printf(“Your mark is %d”, mark); } What will the output be if the mark is 21? What will the output be if the mark is 74?

TK1913-C Programming16 TK1913-C Programming 16 if – else Statement Example: void main() { int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Pass\n”); else printf(“Fail\n”); printf(“Your mark is %d”, mark); } Example: void main() { int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Pass\n”); else printf(“Fail\n”); printf(“Your mark is %d”, mark); } What will the output be if the mark is 74? What will the output be if the mark is 14?

TK1913-C Programming17 TK1913-C Programming 17 if – else Statement Example: void main() { int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Pass\n”); else { printf(“Fail\n”); printf(“Your mark is %d”, mark); } Example: void main() { int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Pass\n”); else { printf(“Fail\n”); printf(“Your mark is %d”, mark); } What will the output be if the mark is 14? What will the output be if the mark is 70?

TK1913-C Programming18 TK1913-C Programming 18 Take a break …. Why we need program styles?….To ensure your program is readable. Let’s look some examples.. Let’s have a minute break. While you’re having a break, let me introduce you some program styles…..

TK1913-C Programming19 TK1913-C Programming 19 Take a break … (Learn styles) Example: void main() { int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Pass\n”); else printf(“Fail\n”); printf(“Your mark is %d”, mark); } Example: void main() { int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Pass\n”); else printf(“Fail\n”); printf(“Your mark is %d”, mark); }

TK1913-C Programming20 TK1913-C Programming 20 Take a break … (Learn styles) Example: void main() { int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Pass\n”); else printf(“Fail\n”); printf(“Your mark is %d”, mark); } Example: void main() { int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Pass\n”); else printf(“Fail\n”); printf(“Your mark is %d”, mark); } Difficult to read!!! Don’t you think so??

TK1913-C Programming21 TK1913-C Programming 21 Syntax: if (expression) statement; Syntax: if (expression) statement; if statement Let’s recap … Syntax: if (expression) statement;elsestatement; Syntax: if (expression) statement;elsestatement; if-else statement Ok now, let’s look at if – else –if statement

TK1913-C Programming22 TK1913-C Programming 22 if – else - if Statement Syntax: if (expression) statement; else if (expression) statement; Syntax: if (expression) statement; else if (expression) statement; if-else-if statement Syntax: if (expression) statement; else if (expression) statement; statement;elsestatement; Syntax: if (expression) statement; else if (expression) statement; statement;elsestatement; if-else-if statement

TK1913-C Programming23 TK1913-C Programming 23 if – else - if Statement Syntax: if (expression) statement; else if (expression) statement; statement; else (expression) statement; Syntax: if (expression) statement; else if (expression) statement; statement; else (expression) statement; if-else-if statement Be careful…common mistake made by students !!

TK1913-C Programming24 TK1913-C Programming 24 Let’s recap … Example: if start step m … end_if if start step n … end_if else start step x … end_else Example: if start step m … end_if if start step n … end_if else start step x … end_else Multiple Selection Assume condition 1 is true, so … step m, step …. will be executed

TK1913-C Programming25 TK1913-C Programming 25 Let’s recap … Example: if start step m … end_if if start step n … end_if else start step x … end_else Example: if start step m … end_if if start step n … end_if else start step x … end_else Multiple Selection Assume condition 1 is false, so … step m, step …. will be skipped, andstep m, step …. will be skipped, and condition 2 will be testedcondition 2 will be tested

TK1913-C Programming26 TK1913-C Programming 26 Let’s recap … Example: if start step m … end_if if start step n … end_if else start step x … end_else Example: if start step m … end_if if start step n … end_if else start step x … end_else Multiple Selection Assume condition 2 is true, so … step n, step …. will be executed

TK1913-C Programming27 TK1913-C Programming 27 Let’s recap … Example: if start step m … end_if if start step n … end_if else start step x … end_else Example: if start step m … end_if if start step n … end_if else start step x … end_else Multiple Selection Assume condition 2 is also false, so … step n, step …. will be skipped, andstep n, step …. will be skipped, and step x will be executedstep x will be executed

TK1913-C Programming28 TK1913-C Programming 28 Multiple Selection in C Example: #include int main( ) { char letter; scanf(“%c”, &letter); if (letter >= ‘a’ && letter <= ‘z’ ) printf(“Lower case\n”); else if (letter >= ‘A’ && letter <= ‘Z’) printf(“Upper case\n”); else if (letter >= ‘0’ && letter <= ‘9’) printf(“Digit\n”); else printf(“Special character\n”); } Example: #include int main( ) { char letter; scanf(“%c”, &letter); if (letter >= ‘a’ && letter <= ‘z’ ) printf(“Lower case\n”); else if (letter >= ‘A’ && letter <= ‘Z’) printf(“Upper case\n”); else if (letter >= ‘0’ && letter <= ‘9’) printf(“Digit\n”); else printf(“Special character\n”); } Is the letter a lower case? Is the letter an upper case? Is the letter a digit?

TK1913-C Programming29 TK1913-C Programming 29 Multiple Selection in C Example: #include int main( ) { char letter; scanf(“%c”, &letter); if (letter >= ‘a’ && letter <= ‘z’ ) printf(“Lower case\n”); else if (letter >= ‘A’ && letter <= ‘Z’) printf(“Upper case\n”); else if (letter >= ‘0’ && letter <= ‘9’) printf(“Digit\n”); else printf(“Special character\n”); } Example: #include int main( ) { char letter; scanf(“%c”, &letter); if (letter >= ‘a’ && letter <= ‘z’ ) printf(“Lower case\n”); else if (letter >= ‘A’ && letter <= ‘Z’) printf(“Upper case\n”); else if (letter >= ‘0’ && letter <= ‘9’) printf(“Digit\n”); else printf(“Special character\n”); } (the letter is a lower case) true (the letter is a lower case) false (the letter is an upper case) true (the letter is a lower case) false (the letter is an upper case) false (the letter is a digit) true (the letter is a lower case) false (the letter is an upper case) false (the letter is a digit) false

TK1913-C Programming30 TK1913-C Programming 30 Exercise Develop a program for the following problem. Given a mark, determine its grade based on the table below: 74 < mark < 100grade = A 64 < mark < 75grade = B 54 < mark < 65grade = C 39 < mark < 55grade = D 0 < mark < 40grade = E others error message

TK1913-C Programming31 TK1913-C Programming 31 Answer1Answer1Answer1Answer1 int mark; printf(“Key-in the mark: “); scanf(“%d”,&mark); if ((mark >= 75) && (mark <= 100)) printf("Grade = A”); else if ((mark >= 65) && (mark <= 74)) printf(" Grade = B”); else if ((mark >= 55) && (mark <= 64)) printf(" Grade = C”); else if ((mark >= 40) && (mark <= 54)) printf(" Grade = D”); else if ((mark >= 0) && (mark <= 39)) printf(" Grade = E”); else printf(“Input error\n”);

TK1913-C Programming32 TK1913-C Programming 32 int mark; char grade ; printf(“Key-in the mark : “); scanf(“%d”,&mark); if ((mark >= 75) && (mark <= 100)) grade =‘A’; else if ((mark >= 65) && (mark <= 74)) grade =‘B’; else if ((mark >= 55) && (mark <= 64)) grade =‘C’; else if ((mark >= 40) && (mark <= 54)) grade =‘D’; else if ((mark >= 0) && (mark <= 39)) grade =‘E’; else printf(“Input error\n”); printf(“Your grade is %c”, grade ); Answer2Answer2Answer2Answer2

TK1913-C Programming33 TK1913-C Programming 33 int mark; char grade; printf(“Key-in the mark: “); scanf(“%d”,&mark); if ((mark >= 75) && (mark <= 100)) grade=‘A’; else if ((mark >= 65) && (mark <= 74)) grade=‘B’; else if ((mark >= 55) && (mark <= 64)) grade=‘C’; else if ((mark >= 40) && (mark <= 54)) grade=‘D’; else if ((mark >= 0) && (mark <= 39)) grade=‘E’; if ((mark > 100) || (mark < 0)) printf(“Input error\n”); else printf(“Your grade is %c”, grade); Answer3Answer3Answer3Answer3

TK1913-C Programming34 TK1913-C Programming 34 Nested ifs if ifif - else if statement that contains other if / if - else statements

TK1913-C Programming35 TK1913-C Programming 35 Nested ifs Example: if (age > 18) { if (age > 55) price = 2.50; /* Price for senior citizens */ else price = 5.00; /* Price for adults */ } else { if (age < 1) price = 0.0; /* Price for infants */ else price = 1.50; /* for children & teenagers*/ } Example: if (age > 18) { if (age > 55) price = 2.50; /* Price for senior citizens */ else price = 5.00; /* Price for adults */ } else { if (age < 1) price = 0.0; /* Price for infants */ else price = 1.50; /* for children & teenagers*/ } This price is valid for people: age > 55 This price is valid for people: 18 < age < 55 This price is valid for people: age < 1 This price is valid for people: 1 < age < 18

TK1913-C Programming36 TK1913-C Programming 36 Nested ifs - Problem Example: if (age > 18) if (age > 55) price = 2.50; /* Price for senior citizens */ else price = 5.00; /* Price for adults */ Example: if (age > 18) if (age > 55) price = 2.50; /* Price for senior citizens */ else price = 5.00; /* Price for adults */ Which if does this else belong to?

TK1913-C Programming37 TK1913-C Programming 37 Nested ifs - Problem if (age > 18) { if (age > 55) price = 2.50; else price = 5.00; } if (age > 18) { if (age > 55) price = 2.50; else price = 5.00; } if (age > 18) { if (age > 55) price = 2.50; } else price = 5.00; if (age > 18) { if (age > 55) price = 2.50; } else price = 5.00; Which one?

TK1913-C Programming38 TK1913-C Programming 38 Nested ifs - Problem else if By default, else will be attached to the nearest if if (age > 18) if (age > 55) price = 2.50; else price = 5.00; if (age > 18) if (age > 55) price = 2.50; else price = 5.00; if (age > 18) { if (age > 55) price = 2.50; else price = 5.00; } if (age > 18) { if (age > 55) price = 2.50; else price = 5.00; }

TK1913-C Programming39 TK1913-C Programming 39 switch and break The structure is similar to multiple selection (flowchart) Syntax: switch (expression) { case expression1: statement1;break; case espression2: statement2;break;…default:expressionX;break;} Syntax: switch (expression) { case expression1: statement1;break; case espression2: statement2;break;…default:expressionX;break;} Syntax: switch (expression) { case expression1: statement1;break; case espression2: statement2;break;… default: expressionX;break;} Syntax: switch (expression) { case expression1: statement1;break; case espression2: statement2;break;… default: expressionX;break;} Don’t forget the brackets !! Don’t forget the curly brackets !! Don’t forget the colons !!

TK1913-C Programming40 TK1913-C Programming 40 switch and break Important Rule !! Syntax: switch (expression) { case expression1: statement1;break; case espression2: statement2;break;… default: expressionX;break;} Syntax: switch (expression) { case expression1: statement1;break; case espression2: statement2;break;… default: expressionX;break;} Must be INTEGER or CHAR !

TK1913-C Programming41 TK1913-C Programming 41 switch and break Example : switch (month) { case 1: printf(“January\n”); break; case 2: printf(“February\n”); break; case 3: printf(“March\n”); break; default: printf(“Others\n”); break; } printf(“End”); Example : switch (month) { case 1: printf(“January\n”); break; case 2: printf(“February\n”); break; case 3: printf(“March\n”); break; default: printf(“Others\n”); break; } printf(“End”); Assume month = 1, so … …this step will be executed. Later … …case is terminated here. Jump to … January _ January End _

TK1913-C Programming42 TK1913-C Programming 42 switch and break Example : switch (month) { case 1: printf(“January\n”); break; case 2: printf(“February\n”); break; case 3: printf(“March\n”); break; default: printf(“Others\n”); break; } printf(“End”); Example : switch (month) { case 1: printf(“January\n”); break; case 2: printf(“February\n”); break; case 3: printf(“March\n”); break; default: printf(“Others\n”); break; } printf(“End”); …this step will be executed. Later … March _ March End _ Assume month = 3, so … …case is terminated here. Jump to …

TK1913-C Programming43 TK1913-C Programming 43 switch and break Example : switch (month) { case 1: printf(“January\n”); break; case 2: printf(“February\n”); break; case 3: printf(“March\n”); break; default: printf(“Others\n”); break; } printf(“End”); Example : switch (month) { case 1: printf(“January\n”); break; case 2: printf(“February\n”); break; case 3: printf(“March\n”); break; default: printf(“Others\n”); break; } printf(“End”); Now…what will happen if this break is taken out from the program? Example : switch (month) { case 1: printf(“January\n”); break; case 2: printf(“February\n”); case 3: printf(“March\n”); break; default: printf(“Others\n”); break; } printf(“End”); Example : switch (month) { case 1: printf(“January\n”); break; case 2: printf(“February\n”); case 3: printf(“March\n”); break; default: printf(“Others\n”); break; } printf(“End”); No more !!

TK1913-C Programming44 TK1913-C Programming 44 switch and break Example : switch (month) { case 1: printf(“January\n”); break; case 2: printf(“February\n”); case 3: printf(“March\n”); break; default: printf(“Others\n”); break; } printf(“End”); Example : switch (month) { case 1: printf(“January\n”); break; case 2: printf(“February\n”); case 3: printf(“March\n”); break; default: printf(“Others\n”); break; } printf(“End”); …this step will be executed. Later … February _ February March _ Assume month = 2, so … …case is terminated here. Jump to … February March End _ …execution continues. Thus, this step is executed. So …

TK1913-C Programming45 TK1913-C Programming 45 switch and break Example : switch (month) { case 1: printf(“January\n”); break; case 2: printf(“February\n”); case 3: printf(“March\n”); break; default: printf(“Others\n”); break; } printf(“End”); Example : switch (month) { case 1: printf(“January\n”); break; case 2: printf(“February\n”); case 3: printf(“March\n”); break; default: printf(“Others\n”); break; } printf(“End”); Now…what will happen if these breaks are taken out from the program? And … if month = 1 ? And … if month = 34 ?

TK1913-C Programming46 TK1913-C Programming 46 Exercise To practice what you’ve learned, try exercises on page … /* Do exercises from pg of Pengaturcaraan C

TK1913-C Programming47 TK1913-C Programming 47 End of Lecture 7 (Part 2) At last, chap 7 is finished. What’s next??? Control Structure: Repetition on the way…