You are on page 1of 15

The selection control structure allows one set of statements to be executed if a condition is

true and another set of actions to be executed if a condition is false.

Repetition structures, or loops, are used when a program needs to repeatedly process one or
more instructions until some condition is met, at which time the loop ends.

A nested if is an if statement that is the target of another if statement. Nested if statements


means an if statement inside another if statement. Yes, C++ allows us to nest if statements
within if statements. i.e, we can place an if statement inside another if statement.

if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}

Switch case statements are a substitute for long if statements that compare a variable to
several integral values

 The switch statement is a multiway branch statement. It provides an easy way to


dispatch execution to different parts of code based on the value of the
expression.
 Switch is a control statement that allows a value to change control of execution.

 switch (n)
 {
 case 1: // code to be executed if n = 1;
 break;
 case 2: // code to be executed if n = 2;
 break;
 default: // code to be executed if n doesn't match any cases
 }

 operator && evaluates left operand first and if the value is logically false then
it avoids evaluating the right operand. Typical use is for example if (x > 0 &&
k/x < limit) ... that avoids division by zero problems.

 operator || evaluates left operand first and if the value is logically true then
it avoids evaluating the right operand. For example if (overwrite_files ||
confirm("File existing, overwrite?")) ... will not ask confirmation
when the flag overwrite_files is set.
1.
a) Control variable – variable that control loop
b) Sentinel value – value that end a loop but it is not part of data.
2. a). Selection structure and repetition structure
 Selection structure allows one set of statements to be executed if a condition is true
and another set of actions to be executed if a condition is false.
 Repetition structures, or loops, are used when a program needs to repeatedly process
one or more instructions until some condition is met, at which time the loop ends.

b) Nested if statement and switch statement

. Nested if statements means an if statement inside another if statement


- The switch statement is a multiway branch statemen

c) For statement, while statement, do..while statement


for statement
- instruction or group of instructions to be execute for a specific number of times
depending on the value of a loop counter.
while statement
- instruction or group of instructions to be execute only while a certain condition
continues to be met.
do..while statement
- checks the condition at the end of the loop. The statements inside the loop body will
be executed at least once even if the condition is never true.

d) Counter loop and sentinel loop


- loop program that is controlled by an integer that counts up from a initial value to an upper
limit

int j;

j = 0;
while ( j < 10 )
{
. . .
j++ ;
}
- loop program that use a value to stop the loop
e) The || and the && operator
Evaluates left operand first and if the value is logically false then it avoids evaluating the right
operand.

evaluates left operand first and if the value is logically true then it avoids evaluating the right
operand

4. a) if statement – to execute a block of statements if the condition is true

if (condition)
{
// Executes this block if
// condition is true
}
b) If else statement - execute the if statements if the condition is true but if the condition is
false it will execute the else statements.

if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}

c. Nested structure

- use for if the true condition have more than one.

.if (condition1)

{
// Executes when condition1 is true
if (next condition)
{
// Executes when next condition is true
}
}
Else

//Executes when none of the conditions is true

}
d. Switch statement
- a multiway branch statement to provides an easy way to dispatch execution to different parts
of code based on the value of the expression.
switch (n)
{
case 1: // code to be executed if n = 1;
break;
case 2: // code to be executed if n = 2;
break;
default: // code to be executed if n doesn't match any cases
}

e. break statement
- used to terminate the loop
break;

f. continue statement
it forces to execute the next iteration of the loop
continue;

g. for statement

- to write a loop that is executed a specific number of times

for (initialization expr; test expr; update expr)


{
// body of the loop
// statements we want to execute
}
h. while statement
- used in situations when do not know the exact number of iterations of loop beforehand and
terminate on the basis of test condition.

initialization expression;
while (test_expression)
{
// statements

update_expression;
}

// C++ program to illustrate for loop


#include <iostream>
using namespace std;
  
int main()
{
    // initialization expression
    int i = 1;
  
    // test expression
    while (i < 6)
    {
        cout << "Hello World\n";
  
        // update expression
        i++;
    }
  
    return 0;
}
i. do..while statement

- used in situations when do not know the exact number of iterations of loop
beforehand and terminate on the basis of test condition but the condition is tested at
the end of loop body.
- initialization expression;
- do
- {
- // statements
-
- update_expression;
- } while (test_expression);

- // C++ program to illustrate do-while loop


- #include <iostream>
- using namespace std;
-   
- int main()
- {
-     int i = 2; // Initialization expression
-   
-     do
-     {
-         // loop body
-         cout << "Hello World\n";
-   
-         // update expression
-         i++;
-   
-     }  while (i < 1);   // test expression
-   
-     return 0;
- }
j. nested loop structure

nested loop structure is for to set more than one true condition in loop.
The syntax for a nested for loop statement in C++ is as follows –

for ( init; condition; increment ) {


for ( init; condition; increment ) {
statement(s);
}
statement(s); // you can put more statements.
}

The syntax for a nested while loop statement in C++ is as follows −

while(condition) {
while(condition) {
statement(s);
}
statement(s); // you can put more statements.
}

The syntax for a nested do...while loop statement in C++ is as follows −

do {
statement(s); // you can put more statements.
do {
statement(s);
} while( condition );

} while( condition );

#include<iostream>

using namespace std;

int main()
{
int no1, no2, sum, option;
cin >>option;
switch(option)
{
case 1: cout <<"sum calculation";
cin >> no1>>no2;
sum = no1+no2;
cout<<sum;
break;

case 2: cout <<"Multiply calculation";


cin >> no1>>no2;
sum = no1*no2;
cout<<sum;

break;

default: cout<< "\nInvalid option"<<endl;


}///switch

}while(option != 4);
cout<< "\nEnd of program";
return 0;
}//main()

