Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19
COMPUTER
PROGRAMMING 1 SLIDESMANIA.C
Mary Grace Cuyno-Lozada, MIT
CC 102 Instructor Decision Control Statements SLIDESMANIA.C Decision Control Statements if Statement if-else Statement if-else-if Statement switch-case Statement SLIDESMANIA.C if Statement the simplest decision control statement that is frequently used in decision making. SLIDESMANIA.C if Statement Construct SLIDESMANIA.C NOTE: In case the statement block contains only one statement, putting curly brackets becomes optional. If there are more than one statement in the statement block, putting curly brackets becomes mandatory. SLIDESMANIA.C if-else Statement used to separate set of statements to be executed if the expression returns a false value. SLIDESMANIA.C if-else Statement Construct SLIDESMANIA.C if-else-if Statement test additional conditions apart from the initial test expression. SLIDESMANIA.C if-else-if Statement Construct SLIDESMANIA.C switch-case Statement a multi-way decision statement that is a simplified version of an if-else-if block. SLIDESMANIA.C switch-case Statement Construct SLIDESMANIA.C The power of nested if-else-if statements lies in the fact that it can evaluate more than one expression in a single logical structure. switch statements are mostly used in two situations: When there is only one variable to evaluate in the expression When many conditions are being tested for SLIDESMANIA.C When there are many conditions to test, using the if and else-if constructs becomes complicated and confusing. Therefore, switch case statements are often used as an alternative to long if statements that compare a variable to several ‘integral’ values (integral values are those values that can be expressed as an integer, such as the value of a SLIDESMANIA.C
char). Switch statements are also used to handle
the input given by the user. When there are many conditions to test, using the if and else-if constructs becomes complicated and confusing. Therefore, switch case statements are often used as an alternative to long if statements that compare a variable to several ‘integral’ values (integral values are those values that can be expressed as an integer, such as the value of a SLIDESMANIA.C
char). Switch statements are also used to handle
the input given by the user. Default is the case that is executed when the value of the variable does not match with any of the values of the case statements. That is, default case is executed when no match is found between the values of switch and case statements and thus there are no statements to be executed. Although the default case is optional, it is always SLIDESMANIA.C
recommended to include it as it handles any
unexpected case. The break statement must be used at the end of each case because if it is not used, then the case that matched and all the following cases will be executed. The break statement tells the compiler to jump out of the switch case statement and execute the statement following the switch-case construct. Thus, the keyword break is used to SLIDESMANIA.C
break out of the case statements.
Advantages of Using a switch-case Statement Easy to debug Easy to read and understand Ease of maintenance as compared to its equivalent if-else statements Like if-else statements, switch statements can SLIDESMANIA.C
also be nested Executes faster than its equivalent if-else Questions? SLIDESMANIA.C