C++ Conditional Statements PDF
Document Details
Uploaded by RewardingBluebell8648
Tags
Summary
This document provides a study guide on conditional statements in C++. It explains the if statement, its syntax, and how to use it for single or multiple statements. The guide also includes examples and diagrams to clarify the concepts.
Full Transcript
STUDY GUIDE C++ CONDITIONAL STATEMENTS 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...
STUDY GUIDE C++ CONDITIONAL STATEMENTS 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