lect3_1

lect3_1 - Control Structures Selection Statements 1 Control...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Control Structures - Selection Statements 1 Control Structures: Selection Statements Any C++ program can be written using only the following three control structures. (1) sequence (2) selection (3) repetition Sequential statements: These statements are executed one after another in the order in which they appear. Selection statements: These statements provide a program the ability to make decisions by evaluating a logical expression and based on that evaluation, the program decides which statements are to be executed next. Repetition statements: These statements provide a program the ability to execute repeatedly the same set of statements. We have seen the nature of sequential statements in our first program and later programs discussed so far. Now we disscuss the seletion statements. The if statement We can use an if statement whenever we want the program to perform some action only when a certain condition is true. Control Structures - Selection Statements 2 Consider the following code segment // assume grade is a variable of char type if ( grade == ’A’ ) cout << "WOW!!!"; For example, in the above code if the variable grade contains the character ’A’, then only the message ”WOW!!!” is output. Note that if grade is not equal to ’A’, no action is taken. We used the relational operator (==) to test for equality. Note that comparison operator (==) is different from the assignment operator (=). The general form of the if statement is if (condition) statement to be executed • The condition shown inside the parentheses must be a logical expression (also called a boolean expression ) whose value is either true or false . For example, grade == ’A’ is true if the variable grade contains the letter ’A’ and is false otherwise. • The condition must be enclosed in parentheses. If the condition is true , the program executes the statement following the if statement and then proceeds to the next statement in the program. If the condition is false , it proceeds directly to the next statement in the program without executing the statement. Control Structures - Selection Statements 3 For example the program include <iostream> using namespace std; int main() { char grade; cout << "Input grade received:"; cin >> grade; if (grade == ’A’) cout << "WOW!!!"<< endl; cout << "Have a nice day!"<< endl; return 0; } in one run ouputs, Input grade received: A ← WOW!!! Have a nice day! and in another run the output is in one run ouputs, Input grade received: B ← Have a nice day! Control Structures - Selection Statements 4 Relational operators C++ has six relational operators for comparing data values....
View Full Document

{[ snackBarMessage ]}

Page1 / 14

lect3_1 - Control Structures Selection Statements 1 Control...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online