Você está na página 1de 8

SJES 2271/ SJEM 2231

3/8/2013

What are C++ constructs?


 The control and looping commands for large
data repetitive processing capabilities.

SJES 2271 : Scientific Computing 1


SJEM 2231 : Structured Programming
LECTURE NOTES (WEEK 4)
BY
DR NOOR FADIYA MOHD NOOR

What are decision


statements?
 Expressions that test a relationship using
relational operators hence decide which
statement to be executed next.
 To make on time decisions.
 Affect value of expression on certain part of
the program.

DR NOOR FADIYA MOHD NOOR, UM

 They include:

a) decision statements
b) iteration statements
c) jump statements
d) exit function

Decision Statements
 The important decision statements in C++ are:

a) if statement
b) if-else statement

c) switch statement

if (a=2)
{a++;}
if (a=2)
{a++;}
else
{a*=3;}

SJES 2271/ SJEM 2231

3/8/2013

Example (if)

Example (if-else)
#include <iostream>

#include <iostream>

#include <cstdlib>

#include <cstdlib>

using namespace std;

using namespace std;

int main()

int main()

int magic, guess;

int magic, guess;

magic = rand();

magic = rand();

cout << "Enter your guess: ";

cout << "Enter your guess: ";

cin >> guess;

cin >> guess;

if (guess == magic)

if (guess == magic)

cout << "** Right **";

cout << "** Right **";

else

return 0;

cout << "...Sorry, you're wrong.";

return 0;
}

Example (nested if-else)

Example (nested if-else)

#include <iostream>

else

#include <cstdlib>

cout << "...Sorry, you're wrong.";

using namespace std;

if (guess > magic)

int main()

int magic, guess;

magic = rand();

else

cout << "Enter your guess: ";

cin >> guess;

if (guess == magic)
{

cout << " Your guess is too low.\n";

cout << "** Right **\n";


cout << magic << " is the magic number.\n";

cout <<" Your guess is too high.\n";

return 0;
}

DR NOOR FADIYA MOHD NOOR, UM

SJES 2271/ SJEM 2231

3/8/2013

What is the switch


statement?
 A statement that allocates separate code for
different test statements under case labels.
 Requires break statement to jump to the next
switch structure.
 If the expression value in switch(expression)
did not match any case label, the statements
under default label will be executed
optionally.

switch EQUIVALENT if-else


switch statement
for (int k=0; k<=6; k++)
{ switch (k)
{ case 1:
cout<< k is 1\n;break;
case 3:
cout<<k is 3\n; break;
default:
cout<<k is random\n;}
}

if-else statement
for (int k=0; k<=6; k++)
{ if (k==1)
{ cout<< k is 1\n;}
else if (k==3)
{cout<<k is 3\n;}
else
{cout<<k is random\n;}
}

Example
for (int k=0; k<=6; k++)
{ switch (k)
{ case 1:
case 3:
case 5:
cout<< k is odd\n;break;
default:
cout<<k is even\n;}
}

switch VERSUS if-else


switch statement

if-else statement

 Can test only for equality

 Can test any type of

operator
 Can evaluate character or
constant integer only
 Requires break statements
to jump to next structure
 A switch statement is more
efficient than nested if-else
statement

relational operators
 Can evaluate data of any
type
 Does not require jump
statement

# C++ allowed >=256 levels of nesting in if-else and switch statements

DR NOOR FADIYA MOHD NOOR, UM

SJES 2271/ SJEM 2231

3/8/2013

What are iterative


statements?

Iterative Statements
 The important iterative statements in C++ are:

 Program control expressions for repeating a


single statement or a block of statements.

a) while loop

while (test statement)


{block of statements;}

 Require a loop control variable.

b) do-while loop

do

 Execute as long as the conditional expression


tests true.

c) for loop

What is the while loop?

{block of statements;}
while (test statement) ;
for (initialization;condition;increase)
{block of statements;}

Example

 A looping statement that controls the


execution of a series of other statements.
 Causes parts of the program to be executed
repeatedly as long as a certain condition is
met.
 Example:

while (n>0)
{cout << x=10;
cout<<y=2; }

DR NOOR FADIYA MOHD NOOR, UM

SJES 2271/ SJEM 2231

3/8/2013

Example

while loop VS if loop


 while and do-while loops execute a section of
code multiple times depending on test
condition.
 if statement executes the section once.

What is the do-while loop?

Example

 Similar to the while loop except the


conditional test occurs at the end of the loop.
 Ensures the body of the loop to execute at
least once.
 Example:

do
{cout<<x=10\n<<y=2;}
while (k==0) ;

DR NOOR FADIYA MOHD NOOR, UM

SJES 2271/ SJEM 2231

3/8/2013

What is the for loop?


 Similar to the while/do-while loops except it
can
provide
initialization/increment
statements.
 Designed to perform repetitive processing
with a counter which is initialized and
increased/ decreased in each iteration.
 Example:

for (int k=0;k<=10;k++)


{int sum=2*k-1; cout<<sum;}

What are jump statements?

Example
#include <iostream>
#include <cmath>
using namespace std;
main ()
{ int k, N, Y;
cin>>N;
for (k=0; k<=N; k++)
{ Y=k;
if (k>0)
Y=4*k+pow(k,2);
}
return (0);
}

Jump Statements
 The important jump statements in C++ are:

 Statements that bypass the loops normal


control.
 To skip any code between itself and the
conditional expression that controls the loop.
 May affect the loops iteration on certain part
of the program.

DR NOOR FADIYA MOHD NOOR, UM

a) continue statement
b) goto statement
c) break statement

SJES 2271/ SJEM 2231

3/8/2013

What is the continue


statement?
 A jump statement to skip the current loop
iteration and perform the start of the next
iteration.
 Typically placed in a loop after an if
statement.
 The format is:

continue;

What is the goto statement?


 An absolute jump statement from one point
to another point in the program.
 Typically placed between an if statement and
a valid identified destination (ending with :).
 The format is: _______ goto _______;
 Example:
InputData:
int k; cin>>k;
if (k!=4) goto InputData;
cout<<k<<endl;

DR NOOR FADIYA MOHD NOOR, UM

Example
#include <iostream>
using namespace std;

#include <iostream>
using namespace std;

int main ()
{ double x, y, Add;
for (int k=0; k<=10; k++)
{ if (k==3) continue;
cin>>x>>y;
Add=x+y;
cout<<k<<"\t"<<Add<<endl;
}
return (0);
}

int main()
{ int x;
for(x=0; x<=100; x++)
{ if(x%2) continue;
cout << x << ' ';
}
return 0;
}

Example
#include <iostream>
using namespace std;

#include <iostream>
using namespace std;

int main ()
{ int x = 1;
loop1:
x++; cout<<x<<' ';
if(x < 100) goto loop1;
system("pause");
return (0);
}

int main ()
{ InputData:
int k; cin>>k;
if (k!=4) goto InputData;
cout<<k<<endl;
return (0);
}

SJES 2271/ SJEM 2231

break

3/8/2013

statement

VS exit

break statement
 Usually appears in the

body of while/do-while/for
loops, if and switch
statement.
 Used to terminate current
loop early.
 Format: break;

Example

DR NOOR FADIYA MOHD NOOR, UM

function

Example

exit function
 Usually appears in the






body of if and switch


statement.
Used to exit the entire
program early.
Requires a header file
stdlib.h
Format: exit (status);
# status=optional integer
variable/literal

Example

Você também pode gostar