How to build a connection for a loop in C ++?

Is there another for loop in the counter section (third part) of the for loop? In my attempt to get elegant text to create the right triangle, I wrote this, but it will not compile:

 #include <stdio.h> int main() { int i, j, N = 5; for (i = 1; i <= N; (for (j = 1; j <= i; j++, printf("%c", '0'));), i++) printf("\n"); } return 0; } 
+7
c ++ lambda for-loop c ++ 11
Sep 07 '14 at 17:13
source share
8 answers

No expressions or declarations are allowed only.

EDIT: Sorry. I thought you were talking about the state of a part of the loop. In the expression part of the loop, only expressions are allowed.

You can use a lambda expression that will contain this for the loop. for example

 for ( i = 1; i <= N; []( int i ) { for ( j = 1; j <= i; j++, printf("%c", '0' ) ); }( i ), i++) 

Here is a demo

 #include <iostream> int main() { int N = 10; for ( int i = 1; i < N; []( int i ) { for ( int j = 1; j < i; j++, ( std::cout << '*' ) ); }( i ), i++ ) { std::cout << std::endl; } return 0; } 

Output signal

 * ** *** **** ***** ****** ******* ******** 

Or you can define a lambda expression outside the outer loop to make the program more readable. for example

 #include <iostream> int main() { int N = 10; auto inner_loop = []( int i ) { for ( int j = 1; j < i; j++, ( std::cout << '*' ) ); }; for ( int i = 1; i < N; inner_loop( i ), i++ ) { std::cout << std::endl; } return 0; } 

Note that in general, nested loops shown in other posts cannot replace a loop with a lambda expression. For example, the outer loop may contain continue statements that will skip the inner loop. Therefore, if you want the inner loop to be executed in any case, regardless of the continue statements, this construction with a lambda expression will be useful. :)

+7
Sep 07 '14 at 17:16
source share

There is no need to do this. Because the loop can easily be replaced with a while loop, each part of the for loop can be placed in a different place where complex constructs can be used. In your case, you can simply change the loop to the following:

 for (i = 1; i <= N; i++) { printf("\n"); for (j = 1; j <= i; j++) { printf("%c", '0'); } } 

However, if you really need to pose a complex action, you can use the gcc extension (compound statement):

 for (i = 1; i <= N; ({for (j = 1; j <= i; j++) putchar('0'); }), i++) { printf("\n"); } 
+5
Sep 07 '14 at 17:24
source share

In the for() loop counter section, expressions are allowed, but no statements.
And each for() in C / C ++ forms a new statement (this is not an expression).

However, you can nest a few for() loops if you want.
For example, since you need a new loop in the counter sections, this means that you need to execute the loop at the end of the main for() loop.

This is the diagram:

  for (int i = 0; i < i_max; i++) { // stuff... for (int j = 0; j < j_max; j++) { // stuff.. } } 
+4
Sep 07 '14 at 17:21
source share

You cannot do this because the condition and the increment of the for parts can only contain expressions. The for loop is an iterative statement.

Just insert loops, for example, programmers-programmers:

 #include <stdio.h> int main() { int N = 5; for (int i = 1; i <= N; i++) { for (int j = 1; j <= i; j++) printf("0"); printf("\n"); } } 

If you feel bad, you can use lambda:

 #include <stdio.h> int main() { int N = 5; for ( int i = 1; i <= N; [=](){ for (int j = 1; j <= i; j++) printf("0"); }(), printf("\n"), i++ ) ; } 
+3
Sep 07 '14 at 17:26
source share

The shortest possible solution:

 main(i){for(i=1;i<11;printf("%0*d\n",i++,0));} 

Output:

 0 00 000 0000 00000 000000 0000000 00000000 000000000 0000000000 

Live demo

+2
Sep 07 '14 at 17:55
source share

Michael Barr hat tip for suggesting using lambda . And thanks to commentators asking me to use putchar() .

 #include <stdio.h> int main() { int N; scanf("%d", &N); for (int i = 0; i < N; i++, [i] { for (int j = 1; j <= i; j++, putchar('0')) ; }(), printf("\n")) ; return 0; } 

Live demo

+1
Sep 07 '14 at 17:42 on
source share

Elegance comes with clarity. When I want to create a character string, I create a C ++ object called std::string .

 #include <iostream> #include <string> int main() { char c = '0'; const int n = 5; for (int i = 1; i <= n; ++i) { std::cout << std::string(i, c) << '\n'; } } 

Thus, there is no need for a nested for -loop in this particular case. Otherwise, put a for -statement in the body of the outer loop, like the other answers.

+1
Sep 08 '14 at 14:51
source share

I think we should avoid confusing code if we are not paid to create the code.

0
Sep 08 '14 at 12:32
source share



All Articles