Docsity

Program Control Statements: if, else, for, while, switch and break by Prof. Muliaro Wafula, Summaries of Computer Programming

Partial preview of the text

Download Program Control Statements: if, else, for, while, switch and break by Prof. Muliaro Wafula and more Summaries Computer Programming in PDF only on Docsity!

Program Control Statements

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

STATEMENTS/CONSTRUCTS IN ‘C’

 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.

  1. If TRUE (non-zero) the statement is executed.
  2. If FALSE (zero) the next_statement following the if statement block is executed.
  3. So, during the execution, based on some condition, some codes were skipped. Program Control Statements- Prof Muliaro Wafula

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:

  1. The (condition) is evaluated.
  2. If it evaluates to non-zero (TRUE), statement_1 is executed, otherwise, if it evaluates to zero (FALSE), statement_2 is executed.
  3. They are mutually exclusive, meaning, either statement_1 is executed or statement_2, but not both.
  4. statements_1 and statements_2 can be a block of codes and must be put in curly braces. Program Control Statements- Prof Muliaro Wafula

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