170 likes | 343 Views
Control-Flow. Control-Flow. The control flow specifies the sequence in which the statements of a program are executed There are two ways to specify control flow: Branching : Deciding which set of statements will be executed
E N D
Control-Flow The control flow specifies the sequence in which the statements of a program are executed There are two ways to specify control flow: Branching: Deciding which set of statements will be executed Looping: Deciding the number of times a set of statements will be executed (iterations) Branching is done with the “if….else….” statements and the “switch….case….” Statements Looping is done with the “for”, “while” and the “do-while” loops.
if statement syntax if (test expression) { statement(s) to be executed if test expression is true; } Test expression False True Body of if
Use of if statements, Example 1 /* check if a number is less than 0 */ #include <stdio.h> intmain(){ intnum; printf("Enter a number to check.\n"); scanf("%d",&num); if(num < 0) /* checking whether number is less than 0 */ printf("%d is less than 0, ",num);/*If test condition is true */ printf("Other statements, following the if block\n\n"); return 0; }
Use of if statements, Example 2 • What to do if you want to execute more than one statements in the if block ?? • E.g • printf("And it is not greater than 0.\n\n");
if….statement…. Course Grade Example 3 Ask the user to input their total score in a course and output their letter grade based on the following criterion: A 90 – 100% B 80 – 89% C 70 – 79% D 60 – 69% F 59% and below
if….statement…. Course Grade Example 3 /* Muhammad Faisal Amjad if else examples – Course Grade COP 3223H - Spring 2014 */ #include <stdio.h> int main() { float total_score = 0; printf("What is your final score in the course?: "); scanf("%f", &total_score); if(total_score >= 90) // test expression in the if statement is checking for total score { printf("\nYour final score is : %.2f, “, total_score); printf("\nYou got an A\n\n"); } printf("Other statements, following the if block....\n\n"); system("pause"); return 0; }
if….else…. if (<boolean expression>) stmt1; else stmt2; stmtA; stmtB; Check if the boolean expression is true. If so, execute stmt1, then skip to the end of the if and next execute stmtA and stmtB. Otherwise, execute stmt2, and go to the end of the if and next execute stmtA and stmtB.
if….else…. Conditional Construct The if .. else .. construct allows us to execute multiple statements within the if or the else clause. For multiple statements, enclose them in a pair of curly braces {}. if (<boolean expression>) { stmt11; stmt12; . stmtn; } else { stmt21; stmt22; . stmtm; } stmtA; stmtB; Test expression False True Body of else Body of if
if….else…. Number of days in a month Ask the user to input a number representing the month e.g. 12 for December. Output the number of days in that month using if else statements.
switch case • The switch statement is a multi-way decision that tests whether an expression matches one of a number of constant values, and branches accordingly. Statements for first case Is switch expression equal to case constant 1 True Is switch expression equal to case constant 2 True Statements for second case False False Default statements Next statements
switch (expression) { case constant1: statements break; case constant2: statements break; . . default: statements to be executed if expression doesn't match any cases; }
Each case is labeled by one or more integer-valued constants or constant expressions. • If a case matches the expression value, execution starts at that case. • All case expressions must be different. • The case labeled default is executed if none of the other cases are satisfied. • A default is optional; if it isn't there and if none of the cases match, no action takes place at all. • Cases and the default clause can occur in any order. switch (expression) { case constant1: statements break; case constant2: statements break; . . default: statements to be executed if expression doesn't match any cases; }
The break statement • The break statement causes an immediate exit from the switch. • Because cases serve just as labels, after the code for one case is done, execution falls through to the next unless you take explicit action to escape. • break and return are the most common ways to leave a switch.. • break can be used in loops as well.
switch case Example #include <stdio.h> int main(){ int month = 0; printf("Enter month number. E.g. enter 1 for january, 12 for December\n"); scanf("%d", &month); switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: printf("There are 31 days in this month\n"); break; case 4: case 6: case 9: case 11: printf("There are 30 days in this month\n"); break; case 2: printf("There are 28 days in this month\n"); break; default: printf("Invalid month Entered\n\n"); break; } system("pause"); return 0; }