Chapter 2 Control Instructions .pdf

Document Details

Uploaded by Deleted User

2022

Tags

C programming control instructions decision structures programming concepts

Full Transcript

DBV10023 Basic C Programming CHAPTER 2: CONTROL INSTRUCTIONS Muhammad Muhyiddin bin Mohamad Ibrahim Jan – Jun 2022 Outline Introduction Learning Objectives Learning Outcomes Decision Control Structure “if ” Statemen...

DBV10023 Basic C Programming CHAPTER 2: CONTROL INSTRUCTIONS Muhammad Muhyiddin bin Mohamad Ibrahim Jan – Jun 2022 Outline Introduction Learning Objectives Learning Outcomes Decision Control Structure “if ” Statement “if-else” Statement Nested “if-else” Statement “switch” Statement Repetition (Iteration) Structure “while” Statement “do..while” Statement “for” Statement Introduction Enable a user to specify the order in which the various instructions in a program are to be executed by the computer - determine the ‘flow of control’ in a program. Three types of control instructions (constructs) in C: Sequence Control Instruction Selection or Decision Control Instruction Repetition or Loop Control Instruction Learning Objectives Explore the uses of the keywords such as; if, if else, switch, while, do while and for statements to implement the control instruction. Differentiate the usage of the control instructions. Discover the relationship between the control instructions with graphical representation methods. Learning Outcomes After completing the unit, students should be able to: Describe that there are three ways for taking decisions in a program. First way is to use the if-else statement, second way is to use the conditional operators and third way is to use the switch statement. Understand that there are three types of loops available in C. Those are for, while, and do- while. Interrelate and utilize the decision and looping statements. Draw and relate between the statements used and flowcharts to represent the overall programs. Differentiate among the types of loops and decision control statements available to be used in solving problems. Decision Control Structure Decision Switch If Family Family Nested if if-else if-else switch “if ” Statement syntax for the if statement is: if ( this condition is true ){ Y condition Execute this statement execute this statement ; N } if instruct the compiler that what follows is a decision control instruction. condition following the keyword if is always enclosed within a pair of parentheses. true, then the statement is executed. not true then the statement is not executed; instead the program skips the statement. “if ” Statement condition is expressed using the C’s ‘relational’ operators - used to compare two values to see whether the condition are equal to each other, unequal, or whether one is greater than the other. “if ” Statement “if ” Statement #include int main( ) { int num ; printf ( "Enter a number less than 5:\t" ) ; scanf ( "%d", &num ) ; if ( num 0) { printf ("That number is positive!\n"); } else { printf ("That number is negative or zero!\n"); } } Nested “if-else” statement There is a need to create multiple choice expressions if (condition_1) using the if-else statement { syntax for the if- else statement is: Execute statement 1; } Y else if (condition_n) condition_1 Execute statement 1 { Execute statement n; N } Y condition_n Execute statement n else { N Execute statement else Execute statement 1 } Nested “if-else” statement Start PRINT Enter a number between 1 and 10 : INPUT number N N PRINT The number < 1 number>10 End number is Y Y PRINT The PRINT The number is number is Nested “if-else” statement #include main() { int number; printf("Enter a number between 1 and 10 :\t"); scanf("%d", &number); if( number < 1 ) { printf("Number is below 1. Please re-enter\n"); } else if( number > 10 ) { printf("Number is above 10. Please re-enter\n"); } else { printf("The number is %d\n", number ); } } Nested “if-else” statement Start PRINT Enter two numbers and an operator in the format PRINT number1 operator number2 INPUT number1 operator number2 N N N N PRINT Invalid operator * operator / operator + operator - operator Y Y Y Y result = number1 * number2 result = number1 / number2 result = number1 + number2 result = number1 - number2 PRINT number1 PRINT number1 PRINT number1 PRINT number1 operator number2 operator number2 operator number2 operator number2 result result result result End Nested “if-else” statement #include else if(operator == '+’) main() { { result = number1 + number2; char operator; printf("%f %c %f is %f\n", number1, operator, number2, result ); float number1, number2, result; } printf("Enter two numbers and an operator in the format\n"); else if(operator == '-’) printf(" number1 operator number2\n"); { scanf("%f %c %f", &number1, &operator, &number2); result = number1 - number2; if(operator == '*’) printf("%f %c %f is %f\n", number1, operator, number2, result ); { } result = number1 * number2; else printf("%f %c %f is %f\n", number1, operator, number2, result ); { } printf("Invalid operator.\n"); else if(operator == '/’) } { } result = number1 / number2; printf("%f %c %f is %f\n", number1, operator, number2, result ); } “switch” Statement make a decision from number of choices is called a switch, or more correctly a switch- case-default statement. syntax for the switch statement is: switch (expression) { Y value1 Execute statement 1 case value1: Execute statement 1; break; N case valuen: Execute statement n; Y break; valuen Execute statement n default: Execute statement default; break; N } Execute statement default keyword break must be included at the end of each case statement. default clause is optional, and is executed if the cases are not met. “switch” Statement Start PRINT Enter two numbers and an operator in the format PRINT numb1 operator numb2 INPUT numb1 operator numb2 N N N N PRINT Invalid operator * operator / operator + operator - operator Y Y Y Y result = numb1 * numb2 result = numb1 / numb2 result = numb1 + numb2 result = numb1 - numb2 PRINT numb1 PRINT numb1 PRINT numb1 PRINT numb1 operator numb2 operator numb2 operator numb2 operator numb2 result result result result End “switch” Statement #include switch( menu ) main() { { case 1: total = numb1 + numb2; int menu, numb1, numb2, total; printf("%d plus %d is %d\n", numb1, numb2, total ); printf("enter in two numbers -->"); break; scanf("%d %d", &numb1, &numb2 ); case 2: total = numb1 - numb2; printf("enter in choice\n"); printf("%d minus %d is %d\n", numb1, numb2, total ); printf("1=addition\n"); break; printf("2=subtraction\n"); default: printf("Invalid option selected\n"); scanf("%d", &menu ); break; } } Repetition (Iteration) Structure commonly called a “loops” can be executed several times in a main program Loop while do..while for “while” Statement syntax for the while statement is: while (condition) { False statement (s); } condition is first tested TRUE : statements within the loop will be executed FALSE : loop will not be executed at all. condition will be tested repeatedly until the condition is FALSE “while” Statement #include main( ) { int p, n, count =1; float r, si ; while ( count

Use Quizgecko on...
Browser
Browser