Output Question
Transcript of Output Question
-
8/9/2019 Output Question
1/51
-
8/9/2019 Output Question
2/51
Pre & Post increment
increment & Decrement Operators
!""
Post#increment $ add % to the value'he value is returned beforethe increment is made( eg
! ) %* + ) !""*'hen + will hold % and ! will hold ,
!##
Post#decrement $ subtract % -rom the value
'he value is returned beforethe decrement is made( eg $ ! ) %* + ) !##*'hen + will hold % and ! will hold .
""!
Pre#increment $ add % to the value'he value is returned afterthe increment is made( eg
! ) %*
+ ) ""!*'hen + will hold , and ! will hold ,
##!
Pre#decrement $ subtract % -rom the value'he value is returned afterthe decrement is made( eg
! ) %* + ) ##!*
'hen + will hold . and ! will hold . 2
-
8/9/2019 Output Question
3/51
***Pre & Post incrementexample
Ques 1: without using variables to holdincremented or decremented value
int sum% ) /*sum%""*
j'e!t0ield%set'e!t122"sum%3*
""sum%*j'e!t0ield,set'e!t122"1""sum%33*
4ns%56
int sum% ) /*int n%)sum%""*
j'e!t0ield%set'e!t122"n%3*int n,)""sum%*
j'e!t0ield,set'e!t122"1""n,33*
Ques 2: Using variables to holdincremented or decremented value
4ns,/6
3
-
8/9/2019 Output Question
4/51
Pre incrementOutput 7uestion /88 value o- -or loop will be incrementedor decremented once loop getscompletedint x!"#$
%orx"1$x'"($))x +
,!stem-out-println. x is a%ter loop.). .)x$ !"))x$
,!stem-out-println. !"))x x is .). .)x$ ,!stem-out-println. !"))x . ).
.)!$//!$
,!stem-out-println. / /! . ). .)!$ 0 ,!stem-out-println.x is.)x$
,!stem-out-println!$
-
8/9/2019 Output Question
5/51
ns 3 :
x is a%ter loop 1!"))x x is 2
!"))x 2/ /! 1x is a%ter loop 3!"))x x is !"))x / /! 3
x is a%ter loop (!"))x x is !"))x / /! (x is4(
(
-
8/9/2019 Output Question
6/51
Post increment
Ques int x!"#$
%orx"1$x'"($x)) +
,!stem-out-println. x is a%terloop. ). .)x$
!"x))$,!stem-out-println. !"x)) .
). .)!$// !$
,!stem-out-println. / /! . )..)!$
0 ,!stem-out-println.x is.)x$ ,!stem-out-println!$
-
8/9/2019 Output Question
7/51
ns :
x is a%ter loop 1!"x)) 1/ /! #
x is a%ter loop 3!"x)) 3/ /! 2x is a%ter loop (!"x)) (/ /!
x is4
4
-
8/9/2019 Output Question
8/51
9:
int sum% ) /*
int sum,)sum%"" ",*j'e!t0ield%set'e!t122"sum,3*""sum%*
j'e!t0ield,set'e!t122"1""sum%33*
5
-
8/9/2019 Output Question
9/51
4ns::6
6
-
8/9/2019 Output Question
10/51
96
int val%):(val,)%.*-or1int i)%*i;)/*i""3(> "##val,3*s+stemoutprintln1= ="val,## ">(> " "
"val%3*
1#
-
8/9/2019 Output Question
11/51
4ns:: (??(@
@(@@(??(::(%%
11
-
8/9/2019 Output Question
12/51
substring in 7avaOutput 7uestion,tring str " .li%e is beauti%ul8$,tring val1 " str-substring#1#$
,tring val2 " str-substring#$,tring val3 " str-substring5$,tring v3"str-substring#$,tring v"str-substring1$ ,!stem-out-println.substr at #1# position is. )..)val1$ ,!stem-out-println.substr at o is. ). .)val2$ ,!stem-out-println.substr at 5 is. ). .)val3$ ,!stem-out-println.substr at o is. ). .)v3$ ,!stem-out-println.substr at 1 is. ). .)v$
. % , / 5 : 6 @ A ? %.
%%
%,
%/
%5
%:
%6
%@
l i % e i s b e a u t i % u l
12
-
8/9/2019 Output Question
13/51
substring in 7ava
nssubstr at #1# position is li%e is besubstr at o is li%e isubstr at 5 is beauti%ulsubstr at o is li%e is beauti%ulsubstr at 1 is i%e is beauti%ul
13
-
8/9/2019 Output Question
14/51
9ested loop
;he placing o% one loop inside the bod! o% another loop iscalled nesting-
-
8/9/2019 Output Question
15/51
-or1num, ) .* num, ;) /*num,""3< -or1num% ) .* num% ;) ,*num%""3 < S+stemoutprintln1num,
" 2 2 " num%3*
Cemor+ Screen
int num, int num%
. .
%
,
/ end loop
% .
%
,
/ end loop
, .
%
,
/ end loop
/ .
%
Working o- nested loop
1(
-
8/9/2019 Output Question
16/51
9ested loop
Ques
%orint i"1$i'"$i)) + %orint 7"1$7'"i$7)) + ,!stem-out-print. .)i$
0 ,!stem-out-println$ 0
ns :
12 23 3 3
1
-
8/9/2019 Output Question
17/51
9ested loop
Ques
%orint i"1$i'"$i)) + %orint 7"1$7'"$7)) + ,!stem-out-print. .)i$
0 ,!stem-out-println$ 0
ns :
1 1 1 12 2 2 23 3 3 3
14
-
8/9/2019 Output Question
18/51
-
8/9/2019 Output Question
19/51
9ested loop
Ques
%orint i"1$i'"$i)) + %orint 7"$7?"i$7// + ,!stem-out-print. .)7$
0 ,!stem-out-println$ 0
ns :
321323
16
-
8/9/2019 Output Question
20/51
9ested loop
Ques ;@A ABU@,DE%orint i"2$i'"($i))+
,!stem-out-printi) ."?.$%orint 7"1$7'"3$7))
+
,!stem-out-printi*7 )..$0
,!stem-out-println$
0
2#
-
8/9/2019 Output Question
21/51
9ested loop
ns :
2"?2
3"?36"?512("?(1#1(
21
-
8/9/2019 Output Question
22/51
Butput using while loop
Ques :
int 7"1s"#$ while7'( + ,!stem-out-print7) .).$
s"s)7$ 7"7)7F3$0 ,!stem-out-println.". )s$
22
-
8/9/2019 Output Question
23/51
23
4ns 6$
%","5")@
-
8/9/2019 Output Question
24/51
-
8/9/2019 Output Question
25/51
ns-
11
(
11
2(
-
8/9/2019 Output Question
26/51
Butput using do while loopQues :
int i7n$ n"#$i"1$ do+ n))$ i))$
0whilei'"($ ,!stem-out-printlnn$ ,!stem-out-printlni$
ns
(
2
-
8/9/2019 Output Question
27/51
EFCP Statement
24
1i3 break $ 'he break is used to break -rom an enclosingdo(while-or(or switch statement
S+nta!$break*
()4 break statement skips the rest o- the loop and jumpsover to the statement -ollowing the loop
()G- a break statement appers in a nested loop ( then itcauses an e!it -rom onl+ the ver+ loop it appears in
-
8/9/2019 Output Question
28/51
brea= statement
int i c$
int a"Gnteger-parseGnt7;extEield1-get;ext$
int b"Gnteger-parseGnt7;extEield2-get;ext$
%ori"#$i'2#$i))
+i%b""#
brea=$
else
c"aHb$
,!stem-out-println.Quotient ".)c$
0
. .25
-
8/9/2019 Output Question
29/51
Jump ,tatement
ii continue: ;he continue statement stops the execution o% thecurrent iteration and causes control to begin with next iteration-
,!ntax:
continue$
Eor the %or loop continue causes the nex iteration b! updating thevariable and then causing the test expressionKs evaluation -
Eor the while and do while loops the program control passes to theconditional tests-
26
-
8/9/2019 Output Question
30/51
int i (c*
int a)GntegerparseGnt1j'e!t0ield%get'e!t133*
int b)GntegerparseGnt1j'e!t0ield,get'e!t133*
-or1i).*i;,.*i""3