Jump to content
Sign in to follow this  
masham

Tutorial:looping

Recommended Posts

nih tutorial nyer yg 1st

TUTORIAL: LOOPING

1. What are the outputs for the following code segments?

If the code is incorrect, how would you correct it?

Given: int a=1; int b=10;

a) while (a<5);

{

printf("%d",a);

a++;

}

cool.gif while (a<5)

{

printf("Hello world");

printf("The value of a is %d", a);

}

c) do printf("Hello world");

a++;

while (a<5);

d) while (a<5)

{ printf("Hello world");

a++;

while (b>6)

{

printf("welcome to TAR \n");

b--;

}

}

ni kawan saya mintak tlg..hehehh...boleh ke nak mintak kawan2 putera tlg jawab..sebab sy dah lama tggalkan subject C++ nih..dulu ada belajar..skg dah x igt daa..kawan2 semua..tlg bagi jawapan utk tutorial ni beserta huraian sekali..kalau kawan2 sudi jawab..nak post tutorial lagi kat sini.. biggrin.gif ok..tima kasih

Share this post


Link to post
Share on other sites

Salam Awang Selamat,

Kalu betullah awak seorang kawan yang baik sangat nak tolong kawan siapkan assignment(yang asas sangat tu),

apa kata minta tolong dia cuba untuk uji lari (test run) guna compiler dulu..

Bukan apa, bimbang nanti dia dapat markah tinggi(biasanya org macam tu dpt markah tinggi ke?), tapi tak tahu apa..

:ph34r:

Share this post


Link to post
Share on other sites

a) while (a<5);

{

printf("%d",a);

a++;

}

jawapan: 1234

cool.gif while (a<5)

{

printf("Hello world");

printf("The value of a is %d", a);

}

jawapan:Hello worldThevalue of a is 1

c) do printf("Hello world");

a++;

while (a<5);

jawapan:Hello worldHello worldHello worldHello world

d) while (a<5)

{ printf("Hello world");

a++;

while (b>6)

{

printf("welcome to TAR \n");

b--;

}

}

jawapan:Hello worldwelcome to TAR

Hello worldwelcome to TAR

Hello worldwelcome to TAR

Hello worldwelcome to TAR

macam ni la kot jawpan dia..aku main agak je..xcompile pun....

Share this post


Link to post
Share on other sites

a-

while (a<5); <-- nothing going on sebab nih

{

printf("%d",a);

a++;

}

b-

infinite loop

c-

syntax error

d-

Hello worldwelcome to TAR

welcome to TAR

welcome to TAR

welcome to TAR

Hello worldHello worldHello world

Edited by MatchMaker

Share this post


Link to post
Share on other sites

alahai.. kalau ini C... kalau C plas plas kepala dia laen sket... gune iostream... he he he... well.. blaja laa... ini asas segala asas ni untuk programming... kalau soalan tu rumit baru la kite same2 selesaikan...

Share this post


Link to post
Share on other sites

Apa pulak beza a++ ngan ++a @ a-- ngan --a? :wacko:

:lol: Cam ni....

a++ - Kandungan a akan di increment selepas expresi

Contoh...

int a = 5;

y = a++; // Kat sini y = 5 sebab a belum di incremen semasa assign ke y

y = a; // Kat sini a = 6

++a - Kandungan a di incremen sebelum expresi

Contoh...

int a = 5;

y = ++a; // Kat sini y = 6 sebab a diincremen dulu sebelum assign ke y

y = a; // Kat sini pun a = 6

Yang --a dan a-- pun sama cume di decrement

Edited by CFoo++

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...