For loop

Pages: 12
I typed the following coding on turbo c++ compiler
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j;
for(i=5;i>=1;i--)
{
for(j=5;j>=i;j--)
{
print("%d",j);
printf("\n");
}
getch();
}
now the output I estimated was
5
4
3
2
1
but instead it is
4
3
2
1
0
Now how is it possible
j starts from 5. . & j is >=5 i.e. j>i, condition is satisfied. .
so first 5 should print in any way . . !!!! am i tracing the program wrong ??
plz guide me
I do not know what is the function print? Maybe did you mean printf? See here

for(j=5;j>=i;j--)
{
print("%d",j);
printf("\n");
}

And there is no additional closing bracket in your code.
Last edited on
Please Use Code Tags

It will be a lot easier to help you.
@vlad from moscow. .Ya sorry the correct one is this
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j;
for(i=5;i>=1;i--)
{
for(j=5;j>=i;j--)
{
printf("%d",j);
printf("\n");
}
}
getch();
}

now plz cn u solve my query !!!!
@Script Coder
ya nw i hv used code tags now. . plz cn u trace out the prog loigic for me
closed account (zb0S216C)
Spelling, grammar, and punctuation aside, there's a few things I find wrong with your code:

1) conio.h (Console I/O) isn't standard
2) main() should return int, not void
3) I have no idea what your code is supposed to do; comments would help in this regard.

Wazzak
Last edited on
Many people have told you to use code tags, if you're unaware how to do so then look here

Simply type [code] Before all your coding and [/code] after your coding...
The result should look like this...

1
2
3
example code line 1
example code line 2
example code line 3
Here is your code written in correct C++. printf is to be avoided; in the future please consider using cout.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <cstdio>
 
using namespace std;
int main()
{
  int i,j;
  for(i=5;i>=1;i--)
  {
     for(j=5;j>=i;j--)
    {
       printf("%d",j);
       printf("\n");
    }
  }
}


Here is a test run:
http://ideone.com/usz0y

The only value ever output is j. Each time through the loop, j starts at five and counts down until it is lower than i, and then i is lowered and the loop goes round again.

So the first time, i is 5, so output j is 5.

Next time, i is 4, so output j is 5 and then 4.

Next time, i is 3, so output j is 5 then 4 then 3.

etc etc.


My usual comments here apply; Turbo C++ is twenty years old, it's not C++, you're learning something that isn't C++ and would be rejected by a C++ compiler, please stop using old tools when you could be using new, correct tools for free.

If this tool is mandated by your school, consider complaining to the school that you're paying a fortune to be educated and they're wasting your time and money by teaching this.
Last edited on
If i am doing that in c. . then plz tell me the logic behind it. . !!!!!!!!!!
My post already explained the logic.
thkx got it. .
problem in c++ operating overloading
#include<iostream.h>
#include<conio.h>
class counter
{
int cnt;
public:
counter()
{
cnt=0;
}
int operator++()
{
return(++cnt);
}
void display()
{
cout<<cnt<<endl;
}
};
main()
{
clrscr();
counter c1;
int a =c1++;
cout<<a<<"\t";
c1.display();
getch();
return(0);
}
now the output is coming 1 1
can some1 plz trace the program logic
The logic is simple. The compiler uses the defined operator ++ for postincrementing ++. So the statement

int a = c1++;

is equivalent to

int a = c1.operator ++();

Inside the operator cnt is incremented and returned from the operator.

If you want realize a postincrement you should declare the following operator

1
2
3
4
int operator ++( int )
{
   return ( cnt++ );
}


In this case after executing the statement

int a = c1++;

a will be equal to 0 and cnt inside c1 will be equal to 1.
Last edited on
alright i got it. . Thanks a lot
problem in equal operator overloading

#include<iostream.h>
#include<conio.h>
class A
{
int x,y;
public:
A()
{
x=y=0;
}
A(int x1,int y1)
{
x=x1;
y=y1;
}
A operator=(A obj)
{
x=obj.x*2;
y=obj.y*2;
return obj;
}
void display()
{
cout<<x<<y<<endl;
}
};
int main(void)
{
clrscr();
A a1(5,6);
A a2,a3;
a3=a2=a1;
a2.display();
a3.display();
getch();
return 0;
}

output is
10 12
10 12
now i have been told that the function will be called like this:
a2=a1
and then
a3=a1
why wont it be like
a2=a1
and then
a3=a2
wont it be in sequence ????
plz guide me


You have no "equal" operator in your code. I think you meant the assignment operator. It should be defined as

1
2
3
4
5
6
7
A & operator= ( const A &obj )
{
   x = 2 * obj.x;
   y = 2 * obj.y;

   return ( *this );
}


Though it is better do not use other operations apart of the assignment with members of the object. Th expression x = 2 * obj.x; will confuse a user that will think that after the assignment x == obj.x.
Ya i mean assignment operator.. u r nt getting my point
actually i mean to say that the program is absolutely alright
the only prob is that i dont understand that . . how it is traced
when the function is called like
a3=a2=a1;
i have been told that first
a2 calls a1 and then a3 calls a1
why so i dont undstd ??????
cant it be like a2 calls a1 first and then a3 calls a2
thk u :D
Cn u spk mr clearly like full words so we cn understand u?
And will you use the code tags - select the code, then press the <> button on the right.

printf is to be avoided; in the future please consider using cout.


cout is for sissies.

But seriously, it is a matter of taste. I like prinf more. There is even printf in Java.
Pages: 12