473,612 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
+ 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 9145
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.prin t("*");
}

System.out.prin tln();

}
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.prin t(prefix);

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

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

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


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





}


}
May 9 '10 #8

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

Similar topics

2
2752
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 through the data using for loops. How would I do this? The format that I am attempting is: Select data ... Get data ... Place data into a nested dictionary Rev = for value1 in a:
25
12694
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 increase. Does Python have a limit on the number of nested for-loops? Thanks.
46
9908
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 already in a loop." Then he goes on to portray a contrived example that doesn't tell me under what conditions a nested loop might be favoured as a solution? i.e. what are nested loops useful for? What kinds of algorithms are served by nested loops?...
10
3210
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 complexity analyzer tool supposedly does not pick it up. Is it really more efficient? Personally I find this coding style extremely cryptic, misleading and error-prone. I believe that I have removed all traces of proprietary-ness from this coding...
77
5193
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. Is this because .NET is inherently slower or does the C# compiler merely produce code that is not as well optimized as the C++ compiler?
4
8910
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
2223
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 more restrictive filters so that only the desired items can fall all the way through. This method is so obvious and common it must have a name. What is it or at least, what is the best (short) way to describe it? For Each In If Then If Then
14
19407
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 strange workaround construct): -- do { // ...
8
5924
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 some functions. I have defined them in the following manner: typedef struct trans Transient; typedef struct sats Satellites;
25
4706
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
8162
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8605
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8565
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8246
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7039
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6076
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5532
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4109
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1695
muto222
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.