5) Write the appropriate statement to do the following: a. Input a value and test whether it is
an even or odd value

#include<iostream>
using namespace std;
int main()
{
int number;
cin>>number;
if(number%2 == 0)
{
cout<<"This is even number";
}//if
else
{
if(number%2 == 1)
{
cout<<"This is odd number";
}//if
else
{
cout<<"Error";
}//else
}//else if
return 0;
}//main
b. Check for a leap year
#include <iostream>
using namespace std;

int main()
{
int year;

cout << "Enter a year: ";


cin >> year;

if (year % 4 == 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
cout << year << " is a leap year.";
else
cout << year << " is not a leap year.";
}
else
cout << year << " is a leap year.";
}
else
cout << year << " is not a leap year.";

return 0;
}

c. Input a gender (‘M’ or ‘F’) and display “Male” if the user keyed in ‘M’ or “Female” if the user
keyed in Female
#include <iostream>
using namespace std;

int main()
{
char code;

cout<<"Enter gender code";


cin>>code;
if(code == 'M')
{
cout<<"Male";
}
else
{
if(code =='F')
{
cout<<"Female";
}
else
{
cout<<"error";
}
}
return 0;
}

D) #include <iostream>
using namespace std;
int main()
{
int number;
cout<<"Program to check value"<<endl;
cout<<"Enter your number"<<endl;
cin >>number;
if((number >= 0) && (number <= 100))
{
cout<<""<<number<<"is accepted value";
}
else
{
cout<<""<<number<<"is incorrect value";
}
cout<<"\nEnd of program";
return 0;
}

E) #include <iostream>

using namespace std;

int main()
{
float numberA, numberB, numberC;

cout <<"Program to get the highest numbers"<<endl;


cout << "Enter 3 Number"<<endl;
cin >> numberA >> numberB >> numberC;
if((numberA >= numberB) && (numberA >= numberC))
{
cout << "\nHighest number is = "<< numberA;
}
Else r
{
if((numberB >= numberA) && (numberB >= numberC))
{
cout << "\nHighest number is = "<< numberB;
}
else
{
if((numberC >= numberA) && (numberC >= numberB))
{
cout << "\nHighest number is = "<< numberC;
}
}
}
cout<<"\nEnd of program";
return 0;
}

6) #include <iostream>
using namespace std;
int main()
{
double distance = 0.0;
cout<<"To calculate the value of distant cost"<<endl;
cout<<"Enter your distance = ";
cin>>distance;
if(distance >= 1000)
{
cout<<"The cost for your travel distance is = RM 12.00"<<endl;
}
else
{
if(distance >500)
{
cout<<"The cost for your travel distance is = RM 10.00"<<endl;
}
else
{
if(distance > 100)
{
cout<<"The cost for your travel distance is = RM 8.00"<<endl;
}
else
{
if(distance > 0)
{
cout<<"The cost for your travel distance is = RM
5.00"<<endl;
}
else
{
cout<<"error"; }

}
}
}
cout<<”End of program”;
return value 0;
}

8) #include <iostream>
#include <cmath>

using namespace std;

int main()
{
int cnt = 1;
double num, squareR = 0.0;
cout<<"please enter a number : ";
cin>>num;

while(cnt <= num)


{
squareR = sqrt(num);
cout.precision( 3 );
cout<<cnt<<" "<<squareR<<endl;
cnt++;
}
return 0;
}
9) i) 10 3 5 9 3

ii)6 -20 7 -10 -56

b) Appropriate message to replace “Result 1 ” is “First result = ” and “Result 2 ” replace


with “Second result = “.

cout<<”First result = “<<a<<endl <<”Second result = “<<b<<endl;


10)
#include <iostream>
using namespace std;

int main()
{
char filmCode, hallType;
double totalPrice = 0.0, pPrice = 0.0, gPrice = 0.0, gTotal = 0.0;
int nTickets, ttlTicket;
cout <<"STAR ENTERTAINMENT REPORT"<<endl;
cout <<"________________________________________________________"<<endl;
cout<<"Film Code : ";
cin>>filmCode;
while(filmCode != 'S')
{
if(filmCode == 'T')
{
cout <<"Film Title : Ong Bak III (Thai)"<<endl;
}
else
{
if(filmCode == 'U')
{
cout <<"Film Title : The Twilight Saga: Eclipse (USA)"<<endl;
}
}
cout <<"Type of Hall : ";
cin >>hallType;
if(hallType == 'G')
{
cout <<"Number of tickets : ";
cin >> nTickets;
ttlTicket += nTickets;
totalPrice = 40.00 * nTickets;
gPrice += totalPrice;
}
else
{
if(hallType == 'P')
{
cout <<"Number of tickets : ";
cin >> nTickets;
ttlTicket += nTickets;
totalPrice = 20.00 * nTickets;
pPrice += totalPrice;
}
else
{
cout <<"Error please try again (use G/P)"<<endl<<endl;

}
}
cout<<"Total Price (RM) : "<<totalPrice<<endl;
cout <<"STAR ENTERTAINMENT REPORT"<<endl;
cout
<<"________________________________________________________"<<endl;
cout<<"Film Code : ";
cin>>filmCode;
}
cout<<"STAR ENTERTAINMENT REPORT"<<endl;
cout <<"________________________________________________________"<<endl;
cout<<"Total Ticket Collection : "<<ttlTicket<<endl;
cout <<"Gold Hall (RM) : "<<gPrice<<endl;
cout <<"Premier Hall (RM) : "<<pPrice<<endl;
gTotal = pPrice + gPrice;
cout <<"Grand Total (RM) : "<<gTotal<<endl;
cout<< "\nEnd of program";
return 0;
}//main()

You might also like