Docsity

C++ Looping Constructs and Predefined Functions - Prof. L. G. Volkert, Study notes of Computer Science

Toggle sidebar
bg1
Looping
constructs review
What are named cons tants? Why are they needed?
What is a block? Wha t is special about declaring a varia ble inside
a block?
What is the difference between while and do-wh ile
what does for do? W hy is it needed? what is init-s tatement,
expression, post -statement in for?
how is break used in side a looping construct
What is continue, how is it used
Predefined functions
Predefined functions
C++ comes with librar ies of code that can be reused in your
programs. The code c omes in the form of pre-defin ed functions
function name - identifier distinguishing the functio n from others;
example square root function: sqrt
argument - the value function starts out with; function m ay have
more than one argum ent; an argument can be a consta nt,
variable or expression
returned value - value computed by the function
the_root = sqrt( 9.0);
function call (function invocation) - expression consistin g of
function name followe d by arguments in parentheses
function accepts para meters of certain type and returns value of
certain type; sqr t accepts and r eturns double
to use a function need to specify include directive:
#include <cm ath>
Type changing functions
Is there a problem wit h this code?
int a=9, b=2 ;
double c=a/b;
C++ provides function s for explicit conversions between types:
function double con verts to type double:
int a=9, b=2;
double c=double(a)/b;
explicit type conversio n is called type casting
Warning: wrong appli cation of casting:
int a=9, b=2;
double c=double(a/b);

Partial preview of the text

Download C++ Looping Constructs and Predefined Functions - Prof. L. G. Volkert and more Study notes Computer Science in PDF only on Docsity!

Looping

constructs review

 What are named constants? Why are they needed?  What is a block? What is special about declaring a variable inside a block?  What is the difference between while and do-while  what does for do? Why is it needed? what is init-statement , expression , post-statement in for?  how is break used inside a looping construct  What is continue, how is it used

Predefined functions

Predefined functions

 C++ comes with libraries of code that can be reused in your programs. The code comes in the form of pre-defined functionsfunction name - identifier distinguishing the function from others; example square root function: sqrt  argument - the value function starts out with; function may have more than one argument; an argument can be a constant, variable or expression  returned value - value computed by the function the_root = sqrt(9.0);  function call ( function invocation ) - expression consisting of function name followed by arguments in parentheses  function accepts parameters of certain type and returns value of certain type; sqrt accepts and returns double  to use a function need to specify include directive: #include

Type changing functions

 Is there a problem with this code? int a=9, b=2; double c=a/b;  C++ provides functions for explicit conversions between types:  function double converts to type double: int a=9, b=2; double c=double(a)/b;  explicit type conversion is called type casting  Warning: wrong application of casting: int a=9, b=2; double c=double(a/b);