Lec 8 Control Structures
Lec 8 Control Structures
CONTROL STRUCTURES
IT2-Lecture 8
Control Structures
The if Statement
Syntax
if(boolean_expression) {
// statement(s) will execute if the boolean expression is true
}
If the boolean expression evaluates to true, then the block of code inside the if
statement will be executed. If boolean expression evaluates to false, then the
first set of code after the end of the if statement (after the closing curly brace)
will be executed.
Example:
#include <iostream>
using namespace std;
Page 1 of 8
POLYTECHNIC COLLEGE OF LA UNION
INFORMATION TECHNOLOGY DEPARTMENT
int main () {
int a = 10;
if( a < 20 ) {
cout << "a is less than 20;" << endl;
}
cout << "value of a is : " << a << endl;
return 0;
}
When the above code is compiled and executed, it produces the following
result –
Syntax
if(boolean_expression) {
// statement(s) will execute if the boolean expression is true
} else {
// statement(s) will execute if the boolean expression is false
}
Page 2 of 8
POLYTECHNIC COLLEGE OF LA UNION
INFORMATION TECHNOLOGY DEPARTMENT
If the boolean expression evaluates to true, then the if block of code will be
executed, otherwise else block of code will be executed.
Example:
#include <iostream>
using namespace std;
int main () {
int a = 100;
if( a < 20 ) {
cout << "a is less than 20;" << endl;
} else {
cout << "a is not less than 20;" << endl;
}
cout << "value of a is : " << a << endl;
return 0;
}
When the above code is compiled and executed, it produces the following
result –
Page 3 of 8
POLYTECHNIC COLLEGE OF LA UNION
INFORMATION TECHNOLOGY DEPARTMENT
When using if , else if , else statements there are few points to keep in mind.
An if can have zero or one else's and it must come after any else if's.
An if can have zero to many else if's and they must come before the else.
Once an else if succeeds, none of he remaining else if's or else's will be
tested.
Syntax
if(boolean_expression 1) {
// Executes when the boolean expression 1 is true
} else if( boolean_expression 2) {
// Executes when the boolean expression 2 is true
} else if( boolean_expression 3) {
// Executes when the boolean expression 3 is true
} else {
// executes when the none of the above condition is true.
}
Example
#include <iostream>
using namespace std;
int main () {
int a = 100;
if( a == 10 ) {
Page 4 of 8
POLYTECHNIC COLLEGE OF LA UNION
INFORMATION TECHNOLOGY DEPARTMENT
return 0;
}
When the above code is compiled and executed, it produces the following
result −
Value of a is not matching
Syntax
switch(expression) {
case constant-expression :
statement(s);
Page 5 of 8
POLYTECHNIC COLLEGE OF LA UNION
INFORMATION TECHNOLOGY DEPARTMENT
break; //optional
case constant-expression :
statement(s);
break; //optional
Page 6 of 8
POLYTECHNIC COLLEGE OF LA UNION
INFORMATION TECHNOLOGY DEPARTMENT
Example
#include <iostream>
using namespace std;
int main () {
// local variable declaration:
char grade = 'D';
switch(grade) {
case 'A' :
cout << "Excellent!" << endl;
break;
case 'B' :
case 'C' :
cout << "Well done" << endl;
break;
case 'D' :
cout << "You passed" << endl;
break;
case 'F' :
cout << "Better try again" << endl;
break;
default :
cout << "Invalid grade" << endl;
}
cout << "Your grade is " << grade << endl;
return 0;
}
This would produce the following result –
Page 7 of 8
POLYTECHNIC COLLEGE OF LA UNION
INFORMATION TECHNOLOGY DEPARTMENT
You passed
Your grade is D
Page 8 of 8