Lecture 25 - Selection. In the previous lecture we have been covered algorithms and programs, in this chapter you will be able to understand: The if statement, the else statement, cascaded if, nested if, switch statement.
Selection Lecture 25 Summary of previous lecture In the previous lecture we have been covered, Algorithms and Programs A C programming language History of C C, A High level language How to get started with C Basic Structure of a C program Data Storage and Data Types Variables, Keywords, identifiers, Assignment Summary of previous lecture constant variable printf() and scanf() functions and usage Precedence int and float Unary operations Increment and decrement operations Comments Error and its Types Today’s Topics The if statement The else statement Cascaded if Nested if Switch statement Conditions We come across conditions in daily life, For example If you will get less than 50 marks then you will fail If team scored less than 250 runs, it will be out of the tournament If the temperature is greater than 50 degree Celsius then wear light dress Conditions in C Programming Same if structure is used in c programming to test the condition It has two parts Conditional or antecedent or LHS part and Conclusion or RHS part action or consequent or The if statement Determines whether a statement or block is executed Implements the selection instructions within an algorithm Decides what to by evaluating a Boolean expression If the expression is true (non-zero), the statement or block is executed if ( expression ) statement What is a statement? Statements are lines of instructions in our programs ending with a semicolon (;) A compound statement or block is a series of statements surrounded by braces E.g { number = number + 1; printf("%d\n", number); } • An empty statement is a single semicolon Example: oddnum.c Read in a number, and print it if it is odd output “Enter an integer” input number if (number is odd) then { output the number } Example: oddnum.c Read in a number, and print it if it is odd #include /* Read in a number, and echo it if it is odd */ int main() { output “Enter an integer” input number if (number is odd) then { output the number } return 0; } Switch statement printFlag is a variable In order to stop next case to be automatically executed, break statement has been used Multiple cases being executed together Summary of switch Statement Rules Develop the program for assigning the grades using if and switch statements separately #include Switch statement Remember you need constants here, so explicitly write all statements for which this case may be executed int main() { int marks; printf("%s", "Enter yours Marks"); scanf("%d",&marks); switch(marks) { case 100:case 99:case 98:case 97:case 96:case 95:case 94: case 93:case 92:case 91:case 90: printf("%s", "You got A grade"); break; In case any of these cases execute, control from case statement will be returned case 89:case 88:case 87:case 86:case 85:case 84: case 83:case 82:case 81:case 80: printf("%s", "You got B grade"); break; case 79:case 78:case 77:case 76:case 75:case 74: case 73:case 72:case 71:case 70: printf("%s", "You got C grade"); break; case 69:case 68:case 67:case 66:case 65:case 64: case 63:case 62:case 61:case 60: printf("%s", "You got D grade"); break; default: printf("%s", "You got F grade"); break; } If user enters some wrong value default will be executed! return 0; } Alternative If statements #include int main() { int marks; User inputs marks First Check, if true rest of the statements will not be executed printf("%s", "Enter yours Marks"); scanf("%d",&marks); if (marks >=90) { printf("%s", "you got A grade"); } If first check is false, this statement will be checked and so on! else if (marks >=80) { printf("%s", "you got B grade"); } Logical comparison in if statement, which is not permitted in case else if (marks >=70) { printf("%s", "you got c grade"); } else if (marks >=60) { printf("%s", "you got D grade"); } else printf("%s", "you got F grade"); return 0; } Integer value is returned by this program! Summary If conditions are used to execute statement or a group of statements based upon logically tested values Alternative to if condition is switch statement Both if and switch statements form logical structure of a program ...Summary of previous lecture In the previous lecture we have been covered, Algorithms and Programs A C programming language ... Conditions We come across conditions in daily life, For example If you will get less than 50 marks then you will fail If team scored less than 250 runs, it will be out of the tournament If... Data Storage and Data Types Variables, Keywords, identifiers, Assignment Summary of previous lecture constant variable printf() and scanf() functions and usage Precedence int and float