Vous êtes sur la page 1sur 15

Computer Programming

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

 Execute code based on a condition(s).


 There are three selection statements supported by C++
 1. If

 2. If else

 3. Switch
Selection Statements

 If statement: Performs an action if a condition is true or skips the action if


the condition is false.
 If statement is a single selection statement
Syntax:
if( Test expression /condition )
statement; // Single - statement if body

if ( Test expression ) {
statement;
statement; // Multiple Statement if body
statement;
}
If statement Example

void main() {

int num= 10;


if( num > 0)
{
cout<<“It’s a positive number”;
}}
Selection Statements
If ...else statement : Performs an action if the condition is true
and performs a different action if the condition is false.
If statement is a double selection statement

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() {

int num= -10;

if(num > 0) {
cout<<“It’s a positive number”;
}
else {
cout<<“It’s a negative number”;
}
}
}
Selection Statements

 Nested if : if statement within an other if statement


 A nested if is an if statement that is the target of another
if or else.
if(condition1) {
if(condition2) {
}
if(condition3) {
}
}
Example
void main(){
Int num= 15;
if(num> 10) {
if(num<20) {
Cout<<“number is between 10 to 20”;
Cout<<“end of inner if”;
}
Cout<<“end of outer if”;
}
Cout<<“end of program”;
}
}
Selection Statements

 If . . .else if construction /ladder


if(condition)
statement;
else if(condition)
statement;
else if(condition)
statement;
.
.
else
statement;
Example
void main(){
String designation = “boss”;
if(designation == “boss”) {
Cout<<“hello boss!”; }
else if(designation == “manager”) {
Cout<<“hi sir!”; }
else if(designation == “office boy”) {
Cout<<“bring me cup of water chump!”; }
else {
Cout<<“Good By ”;
}
}
}
Switch statement:
is a multiple selection statement,
performs one of different actions case value2:
// statement sequence
Default keyword break;
If none of the constants matches the ...
value of the expression, then the default
statement is executed.
case valueN:
// statement sequence
The default statement is optional Break;
Syntax: default:
switch (expression) { // default statement sequence
case value1: }
// statement sequence
break;
Switch statement Example:
Class SwitchTest{
public static void main(String args[]){
Int temperature = 10;
switch(temperature){
case 0:
System.out.println(“its freezing”);
break;
case 30:
System.out.println(“it’s a sunny day”);
break;
default:
System.out.println(“invalid temperature”);
break;
}
}
}
Tasks for Lab # 6
Task # 1 Write a C++ program that takes a single character as input and tells whether it is a vowel or a
consonant. (Use Switch Statement)

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:

10000 –20000 Manager Operations


20000 –30000 Manager
30000 –40000 Area Manager
40000 –50000 Regional Manager

Vous aimerez peut-être aussi