Jump to content

how to convert for to while very confusing

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
onat12agi@gmail.com

onat12agi@gmail.com

    Newbie

  • Members
  • Pip
  • 8 posts
turbo c
#include <iostream.h>

using namespace std;

int main()
{
	int i;
	
	for(i = 10; i > 2; i--)
	{
	printf("%d\n", i);
	}

	return 0;
}
to this Syntax:
while ( expression )  statement
my book's example (s = str;           // Let the char pointer s  
while( *s != '\0') // point to the end of str 
    ++s; 
) 
is confusing.

Edited by WingedPanther, 10 October 2008 - 08:08 AM.
add code tags


#2
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Equivalent loops:
   [B][COLOR=blue]int[/COLOR][/B] i;


   [COLOR=chocolate]/*

    * #1: while

    */[/COLOR]

   i = [COLOR=teal]0[/COLOR];

   [B][COLOR=blue]while[/COLOR][/B] ( i < [COLOR=teal]10[/COLOR] )

   {

      [COLOR=chocolate]// ...[/COLOR]

      ++i;

   }


   [COLOR=chocolate]/*

    * #2: do...while

    */[/COLOR]

   i = [COLOR=teal]0[/COLOR];

   [B][COLOR=blue]do[/COLOR][/B]

   {

      [COLOR=chocolate]// ...[/COLOR]

      ++i;

   } [B][COLOR=blue]while[/COLOR][/B] ( i < [COLOR=teal]10[/COLOR] );


   [COLOR=chocolate]/*

    * #3: for

    */[/COLOR]

   [B][COLOR=blue]for[/COLOR][/B] ( i = [COLOR=teal]0[/COLOR]; i < [COLOR=teal]10[/COLOR]; ++i )

   {

      [COLOR=chocolate]// ...[/COLOR]

   }


   [COLOR=chocolate]/*

    * #4: goto

    */[/COLOR]

   i = [COLOR=teal]0[/COLOR];

label:

   [COLOR=chocolate]// ...[/COLOR]

   ++i;

   [B][COLOR=blue]if[/COLOR][/B] ( i < [COLOR=teal]10[/COLOR] )

   {

      [B][COLOR=blue]goto[/COLOR][/B] label;

   }


#3
kresh7

kresh7

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 661 posts
Dident Test it but should work

#include <iostream>

using namespace std;

int main()
{
int i = 10;

while(i>2)
{
cout << i--;
}

return 0;
system("Pause");

}

Posted Image

#4
Steve.L

Steve.L

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 444 posts

kresh7 said:

Dident Test it but should work

#include <iostream>

using namespace std;

int main()
{
int i = 10;

while(i>2)
{
cout << i--;
}

return 0;
system("Pause");

}

What the **** is the cout for? And you made a call to system after you return, which defeats the purpose....

DCS's code seems clean.

#5
kresh7

kresh7

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 661 posts

Steve.L said:

What the **** is the cout for? And you made a call to system after you return, which defeats the purpose....

DCS's code seems clean.

Ups at the system call it was a mistake :H dude slow down im 3 weeks now in c++ programming :H mistakes happend just tryed to help from what i know until now dident know that he dont want to display output so i put cout :H
Posted Image

#6
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
Hmm, both codes are ok, it seems, if one ignores kresh7's system call... Look at dcs' code, it explains it well, yeah.
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#7
Steve.L

Steve.L

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 444 posts
Sorry, but you'd think it would be obvious that when you RETURN you don't go any further in the code. I suppose the cout is understandable.

#8
Guest_jacob123_*

Guest_jacob123_*
  • Guests
hi to all