c++ statements

33
C++ STATEMENTS  Arvin Anthony S. Araneta 

Upload: arvin-anthony-sabido-araneta

Post on 07-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 1/33

C++ STATEMENTS

 Arvin Anthony S. Araneta 

Page 2: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 2/33

Objectives:

Ou tlines the different statements used

in C++.

Use the different statements in writingprograms.

Prepare correct programs.

Page 3: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 3/33

Guide Questions:

W hat are the different statements inC++?

W hat is the best statement to use in

C++? W hen for loop is used in a program, is there a possibility in rewriting theprogram so that the while loop isused? the if statement is used? the while statement is used? the do whileloop is used? How?

Page 4: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 4/33

It permits us t o m odify the instructi on  of  h ow  the statements in  our pr ograms are 

acc omplished .

Control Flow Statements

Page 5: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 5/33

 T he semicolon is used as a statement

 terminator. A null statement is signified by an isolated semicolon. A compound

statement, or block, is a series of  

statements enclosed by braces, i.e.,

{

statement1;

statement2;}

Page 6: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 6/33

Categories of Control Flow Statements:

� Decision Control Structures. Statements that

allows us to select and execu te specific

blocks of code while skipping other sections.

� Repetition Control Structures. Statements

 that allows us to execu te specific blocks of 

code a number of times.

Page 7: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 7/33

if  if else if else if 

switc

Decision Control Structure

Page 8: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 8/33

if

T he if  keyword i s used to execu te a statement

or block or block only   if   the condi  ti on i s

f  ulfi lled.

Its f  orm i s:

if  (expressi on)

statement/s...

Page 9: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 9/33

if

E xample:

1. if ( a > b)

cou t << "a is greater than b " << endl;

2. if ( a > b)

{ a = a * b;

cou t << "the value of a is " << a << endl;

}

Page 10: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 10/33

Sample Program for if statement:

#include <iostream>

int main(){

char answer;

cou t << \Do you consider your self a hot-tempered individual

(y=Yes / n=No) ?[ ;

cin >> answer;

if (answer == Zy ) // 1st condition

{

cou t << \\n T his job involves a high level of self-control.[;

cou t << n W e will get back to you!\ n;

}

if (answer == Zn) //2nd condition

cou t << \\ nYou are hired!;

return 0;

}

Page 11: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 11/33

if ² else 

T he if-else statement is used when we want to

execu te a certain statement if a condition is

 true, and a different statement if the condition

is false.

Page 12: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 12/33

if ² else 

Its form is:

if (expression)

statement/s...else statement/s...

Page 13: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 13/33

if ² else 

E xample:

1. if (a > b) // if true

cou t << "a is greater than b" << endlelse

cou t << "a is less than or equal to b" <<

endl;

Page 14: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 14/33

if ² else 

E xample:

2. if (x == 100)

cou t << \x is 100" << endlelse

cou t << \x is not 100" << endl;

Page 15: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 15/33

Sample Program for if else statement:

#include <iostream>

int main(){

char answer;

cou t << \Do you consider your self a hot-tempered individual

(y=Yes / n=No) ?[ ;

cin >> answer;

if (answer == Zy ) // 1st condition

{

cou t << \\ n T his job involves a high level of self-control.[;

cou t << \\ n W e will get back to you!\ n;

}

else // Any other answer

cou t << \\ nYou are hired!;

return 0;

}

Page 16: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 16/33

if ² else if 

T he statement in the else-cla use of an if-elseblock can be another if-else structures.

Its form is:

if (expression)

statement/s...

else if 

statement/s...else

statements/s

Page 17: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 17/33

if ² else if 

E xample:

1. if (a == 0) // if true

cou t << "a is exactly equal to 0" << endl;else if (a < 0) // if true

cou t << "a is a negative number " << endl

else

cou t << "a is a positive number " << endl;

Page 18: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 18/33

Sample Program for if else statement:

#include <iostream>

int main(){

char answer;

cou t << \Do you consider your self a hot-tempered individual

(y=Yes / n=No) ?[ ;

cin >> answer;

if (answer == Zy ) // 1st condition

{

cou t << \\ n T his job involves a high level of self-control.[;

cou t << \\ n W e will get back to you!\ n;

}

else if (answer == Zn)

cou t << \\ nYou are hired!;

else

cou t << \\ n T hat;s not a valid answer!\ n[;

return 0;

}

Page 19: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 19/33

switch

Its objective is to check several possible constant

 values for an expresion

Its form is:

switch (expression) {

case constant_expression: statement/s

case constant_expression: statement/s

 /* as many cases as needed... */defa ult: statements

}

Page 20: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 20/33

switch

E xample:1. switch (MyChoice) {

case 'a': {

cou t << " T hecharacter is a. " <<endl;

break; }

case 'X': {

cou t << " T he

character is X. " <<endl;

break; }

case 'j': {

cou t << " T he character is

 j. " << endl;

break; }

defa ult: {

cou t << "Invalidentry !WPress any key W " << endl;

break; }}

Page 21: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 21/33

Sample Program for switch statement:

#include <iostream>

int main(){

int number;

cou t << \ T  ype a number between 1 and 3: \;

cin >> number;

switch (number)

{case 1:

cou t << \You type 1.[;

case 2:

cou t << \You type 2.[;

case 3:

cou t << \You type 3.[;defa ult:

cou t << endln << Number << \ is not of therequested range! \;

}

return 0;

}

Page 22: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 22/33

 while for

do h while

Repetition Control Structure

Page 23: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 23/33

while

 A statement or block of statements that is

repeated as long as some condition is

satisfied.

Its form is:

 while (expression)

statement/s

Page 24: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 24/33

while

E xample

1. a = 1;

 while ( a <= 10)

{

cou t << a << endl; a ++;

}

Page 25: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 25/33

Sample Program for while statement:

#include <iostream>int main()

{

int number; // = 0

 while (number < = 12)

{

cou t << \Number \ << Number << endln;

number ++;

}

return 0;

}

Page 26: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 26/33

for

T he for statement is typically used to count a 

number of items. At its regular structure, it is

divided into three parts:

� T he first section specifies the starting point for

 the count.

� T he second section sets the counting limit.

� T he last section determines the counting frequency.

Page 27: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 27/33

for

Its form is:

 for (InitializationE xpression;

LoopCondition;StepE xpression)

{

statement1;

statement2;

. . .}

Page 28: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 28/33

Page 29: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 29/33

Sample Program for for statement:

#include <iostream>int main()

{

 for (int count =0; count <= 12; count++)

cou t << \Number \ << count << endln;

return 0;

}

Page 30: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 30/33

do ² while

� Statements inside a do-while loop are

execu ted several times as long as the

condition is satisfied.

� Its form is:

do {

statement/s...

} while (expression);

Page 31: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 31/33

do ² while 

E xample

1. do

cou t << Number \ << Number ++

<< endln;

 while (number <= 12)

Page 32: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 32/33

Sample Program for for statement:

#include <iostream>

int main()

{

unsigned long n;

do{

cou t << \Enter number (0 to end) \;

cin >> n;

cou t << \you entered: \; << n << endln;

} while (n != 0);

return 0;

}

Page 33: C++ Statements

8/6/2019 C++ Statements

http://slidepdf.com/reader/full/c-statements 33/33

Drills

Construct a program that will display the

multiplication table of 10.