Computer Programming in C++: Practical#06)
Computer Programming in C++: Practical#06)
Computer Programming in C++: Practical#06)
in
C++
(PRACTICAL#06)
Objective: To become familiar with Control Structures
(Selection / Conditional Statements)
The execution of the program is linear.
Control Structures are used to alter/change the flow of
program.
Controls which statement(s) should be executed next.
Control Structures
Selection / Iterative/
Conditional Loops
Selection Statements
2. If else
3. Switch
Selection Statements
if ( Test expression ) {
statement;
statement; // Multiple Statement if body
statement;
}
If statement Example
void main() {
Syntax:
if( Test expression)
Statement; // Single - statement if body
else
Statement; // Single - statement else body
if ( Test expression ) {
Statement;
statement; } // Multiple Statement if body
else {
Statement;
statement; } // Multiple Statement else body
Example
void main() {
if(num > 0) {
cout<<“It’s a positive number”;
}
else {
cout<<“It’s a negative number”;
}
}
}
Selection Statements
Task # 2 Write a program that takes two numbers as input and prints which one is greater.
Task # 3: Write a program that declares an int variable, assign it a value of your choice. The program checks
as:
if the number is even make it odd number.
if the number is odd prints its square.
if the number is equal to 0 make it a double figure number and prints its cube.
Tasks for Lab # 6
Task # 4: Write a program that prints the designation of the employee according to their pay scale: