Professional Documents
Culture Documents
ENG 214
O
C
CLASS NOTES
X.
LA
ATIMATI EHINOMEN (ENGR. MRS.)
C
N
AR
LE
outline
• Control of codes:
• Decision loops (not in this class)
M
• Iterative loops
O
• Iterative loops/statements
C
X.
• While statement
• Do… while statement
LA
• For statement
C
• Break statement
N
• Continue statement AR
• Real number types
LE
M
• C++ has three (3) iteration statement:
O
• While statement
C
• Do… while statement
X.
• For statement
LA
• Iterative statements are called loops because of their cyclic nature.
C
• A loop is controlled by three necessary parts:
N
• initialization of a counter/control variable
AR
• Condition for loop to be maintained.
• An update (increment/decrement) of the control variable
LE
While Statement
• Syntax:
• While (condition) {(statement to be iterated)}
M
• How it works:
O
• The condition in bracket is evaluated
C
• The statement in curly braises is executed if the condition if true
X.
• The condition is re-evaluated
LA
• The statement is executed if the condition is true
C
• This repeats itself in a cyclic loop until the condition is false.
N
• For the while loop example 1 below:
AR
• the condition is checked i<n, if true, statement in curly braises are executed
LE
M
• using namespace std;
O
• main()
C
• {
X.
• int n, i=1; //n is maximum number and i is initialized as a counter
LA
• cout<<"Enter positive intergers. Teminate with 0.\n\t:"; // prompts an input during
runtime
C
• cin>>n; // assigns the keyed number at run time to a variable ‘n’
N
• while (i<=n) // condition to be met for loop to be executed
• {
AR
• cout<<i << "\n"; // loop statement to be implement
LE
M
• using namespace std;
O
• main()
C
• {
X.
• int n;
LA
• cout<<"Enter positive intergers. Teminate with 0.\n\t:";
• cin>>n;
C
• while (n>0)
N
• { AR
• cout<< n << " cubed is " << n*n*n << "\n\t";
• cin>>n;
LE
• }}
While loop example 3
M
O
C
X.
LA
C
N
AR
LE
Do… While Statement
• Similar to While statement
• Syntax:
M
• Do {statement} while (condition);
O
• Difference from While statement
C
• It executes the statement first and
X.
• then evaluates the condition.
• It must iterate at least once before breaking the loop
LA
• The cycle continues until the condition is false.
C
• Note: the update must be within the loop
N
• In the do while loop example 1,
AR
• Difference between while and do while
• Key in a value that is less than the counter i, say 0 during run time
LE
• For the while program, nothing is displayed, the do while program will
display ‘0’ before stopping.
Do while example 1
• Simple program that counts numbers. Counting numbers to a specified maximum
• #include<iostream>
M
• using namespace std;
O
• main()
• {
C
• int n, i=0;//initialization of counter i
X.
• cout<<"Enter positive intergers. \n\t:";
LA
• cin>>n;
C
• do
N
• {
• cout<<i << "\n"; AR
• i++; //update (increment)
LE
• }
• while (i<n); //condition
• }
Do while example 2
• Computes n factorial function
• n! = n(n-1)(n-2)…(3)(2)(1).
M
• #include<iostream>
• using namespace std;
O
• main()
C
• {
X.
• int n,f=1;//initialize n
LA
• cout<< "Enter a positive integer ";cin>>n;
• cout << n << " Factorial is : ";
C
• do{
N
• f*=n; //loop statement (multiplies from the largest value to smallest)
AR
• n--;} //update (decrement)
• while (n>1); //control condition
LE
M
O
• Note initialization, condition or the update may be empty
C
• Take care in declaring conditional expressions
X.
• As there has to be a time when the expression is false
LA
• That is a condition for the loop to stop iterating
• Simple tryouts
C
N
• Use any of the loop statement in
AR
• Displaying even numbers
• Displaying odd numbers
LE
M
• #include<iostream>
O
• using namespace std;
C
X.
• main()
LA
•{
• int n;
C
• cout<<"Enter positive intergers. \n\t:";
N
• cin>>n; AR
• for (int i=1;i<=n;i++)
LE
M
O
C
X.
LA
C
N
AR
LE
For example 3
• Computes n factorial function
• n! = (2)(3).....(n-2)(n-1)n
M
• #include<iostream>
O
• using namespace std;
C
• main()
X.
• {
LA
• int n, f=1;
C
• cout<<" Enter positive integer. ";cin>>n;
N
• for (int i=2; i<= n; i++)
AR
• f*=i;
• cout <<n<<" factorial is " << f << endl;
LE
• }
• 2*3*4*…*n
For loop example 4
• More than one control variable
• #include<iostream>
M
O
• using namespace std;
C
• main()
X.
•{
LA
• for (int m=1, n=10;m<n; m++,n--)
C
•{
N
• cout << " m = " <<m<< " and n = " <<n<< "\n";
AR
•}
LE
•}
Break statement (Jump statement)
• The break statement jumps over all the rest of the statement in the
loop block
M
• Goes to the next statement after the loop
O
C
Continue statement
X.
LA
• Similar to break statement
• Does not terminate loop but goes to the beginning of the loop
C
N
• Continue statement example 1 (slide 19):
AR
• When both if conditions fail, it displays a statement
• When keyed number is divisible by 2, it stays in the loop
LE
M
• main()
• {
O
• int i=1, sum =0, n;
C
• cout<< "input a positive integer ";
X.
• cin>>n;
LA
• while (1)
• {
C
N
• if(i>n) break;
• sum +=i*i; AR
• i++;
LE
• }
• cout<< sum ;
• }
Break example 2
• #include<iostream>
• using namespace std;
• main()
M
• {
O
• int n, count =0, sum = 0;
• cout<<" Enter positive integer. Terminate input with 0 :\n ";
C
• for ( ; ; )
X.
• {
LA
• cout<<"\t" << count +1<<": ";//displays serial numbers
• cin>>n;
C
• if (n==0) break;// terminates when 0 is keyed in at run time
N
• ++count; // count, records the number of values keyed in
•
AR
sum+=n; // add all the number keyed in
• }
LE
• cout<<"The average of the " <<count<< " numbers is " << float (sum)/count ; //data type sum is changed from
int to float
• }
• Prompts you to input integers during run time
Continue statement example 1
• #include<iostream>
• using namespace std;
M
• main()
• {
O
• int n;
C
• for ( ; ; )
X.
• {
LA
• cout<<" Enter positive integer. ";
C
• cin>>n;
N
• if (n%2==0) continue;
• AR
else if (n%3==0)break;
• cout<<"\t Bottom of loop. \n";
LE
• }
• cout<<"\tOutside of loop.\n";
• }
Real Number Types
• Real numbers are supported with the following data types in C++:
• Float (eg. 0.1)
M
• Double (eg. 2.33)
O
• Long double (e.g. 34.555555555555555555555)
C
• Double uses twice as much byte (data space) as float
X.
• Long double uses more data space.
LA
• Using the sizeof operator
C
• The example below displays all the memory size for different data
N
types AR
LE
Sizeof operator example
• This program displays how much computer memory space 12 fundamental types occupy
• #include<iostream>
• using namespace std;
M
• main()
O
• {cout<< "Number of BYTES used : \n";
• cout<<"\t char:" << sizeof (char)<< endl;
C
• cout<<"\t short:" << sizeof (short)<< endl;
X.
• cout<<"\t int:" << sizeof (int)<< endl;
LA
• cout<<"\t long" << sizeof (long)<< endl;
• cout<<"\t unsigned char:" << sizeof (unsigned char)<< endl;
• cout<<"\t unsigned short:" << sizeof (unsigned short)<< endl;
C
• cout<<"\t unsigned int:" << sizeof (unsigned int)<< endl;
N
• cout<<"\t unsigned long:" << sizeof (unsigned long)<< endl;
AR
• cout<<"\t signed char:" << sizeof (signed char)<< endl;
• cout<<"\t signed short:" << sizeof (signed short)<< endl;
• cout<<"\t signed int:" << sizeof (signed int)<< endl;
LE
M
• Data type conversion or CASTING
O
C
• Syntax for casting
X.
• If T is one type and v is a value in another type
LA
• T(v) or (T)v
C
• Coverts v to type T.
N
• Example: if n is a double of 3.4446
AR
• to display only the whole number
LE
• Int (n)
Casting example 1
• The program ignores the decimal numbers by casting.
• #include<iostream>
M
• using namespace std;
O
C
• main()
X.
•{
LA
• double v= 1234.56789;
C
• int n= (int)v;
N
• cout<<"v = " <<v<< " \n \n n = "<<n << endl;
AR
•}
LE
Constants, Variables and Objects
• An object is a contiguous region of memory that has an
• Address
M
• Size
O
• Type and
C
• Value
X.
• Address of an object is the memory address of its first byte
LA
• Determined by the computer’s operating system
• The size of an object is simply the number of bytes that it occupies in
C
memory.
N
• Determined by the compiler
AR
• The value is the constant determined by actual bits stored in its memory
• Can be determined by programmer or during runtime
LE
M
O
• Constants are declared with this syntax
C
• const int n = 3.14;
X.
• Constants must be initialized (declared and assigned)
LA
C
N
AR
LE