LAB Exercise 5 - practice PDF

Title LAB Exercise 5 - practice
Author Louis Poquiz
Course Bachelor of Science in Electrical Engineering
Institution FEU Institute of Technology
Pages 25
File Size 1.2 MB
File Type PDF
Total Downloads 40
Total Views 158

Summary

practice...


Description

COLLEGE OF COMPUTER STUDIES

CCS0003L (COMPUTER PROGRAMMING 1)

EXERCISE

5 CONDITIONAL CONTROL STRUCTURE

score

Name of Student

Name of Professor

Date Performed

Date Submitted

I.

OBJECTIVES

At the end of this exercise, students must be able to: • •

II.

Enumerate the different types of control structures Know how to use the following Conditional Statements: if statement if statement if-else statement if-else-if statement Nested if statement Switch statement BACKGROUND INFORMATION

A program is usually not limited to a linear sequence of instructions. During its process it may bifurcate, repeat code or take decisions. For that purpose, C++ provides control structures that serve to specify what has to be done to perform our program. With the introduction of control sequences we are going to have to introduce a new concept: the block of instructions. A block of instructions is a group of instructions separated by semicolons (;) but grouped in a block delimited by curly bracket signs: { and }. Conditional structure: if and else The general decision making capability in C++ is provided by the if statement. The format of this statement is if (expression) program statement You will see what sort of `expression' you need to use in the following section. The program statement may be a single statement or may be a block of statements enclosed within braces `{}'. If it is a single statement the format is if (expression) single statement; but if more statements are to be executed as a compound statement or program block the format becomes if (expression) { statement1; statement2; //..... and so on as long as you want

statement_last; } A program block, or compound statement, is a series of statements places inside curly braces {}. When the if statement is true, then all the statements inside the {} are run. When the if statement is false, then none of them are run and the program jumps to the next statement after the closing brace `}' The condition to be tested appears in parenthesis like this ()after the keyword if. If the condition is false the following statement, or compound statement, is ignored but if it is true it is executed. Schematic Representation of an if statement

If there is an integer numeric expression in the brackets () such as if (total) { ... then a value of zero for total is considered FALSE and the following statements will not be executed. If the value of total is non-zero it is regarded as TRUE. This is almost certainly wrong: if (expression); // ends with semicolon It is used to execute an instruction or block of instructions only if a condition is fulfilled. Its form is: if (condition) statement where condition is the expression that is being evaluated. If this condition is true, statement is executed. If it is false, statement is ignored (not executed) and the program continues on the next instruction after the conditional structure. For example, the following code fragment prints out x is 100 only if the value stored in variable x is indeed 100: if (x == 100) cout...


Similar Free PDFs