Вы находитесь на странице: 1из 51

Chapter 2

Decision Making

%ecision :a"ing

*n programming, man times we want a set o!


instructions to 'e execute& * one situation an& an
entirel &i!!erent set to 'e execute& in another
situation.

This tren& o! situation is &ealt in ?C? programs using a


&ecision 2 selection control instructions an& the are,
". The i! statement
). The i!Jelse statement
3. The i!Jelse i! statements
C. The con&itional ternar operator

!he if statement

The i! (ewor& is use& to test a con&ition.

Qeneral .ntax:
if , condition
N
statements
O

*! the con&ition is true then statements written insi&e


i! 'loc( are execute&.

*! the con&ition is !alse then control goes out o! the i!


'loc(.







!he ifGelse statement

.imple i! statement can 'e use& to chec( whether con&ition


is true or not.

*n simple i! statements onl true part is inclu&e& an& it &oes


nothing when the con&ition is !alse.

.o with the help o! i! J else statement, i! the con&ition is true


we can execute one group o! statements an& i! the con&ition
is !alse we can execute another group o! statements .

Qeneral .ntax: if , condition


N statement1R
O
else
N statement#R
O







!he ifGelse if statement
The i! R else i! statement is use& when we want to
chec( multiple con&itions

Qeneral .ntax: if , condition1


N statement1R O
else if , condition#
N statement#R O
else if , condition$
N statement$R O
else
N statementZR O













!he \ested ifGelse statements
Yhen there is a con&ition insi&e another con&ition, the
?5este& i! ? statements are use&

Qeneral .ntax: if , condition1


N
if , condition#
N statement#R O
else
N statement$R O
O
else
N statementZR O











!he switchGcase statement
The switch case statement allows us to ma(e
a &ecision !rom num'er o! choices

Qeneral .ntax: switch , choice 1ariable


N
case constant 1:
statements
brea"R
case constant #:
statements
brea"R
case constant n:
statements
brea"R
default:
statements
O

!he brea" statement
The 'rea( statement is use& to 'rea( the control in
the loops or to 'rea( the se@uence
*n switch case statement, 'rea( is use& to 'rea( the
control o! switch itsel! when the selecte& case is
execute&.
















!he while, &oop
The while loop is use& !or the purpose o! repetition

A con&ition is chec(e& !irst,then the while loop


repeats itsel! till the con&ition is true.

*! the con&ition is !alse, then loop gets 'rea( i.e.


control goes out o! while loop.

Qeneral .ntax: initialiWe counter 1ariable


while , condition
N
statements
increment counter 1ariable
O







!he doGwhile, &oop

The while loop !irst chec(s the con&ition an& then


statements within while loop are execute&.

But in &o while loop, !irst statements are execute&


an& then the con&ition is chec(e&.

*! the con&ition in while loop is not satis!ie& ' an


value o! counter varia'le, then statements will never
'e execute&.

But in &o while, even i! the con&ition is not satis!ie&


' an value o! counter varia'le, still the statements
will get execute& at least once.

Qeneral .ntax:
initialiWe counter 1ariable
do
N
statements
increment counter 1ariable
O while , condition R

4emem'er &o while loop en&s with a semicolon.









%ifference between while, and
doGwhile, &oops

while loop &o while loop
.ntax: initiali:e counter varia'le
Yhile G con&ition #
\
.tatements
increment counter varia'le
]
.ntax: initiali:e counter varia'le
&o
\
.tatements
increment counter varia'le
] Yhile G con&ition # S
*t !irst chec(s con&ition an& i! con&ition is
true then onl statements are execute&
*t !irst execute statements an& then chec(s
the con&ition
*! the con&ition in while loop is not
satis!ie& ' an value o! counter varia'le,
then statements will never 'e execute&.
*n &o while loop even i! the con&ition is not
satis!ie& ' an value o! counter varia'le,
still the statements will get execute& at
least once.
while loop is more accurate &o while is not as accurate as while loop
Example: voi& mainG#
\
int i B CS
while G i O = #
\
print!G$elloA#S
i++S
]
]
Example: voi& mainG#
\
*nt i B CS
&o
\
print!G$elloA#S
i++S
] while G i O = #S
]

!he for &oop
The !or loop is use& to repeat an action within the limit A

Qeneral .ntax:
for , initialiWe counterR test conditionR
incrementKdecrement counter
N
statementsR
O







!he continue statement
The continue statement is use& to trans!er the control
to the 'eginning o! loop A

Yherever in program, the continue statement appears


the control imme&iatel get trans!erre& to the
'eginning o! loop.

The continue statement is generall use& with the i!


statement


































7ral ]uestions

Yhat are &i!!erent control statements.

Explain use o! i! else statement.

Yrite sntax o! i! , i! R else an& i! R else i! statements.

Explain use o! while an& !or loop.

Yrite sntax o! while, &oJwhile an& !or loops.

Explain the &i!!erences 'etween while an& &o while


loops.

Explain &i!!erences 'etween !or an& while loop.

Explain continue statement.




Chapter 3
Arrays and Strings

Array
Array is bounded collection of elements having same
data type

To store a single element, a single variable is used,


but to store collection of elements, array is used.

Eample, integer array contains integer elements.

!loat array contains collection of floating point values.

Character array contains collection of characters.

Вам также может понравиться