C++ for loops in a single line


 
Thread Tools Search this Thread
Top Forums Programming C++ for loops in a single line
# 1  
Old 12-10-2012
C++ for loops in a single line

I am wondering whether I can write for loops in the following ways

Code:
for (int i = 0; i < NL; i++) L[i]->read_param(P, i + 1);
for (int k = 0; k < lay; k++) sum += L[k]->get_npar();
for (int i = 0; i < NL; i++) L[i] = new Layer(Xi, Xf);

for (int i = 0; i < NL; i++) {
   for (int j = 0; j < NL; j++) L[i][j] = new Layer(Xi, Xf);
}

# 2  
Old 12-10-2012
Yes, you can - C++ doesn't mandate that you use a newline in this case. However, I find that much harder to read than with a newline and proper spacing, e.g.:

Code:
for (int i = 0; i < NL; i++)
    L[i]->read_param(P, i + 1);

for (int k = 0; k < lay; k++)
    sum += L[k]->get_npar();

for (int i = 0; i < NL; i++)
    L[i] = new Layer(Xi, Xf);

# 3  
Old 12-10-2012
Quote:
Originally Posted by kristinu
I am wondering whether I can write for loops in the following ways

Code:
for (int i = 0; i < NL; i++) L[i]->read_param(P, i + 1);
for (int k = 0; k < lay; k++) sum += L[k]->get_npar();
for (int i = 0; i < NL; i++) L[i] = new Layer(Xi, Xf);

for (int i = 0; i < NL; i++) {
   for (int j = 0; j < NL; j++) L[i][j] = new Layer(Xi, Xf);
}

Yes, you CAN.

But why would you want to? If you get a compiler error it's a lot harder to figure out what's wrong, and if you're running a debugger you have a lot less control over stepping through your code line-by-line, and a lot more limits on where you can put a break statement.

The goal is not to write complex, dense code that pushes the limits of compiler standards. Real-world coding is not a contest to see who can use the most obscure features of a language.
# 4  
Old 12-10-2012
Would you apply the newline thing to if statements as well?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multi line log files to single line format

I want to read the log file which was generate from other command . And the output was having multi line in log files for job name and server name. But i need to make all the logs on one line Source file 07/15/2018 17:02:00 TRANSLOG_1700 Server0005_SQL ... (2 Replies)
Discussion started by: ranjancom2000
2 Replies

2. UNIX for Beginners Questions & Answers

Output to file print as single line, not separate line

example of problem: when I echo "$e" >> /home/cogiz/file.txt result prints to file as:AA BB CC I need it to save to file as this:AA BB CC I know it's probably something really simple but any help would be greatly appreciated. Thank You. Cogiz (7 Replies)
Discussion started by: cogiz
7 Replies

3. UNIX for Dummies Questions & Answers

To find and display the middle line in a file using single line command.

Hi all, How can i display the middle line of a file using a single line command? (6 Replies)
Discussion started by: Lakme Pemmaiah
6 Replies

4. Shell Programming and Scripting

Comparing two strings receiving form two different loops and execute if condition when single match

I want to read a file contain sub-string and same string need to match in file name I got from for loop. I am using below code: #!/bin/bash C_UPLOADEDSUFFIX='.uploaded' files=$(find . -iname '*'$C_UPLOADEDSUFFIX -type f) # find files having .uploaded prefix for file in $files do ... (1 Reply)
Discussion started by: ketanraut
1 Replies

5. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

6. Shell Programming and Scripting

how to use loops to take the values line by line

Hi am using aix ... I have tried so far but am getting unknown test operator error for example i have three files in one dir ... A2008 A2408 A2808 final output results should be A0108 is missing --- A1908 is missing A2108 is missing ..... #! /bin/ksh i=`ls A* |cut -c2-3` j=`date... (3 Replies)
Discussion started by: Venkatesh1
3 Replies

7. Shell Programming and Scripting

Joining multi-line output to a single line in a group

Hi, My Oracle query is returing below o/p ---------------------------------------------------------- Ins trnas value a lkp1 x a lkp1 y b lkp1 a b lkp2 x b lkp2 y ... (7 Replies)
Discussion started by: gvk25
7 Replies

8. Shell Programming and Scripting

Multiple lines in a single column to be merged as a single line for a record

Hi, I have a requirement with, No~Dt~Notes 1~2011/08/1~"aaa bbb ccc ddd eee fff ggg hhh" Single column alone got splitted into multiple lines. I require the output as No~Dt~Notes 1~2011/08/1~"aaa<>bbb<>ccc<>ddd<>eee<>fff<>ggg<>hhh" mean to say those new lines to be... (1 Reply)
Discussion started by: Bhuvaneswari
1 Replies

9. Shell Programming and Scripting

Preserving newlines when writing loops on the command line in bash

Dear All, I have a question that's been difficult to get an answer to. I often write command line loops, e.g. find files, print name, grep for term, apply sed, etc I use both zsh and bash. When I write a loop e.g. for line in `more myfile.txt` > do > echo $line > done but... (2 Replies)
Discussion started by: JohnK1
2 Replies

10. Shell Programming and Scripting

single line input to multiple line output with sed

hey gents, I'm working on something that will use snmpwalk to query the devices on my network and retreive the device name, device IP, device model and device serial. I'm using Nmap for the enumeration and sed to clean up the results for use by snmpwalk. Once i get all the data organized I'm... (8 Replies)
Discussion started by: mitch
8 Replies
Login or Register to Ask a Question