COMSCI 1101 LEC (MIDTERMS) UNIT 5: (PART 1) STATEMENTS Three basic classifications of statements in C programming: Primitives – simple statements Decisions – “Decision Control Structures” Loops – iterative statements Primitive Statement These are also known as simple statement. A simple statement is composed of an expression or a function call followed by a semicolon. The other two types of primitives are: null statement and compound statement. o The Null statement is made up of a null expression followed by a semicolon (a null expression is an empty expression). o A compound statement is formed by placing more than one statement inside curly braces, { }. Syntax of primitive statements Decision Statement Also called as the Decision Control Structure. These statements allows to select and execute specific blocks of code while skipping other sections. Decision statements are actually part of a larger category of statements known as branches. Branch statements allow the programmer to alter the flow of program control. C supports two kinds of branch statements – conditional branches and unconditional branches. Decision statements has 2 basic types: 1. Conditional branches – make decision before branching. o Single decision branch - if/else o Multi-decision branch - switch case 2. Unconditional branches – direct program branching with no decision involved. o The goto statement is an example of an unconditional branch. IF STATEMENT Expression (condition) is evaluated. If expression is true (yields a non-zero value), then statement1 is executed; if expression is false (yields a zero value), then statement1 is not executed. And continue executing statement that follows the if statement (ex. statement2 will then be executed on the figure). IF-ELSE STATEMENT If expression is true, statement1 is executed; if expression is false, skip statement1 then the else branch is followed, and statement2 is executed. And continue executing statement that follows the if- else statement (ex. statement3 will then be executed on the figure). IF-ELSE-IF STATEMENT In an if-else-if statement, the statement in the elseclause of an if-else block is another if-else structure. This cascading of control structures enables a programmer to make more complex selections. SWITCH CASE The expression is evaluated and its value is compared, in turn, to constant A, constant B, etc. If a match is found at constant A, then all statements following that case are executed until a break, or the end of the switch statement is encountered. If no match is found, then the default case (if present) is taken. CODING GUIDELINES 1. Deciding whether to use an if statement or a switch statement is a judgment call. You can decide which to use, based on readability and other factors. 2. An if statement can be used to make decisions based on ranges of values or conditions, whereas a switch statement can make decisions based only on a single integer or character value. Also, the value provided to each case statement must be unique. 3. Unlike with the if statement, the multiple statements are executed in the switch statement without needing the curly braces. 4. When a case in a switch statement has been matched, all the statements associated with that case are executed. Not only that, the statements associated with the succeeding cases are also executed. To prevent the program from executing statements in the subsequent cases, we use a break statement as our last statement. UNIT 5: (PART 1) Iterative Statement One of the fundamental properties of a computer is its ability to repetitively execute a set of statements. These looping capabilities enable the programmer to develop concise programs containing repetitive processes that could otherwise require thousands of program statements to perform. C contains three looping constructs which are also called repetition control structures: o For loop o While loop o Do-while loop for vs. while vs. do-while The while() and for() statements may be described as test-before-execute. If the controlling condition results initially to a zero or false value, the body of the loop is never executed. The do-while() statement, on the other hand, may be considered as executebefore- test. The body of the loop is always executed at least once. Properties of Iterative Statements Initialization Initial value of the variable to be evaluated. Condition Evaluated each and every time before the loop is executed to determine whether the loop will be executed or not. Change of state Increment or Decrement Statement Executed once the condition is met. For Loop It is the most general looping construct. It can merge all the three parts i.e. assignment, condition checking and increment/decrementing. While Loop A while loop has one control expression (a specific condition) and executes as long as the given expression is true. It evaluates the test expression before every loop, so it can execute zero times if the condition is initially false. Do-While Loop It is an “upside down” version of the while loop. Here, the loop condition is tested at the end of the body of the loop. break and continue Statements The statements break and continue are used to interrupt the normal flow of loops and the switch statement. Sometimes when executing a loop, it becomes desirable to leave the loop as soon as a certain condition occurs (perhaps an error condition). break Statements The break statement ends the loop immediately when it is encountered. The break statement is almost always used with if...else statement inside the loop. continue Statements The continue statement skips the current iteration of the loop and continues with the next iteration. continue is the most often used statement to bypass a group of statements inside a loop based on some condition: Unconditional Branch: goto Statement Execution of the goto statement causes a direct unconditional branch to be made to a specified point in the program. The goto statement causes the flow of the program to branch to the statement immediately following the appropriate label. A label is a name that is formed with the same rules as variable names and must be immediately followed by a colon.