






























































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!































































Prof Muliaro Wafula Program Control Statements- Prof Muliaro Wafula 1
Program Control Statements/Constructs Selection/Branching Conditional Type if if-else if-else- if switch Unconditional Type break continue (^) goto Iteration/Looping for while do- while
If an expression, involving the relational operator, is true, it is given a value of 1. If an expression is false, it is given a value of 0. Similarly, if a numeric expression is used as a test expression, any non-zero value (including negative) will be considered as true, while a zero value will be considered as false. Space can be given between operand and operator (relational or logical) but space is not allowed between any compound operator like <=, >=, ==, !=. It is also compiler error to reverse them. a == b and a = b are not similar, as == is a test for equality, a = b is an assignment operator. Therefore, the equality operator has to be used carefully. The relational operators have lower precedence than all arithmetic operators. POINTS TO NOTE
➢ Program begins execution at the main() function. ➢ Statements within the main() function are then executed from top-down style, line-by-line. ➢ However, this order is rarely encountered in real C program. ➢ The order of the execution within the main() body may be branched. ➢ Changing the order in which statements are executed is called program control. ➢ Accomplished by using program control statements. ➢ So we can control the program flows. Program Control Statements- Prof Muliaro Wafula
▪ Take a look at the following example Jump/branch to printf() (^) Back to main() from printf() Program Control Statements- Prof Muliaro Wafula
float paidRate = 5.0, totalPaid, paidHours = 25; S sumPaid = paidHours * paidRate; S printf("Paid Amount = $%.2f \n", totalPaid); S return 0; S ▪ One entry point and one exit point. ▪ Conceptually, a control structure like this means a sequence execution. Program Control Statements- Prof Muliaro Wafula
Selection: if, if-else, if-else-if ▪ Starting from the most basic if syntax, if (condition) if (condition) statement; { statements;} next_statement; next_statement; 1.(condition) is evaluated.
A PROGRAM TO FIND THE LARGEST AMONG THREE NUMBERS USING THE NESTED LOOP #include <stdio.h> int main() { int a, b, c; printf(“\nEnter the three numbers”); scanf(“%d %d %d”, &a, &b, &c); if(a > b) if(a > c) printf(“%d”, a); else printf(“%d”, c); else if(b > c) printf(“%d”, b); else printf(“%d”, c); return 0; }
if (condition) if (condition) statement_1; { a block of statements;} else else statement_2; { a block of statements;} next_statement; next_statement; Explanation:
For example: if(myCode == '1') rate = 7.20; else rate = 12.50; If myCode is equal to '1', the rate is 7.20 else, if myCode is not equal to '1' the rate is 12.50. Equal/not equal (=) is not a value comparison, but a character comparison!!! Program Control Statements- Prof Muliaro Wafula
▪ In this nested form, condition_1 is evaluated. If it is zero (FALSE), statement_1 is executed and the entire nested if statement is terminated. ▪ If non-zero (TRUE), control goes to the second if (within the first if) and condition_2 is evaluated. ▪ If it is zero (FALSE), statement_2 is executed; if not, control goes to the third if (within the second if) and condition_3 is evaluated. ▪ If it is zero (FALSE), statement_3 is executed; if not, statement_4 is executed. The statement_4 (inner most) will only be executed if all the if statement are TRUE. ▪ Again, only one of the statements is executed other will be skipped. ▪ If the else is used together with if, always match an else with the nearest if before the else. ▪ statements_x can be a block of codes and must be put in curly braces. Program example: nested if-else Program Control Statements- Prof Muliaro Wafula
▪ The if-else-if statement has the following form (3 levels example). if(condition_1) statement_1; else if (condition_2) statement_2; else if(condition_3) statement_3; else statement_4; next_statement; Program Control Statements- Prof Muliaro Wafula
The if-else-if program example ▪ If mark is less than 40 then grade 'F' will be displayed; if it is greater than or equal to 40 but less than 50, then grade 'E' is displayed. ▪ The test continues for grades 'D', 'C', and 'B'. ▪ Finally, if mark is greater than or equal to 80, then grade 'A' is displayed. Program Control Statements- Prof Muliaro Wafula
Selection: The switch-case-break ▪ The most flexible selection program control. ▪ Enables the program to execute different statements based on an condition or expression that can have more than two values. ▪ Also called multiple choice statements. ▪ The if statement were limited to evaluating an expression that could have only two logical values: TRUE or FALSE. ▪ If more than two values, have to use nested if. ▪ The switch statement makes such nesting unnecessary. ▪ Used together with case and break. Program Control Statements- Prof Muliaro Wafula