Professional Documents
Culture Documents
Repetition Structures, or Loops, Are Used When A Program Needs To Repeatedly Process One or
Repetition Structures, or Loops, Are Used When A Program Needs To Repeatedly Process One or
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.
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
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.
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
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
.if (condition1)
{
// Executes when condition1 is true
if (next condition)
{
// Executes when next condition is true
}
}
Else
}
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
initialization expression;
while (test_expression)
{
// statements
update_expression;
}
- 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);
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 –
while(condition) {
while(condition) {
statement(s);
}
statement(s); // you can put more statements.
}
do {
statement(s); // you can put more statements.
do {
statement(s);
} while( condition );
} while( condition );
#include<iostream>
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;
break;
}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;
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;
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>
int main()
{
float numberA, numberB, numberC;
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>
int main()
{
int cnt = 1;
double num, squareR = 0.0;
cout<<"please enter a number : ";
cin>>num;
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()