473,485 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Nested loop structure

1 New Member
hi
i am beginner in java and i need help with programining ,Please if you could suggest me some sites where i can see some solved examples of codes as well as i need code for following

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

I need a code to make above mentioned , in nested loop form.

thanks and regards
Oct 28 '06 #1
7 9134
r035198x
13,262 MVP
hi
i am beginner in java and i need help with programining ,Please if you could suggest me some sites where i can see some solved examples of codes as well as i need code for following

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

I need a code to make above mentioned , in nested loop form.

thanks and regards
There are many ways of doing this:
Expand|Select|Wrap|Line Numbers
  1. public class Test {
  2.     public static void main (String []args) {
  3.         int i = 0;
  4.  
  5.         for(int j = 1; j <= 5; j++) {
  6.             if(j == 1 || j == 5) {
  7.                 i = 4;
  8.             }
  9.             else {
  10.                 i = 1;
  11.             }
  12.             for(int x = 0; x < i; x++) {
  13.                 System.out.print("**");
  14.             }
  15.             System.out.println();
  16.  
  17.         }
  18.     }
  19. }
Oct 30 '06 #2
fido1234
2 New Member
@ayesha
same as his question...do u hve a code for this one:
*
* *
* * *
* *
*

ty...

must be look like a diamond...
Jan 7 '09 #3
r035198x
13,262 MVP
We no longer hand out boilerplate codes. What have you tried so far?
Jan 7 '09 #4
fido1234
2 New Member
@r035198x
i tried to edit the code above but i cant come up with the result i want...im just a beginner to this language hope you can help me ty
Jan 7 '09 #5
r035198x
13,262 MVP
Don't try to edit that code. Start with a for loop and start printing out the stars with spaces at the correct location. Edit that code to so it gets as close to what is required as possible. Keep on editing and testing until you get the right picture.
That is the best way to learn the language. Make sure you understand the results of each edit that you make.
Jan 7 '09 #6
kamalFromEgypt
2 New Member
public static void main(String[] args) {

int flag;
for (int i = 1; i <= 5; i++) {
flag=i;
if (i==4) {
flag=2;
} else if (i==5){
flag=1;
}

for (int j = 0; j < flag; j++) {
System.out.print("*");
}

System.out.println();

}
May 9 '10 #7
kamalFromEgypt
2 New Member
*
****
******
********
**********
************
**************
****************
******************
********************
**********************


public class Pyramid {

/**
* @param arg
*/
public static void main(String arg[]){

char prefix=' ';
int lines=20;
int spaces=45;

for (int i=0;i<=lines;i++)
{


for(int j=spaces; j>0; j--)
System.out.print(prefix);

if(i==0)
{ System.out.print("*");
System.out.print('\n');
lines--;
spaces--;
continue;
}

for(int k=0;k<=i;k++)
System.out.print("*");

for(int k=0;k<=i;k++)
System.out.print("*");


System.out.print('\n');
lines--;
spaces--;
}





}


}
May 9 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2737
by: kbass | last post by:
I am new to Python and I am attempting to retrieve data from a database and I would like to place this data into a nested dictionary. After placing the data into a dictionary, I would like to loop...
25
12661
by: chad | last post by:
I am writing a program to do some reliability calculations that require several nested for-loops. However, I believe that as the models become more complex, the number of required for-loops will...
46
9863
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
10
3178
by: nimmi_srivastav | last post by:
Below you will see an example of a nested conditional expression that this colleague of mine loves. He claims that it is more efficient that a multi-level if-else-if structure. Moreover, our...
77
5149
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
4
8901
by: so.intech | last post by:
for example, ret = 0; for(i=0; i<3; i ++;) { for(j=0; j<4; j++;) { for(k=0; k<3; k++;) { for(m=0; m<4; m++;) {
25
2185
by: GY2 | last post by:
I writing some documentation and I want to describe a common code structure which is used to step through all the items in a collection (e.g. each file in a subdirectory) while applying more and...
14
19395
by: Jan Schmidt | last post by:
Hi, in a nested do-while-loop structure I would like to "continue" the outer loop. With goto this should be no problem in while-loops. However, for do-while I cannot get it to work (without a...
8
5890
by: Sheldon | last post by:
Hi, Can anyone help with this problem with setting up nested structures and initializing them for use. I have created several structs and placed them in a super struct that I will then pass to...
25
4674
by: Andreas Eibach | last post by:
Hi again, one of the other big woes I'm having... typedef struct perBlockStru /* (structure) Long words per block */ { unsigned long *lword; } lwperBlockStru_t;
0
7090
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6960
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7116
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6825
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5418
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4551
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3063
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1376
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
595
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.