Programming Language C++ Lecture 3
Control Structures C++ provides control structures that serve to specify what has to be done to perform our program. Control structures are used to control the flow of execution in a program All programs could be written in terms of only three control structures: the sequence structure 2
3 - Programs executed sequentially by default the selection structure - if, if/else, switch the repetition structure - while, do/while, for
Control Structures C++ provides three types of selection statements The if selection statement is a single-selection statement because it selects or ignores a single action (or, as we’ll soon see, a single group of actions). The if…else statement is called a double-selection statement because it selects between two different actions (or groups of actions). The switch and (nested if) selection statement is called a multiple- selection statement because it selects among many different actions (or groups of actions). 4
Control Structures C++ provides three types of repetition statements ( Lecture 4) 5
Conditional Statements (if and if…else) 6
if Selection Structure Choose among alternative courses of action example: -If student’s grade is greater than or equal to 40 Print “Passed” – If the condition is true Print statement executed, program continues to next statement – If the condition is false Print statement ignored, program continues – Indenting makes programs easier to read C++ ignores whitespace characters (tabs, spaces, etc.) 7
The syntax for the if statement is as follows: if (condition) { statement(s); } => If the Condition is true, then the compiler would execute the Statement. 8
Conditions: Conditions are expressions that evaluate to a boolean value — a true or false value (true and false are C++ keywords, representing the two possible values of a Boolean expression or variable). Simple conditions involve two operands, each of which can be a variable or a literal value, and an operator, typically a comparison operator. 9
== : true if and only if left operand is equal to right operand != : true if and only if left operand is not equal to right operand > : true if and only if left operand is greater than right operand < : true if and only if left operand is less than right operand >= : true if and only if left operand is greater than or equal than right operand <= : true if and only if left operand is less than or equal than right operand 10 The comparison operators are shown below:
C++ codes: If student’s grade is greater than or equal to 60 Print “Passed” if ( grade >= 60 ) cout << "Passed"; 11
Examples: 1)if ( a < 0 ) { cout<< “a is negative”; } 2)if ( a > 0 ) { cout<<“a is positive”; } 3)if ( a == 0 ) { cout<< "a is null”; } 4) if( a < b ) { // if condition is true then print the following cout << "a is less than b" << endl; } 12
Important: The if statement must NOT have a semicolon at the end of the line (i.e., after the closing bracket for the condition) This is one of the common mistakes that can give you a really hard time before identifying it. => The compiler sees that there is a statement after the closing bracket for the condition. if (number < 0) ; { cout << "Negative number" << endl; } 13
if/else Selection Structure The simple if statement covers the cases where we have a fragment of code that should be executed depending on a condition. If we have a situation where we have two possible actions, and we want to do one or the other, depending on a given condition, we use the if – else statement. 14
if/else Selection Structure if – Performs action if condition true if/else – Different actions if conditions true or false example if student’s grade is greater than or equal to 60 print “Passed” else print “Failed” C++ code 15 if ( grade >= 60 ) cout << "Passed"; else cout << "Failed";
if/else Selection Structure Ternary conditional operator (?:) – Three arguments (condition, value if true, value if false) Code could be written: cout = 60 ? “Passed” : “Failed” ); 16 conditionValue if trueValue if false
The syntax for the if-else statement is : If (condition) { // block 1 } Else { //block 2 } => In the above, if the condition is true, then block 1 is executed, and then execution continues after the end of block 2; if the condition is false, then block 2 is executed (block 1 is skipped), and then execution continues after the end of block 2. 17
Compound statement – Set of statements within a pair of braces if ( grade >= 60 ) cout << "Passed.\n"; else { cout << "Failed.\n"; cout << "You must take this course again.\n"; } – Without braces, cout << "You must take this course again.\n"; always executed Block – Set of statements within braces
Example: if ( age < 100 ) { // If the age is less than 100 cout<<"You are pretty young!\n"; } else { cout<<"You are old\n"; } 19
Else If (nested if) : Another use of else is when there are multiple conditional statements that may all evaluate to true, yet you want only one if statement's body to execute. You can use an "else if" statement following an if statement and its body; that way, if the first statement is true, the "else if" will be ignored, but if the if statement is false, it will then check the condition for the else if statement. If the if statement was true the else statement will not be checked. It is possible to use numerous else if statements to ensure that only one block of code is executed. 20
Nested if/else structures – One inside another, test for multiple cases – Once condition met, other statements skipped if student’s grade is greater than or equal to 90 Print “A” else if student’s grade is greater than or equal to 80 Print “B” else if student’s grade is greater than or equal to 70 Print “C” else if student’s grade is greater than or equal to 60 Print “D” else Print “F”
if ( ) { // Execute these statements if is TRUE } else if ( ) { // Execute these statements if is TRUE and // is FALSE } The syntax : 22
Example if ( grade >= 90 ) // 90 and above cout << "A"; else if ( grade >= 80 ) // cout << "B"; else if ( grade >= 70 ) // cout << "C"; else if ( grade >= 60 ) // cout << "D"; else // less than 60 cout << "F";
Example 24 if (x > 0) { cout << "x is positive"; } else if (x < 0) { cout << "x is negative"; } else { cout << "x is 0"; }
switch structure switch structure: alternate to if-else switch (integral) expression is evaluated first Value of the expression determines which corresponding action is taken Expression is sometimes called the selector
26
switch structure -One or more statements may follow a case label. - Braces are not needed to turn multiple statements into a single compound statement. -switch, case, break, and default are reserved word.
28
Avoiding Bugs by Avoiding Partially Understood Concepts and Techniques: To output results correctly The switch structure must include a break statement after each cout statement
Program: Effect of break statements in a switch structure
Grade program with bugs
Exercice : write a c + + program that compare two values (num1 and num2) using the operators we have seen ( using conditional statements ) 32