control structure c++



Comments



Description

BFC 20802CONTROL STRUCTURE AIM To acknowledge student with the concept of control structures in programming: selection control structure and repetition control structure OBJECTIVES At the end of the chapter, student will know: • To explain control structures concept • To write a C++ program using control structures UNIVERSITI TUN HUSSEIN ONN MALAYSIA CONTROL STRUCTURE Sequence Control Structure Repetition Control Structure Selection Control Structure Selection Control Structure Used in programming to: offer choice of interests to user of a system. restrict/limit to only one operation/process in a system to be executed at a time allow user to choose only one selection of process/operation at a time from a system. execute a process/operation based on selection. 4  The concept is "one selection. 5 . the ATM processes are controlled by some restrictions or rules which is written using selection control structure (using particular programming language). one operation".Selection Control Structure Best example/case:  In general. Selection Control Structure 6 . 7 . If the input matches the setting conditions.Selection Control Structure How decision is made when user make their selection from the ATM menu? The system will check the input and compares the selection with the conditions set in the system. the corresponding menu will be displayed. Selection Control Structure  Choose only one instruction to be executed.  Selection will be done if only the conditional statement is TRUE.  Types of selection control structure: Single selection (if) Double selection (if-else) Multi level selection (if-else-if) 8 . statement will be selected. 9 .  Used in a situation which  if conditional statement is TRUE.Single selection (if)  Use if statement. i) Single Selection cont… Convert to flowchart: grade ==‘A’ Yes Print “Excellent” No UNIVERSITI TUN HUSSEIN ONN MALAYSIA . Consist of: Output statement/ Input statement/ expression .Single selection (if) FORMAT Reserved word if (conditional expression) C++ statement. print a message ‘Excellent” Convert to valid selection statement: if ( grade = = ‘A’) cout << “Excellent” .Single selection (if) EXAMPL E If grade is A. Conditional expression .  If the condition is TRUE.  Use in situation which provide 2 condition.Double selection (if-else)  Use if. 13 .  If not. the next selection will be executed. then the first statement will be executed. but only 1 selection can be made.else statement. ii) Double Selection cont… Convert to flow chart: Yes grade = ‘A’ Print “Passed” No Print “Failed” UNIVERSITI TUN HUSSEIN ONN MALAYSIA . Double selection (if-else) FORMAT if (conditional statement) C++ statement. . else C++ statement. 16 . else cout << “Failed”.Double selection (if-else) EXAMPL E If grade == ‘A’. print a message “You passed ” Otherwise. print a message “ You failed” Convert to a valid C++ selection statement: if ( grade ==‘A’) cout << “Passed”. else if (conditional expression is TRUE) C++ statement..  If the conditional is TRUE (1).else if statement  Use in a situation which requires/provides two choices.  Format: if (conditional expression is TRUE) C++ statement. else if (conditional expression is TRUE) C++ statement.iii) Multi Level Selection  Use if. UNIVERSITI TUN HUSSEIN ONN MALAYSIA . then selection will be made. else C++ statement. else if (conditional expression is TRUE) C++ statement.. UNIVERSITI TUN HUSSEIN ONN MALAYSIA .else if statement  Use in a situation which requires/provides two choices.iii) Multi Level Selection  Use if. else C++ statement. else if (conditional expression is TRUE) C++ statement.  If the conditional is TRUE (1).  Format: if (conditional expression is TRUE) C++ statement. then selection will be made. iii) Multi Level Selection cont… Example 1: Write the selection control structure statements in C++ to produce the following output based on the input: Input Output 1 One 2 Two 3 Three 4 Four 5 Five UNIVERSITI TUN HUSSEIN ONN MALAYSIA . system("pause"). cout << endl << endl. return 0.iii) Multi Level Selection Answer: if (input == 1) cout << "=One\n". else if (input == 3) cout << "\n=Three\n". else if (input == 4) cout << "\n=Four\n". else if (input == 5) cout << "\n=Five\n". TUN HUSSEIN ONN MALAYSIA UNIVERSITI cont… . else cout << "\nInvalid\n". else if (input == 2) cout << "\n=Two\n". iii) Multi Level Selection cont… Example 2: Write the C++ statements to determine student’s grade based on their marks as shown in the following table: Mark Grade 80-100 A 60-79 B 40-59 C 0-39 D UNIVERSITI TUN HUSSEIN ONN MALAYSIA . else if (mark >= 0 && mark <= 39) grade = ‘D’. else UNIVERSITI TUN HUSSEIN ONN MALAYSIA .iii) Multi Level Selection cont… Answer: if (mark >=80 && mark <=100) grade = ‘A’. else if (mark >= 40 && mark <= 59) grade = ‘C’. else if (mark >=60 && mark <=79) grade = ‘B’. case statement cont… • Use for multi level selection • Similar to if. … default: C++ statement.else.. option case label2: C++ statement. Exit from break. } Functioning like else UNIVERSITI TUN HUSSEIN ONN MALAYSIA .if but use different format Only Integer/ • Format: character value switch( conditional expression/variable) { case label1: C++ statement..iii) Multi Level Selection switch. break.. The value case 2 : cout << “Two\n”. break.. } } UNIVERSITI TUN HUSSEIN ONN MALAYSIA Action . cin>>number. variable switch(number) { case 1 : cout << “One\n”. of break.iii) Multi Level Selection switch. variablecase 3 : cout << “Three\n”. number break. default: cout << “Others”. cout<<“Please insert a number = \n”.case statement cont… Example: void main() { int number. cont… void main() { int number. { case 1 : cout<<“One\n”. else case 3 : cout<< “Three\n”. } default: cout<< “Others”.case statement void main() { int number. break. cout<< “Three\n”. } } UNIVERSITI TUN HUSSEIN ONN MALAYSIA . cin>>number.else if (number == 2) cout<< “Two\n”. else if (number == 3) case 2 : cout<< “Two\n”. break.. if (number == 1) switch(number) cout<< “One\n”. cout<< “Others”. cin>>number.iii) Multi Level Selection switch. break. Exercise Convert the following if.else statement to switch. else cout<< endl<< “Error”. UNIVERSITI TUN HUSSEIN ONN MALAYSIA . else if(colour == ‘b’) cout<< endl<< “blue symbolises unity”... else if(colour == ‘k’) cout<< endl<< “yellow is a royal colour”.case statement if (colour == ‘r’) cout<<endl<<“red symbolise bravery”. break.{ int r. cout << "yellow is a royal colour\n". break. cin>>color. cout << "blue symbolise unity\n". default: cout << "Others". UNIVERSITI TUN HUSSEIN ONN MALAYSIA . k. cout<<"Enter one color = ". case 'b' : case 'k' : break. switch(color) { case 'r' : cout << "red symbolise bravery\n". b. char color. Exercise cont… How will you solve this? Write a C++ program which will accept five integer numbers UNIVERSITI TUN HUSSEIN ONN MALAYSIA . nom2. cin>>nom3. nom5. cout<<"Enter one number = ". cout<<"Enter one number = ". nom4. nom3. cin>>nom4.cont… { int nom1. cout<<"Enter one number = ". cin>>nom5. cout<<"Enter one number = ". return 0. cout << endl << endl. system("pause"). cin>>nom2. } UNIVERSITI TUN HUSSEIN ONN MALAYSIA . cout<<"Enter one number = ". cin>>nom1. while loop .. statements will be repeated. Type of repetition/loop control structures: • While loop • Do.. Repetition will be terminated if the conditional expression is (FALSE). While the value of conditional expression is TRUE.Repitition Control Structure Enable statements to be repeated. while (conditional expression) { IMPORTANT! C++ statement. • Entry controlled loop because condition is checked before the statements in the loop are executed. Counter initialization } 3.2. • Statement will be repeated if the condition is TRUE. 1. the while loop is exited. Counter counter++. When the condition becomes false. Repeated statements Format : counter = 0. Conditional expression testing 4. Increment/decrement of counter .While loop • Use while statement for pre test loop. While loop Example: . Do…while loop Example: nom = 10. } 10 11 12 13 14 15 16 17 18 19 . While i < 20 { cout << ‘\t’<<bil. nom++. While loop Example: . Infinite While loop Example: . Format : counter++.Do…while loop Use do. must include 3 important things { C++ statement. counter = 0. Similar to dowhile. The condition checked at the end of loop. } while Repeated (x < 5).while statement for post test loop.. statements . Repetition will occurred if condition is TRUE otherwise the loop is exited if the condition is FALSE. Do…while loop Example: . Do…while loop Example: nom = 10. do { cout << ‘\t’<<bil. 10 9 8 7 6 . nom--. } while (bil > 5). Infinite Do… While loop Example: . Example: while do.while .. FOR LOOP . • The statements in the for loop repeat continuously for aspecific number of times. or can be supplied by the user. • The coding format is: for(startExpression. countExpression) { block of code. } . testExpression. • The for loop repeats until a specific count is met. • Use a for loop when the number of repetition is know. The while and do-while loops repeat until a certain condition is met. the body of the loop repeats. • The testExpression will evaluate to TRUE (nonzero) or FALSE (zero).• The startExpression is evaluated before the loop begins. While TRUE. This startExpression is evaluated only once at the beginning of the loop. . When the testExpression becomes FALSE. It is acceptable to declare and assign in the startExpression (such as int x = 1.). the looping stops and the program continues with the statement immediately following the for loop body in the program code. Please indent the body of the loop for readability • CAREFUL: When a for loop terminates. When this loop is finished. x++) cout<<"Melody". • Braces are not required if the body of the for loop consists of only ONE statement. the value stored in the computer's memory under the looping variable will be "beyond" the testExpression in the loop. x <= 13. . Consider: for (x = 0. the value 14 is stored in x. It must be sufficiently large (or small) to cause a false condition.• The countExpression executes after each trip through the loop. The count may increase/decrease by an increment of 1 or of some other value. For.doo iii.. Multiple(else if) •.Conclusion • Selection structure i. While ii.loop . Single(if) ii.. Repeatation (loop) i. While. Double(else) iii. ...Kari udang kari ketam Enak dimakan di tepi pantai Kalau korang masih tak faham Harap kawan sebelah jangan dibantai. Sekian.
Copyright © 2024 DOKUMEN.SITE Inc.