Click here to Skip to main content
14,920,483 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include<iostream>
using namespace std;

int main()
{
int i = 1;
while (i < 3 ){
int j = 1 ;
while (j<5){
if (j==3){
  break;
}
cout <<j<<" ";
j++;
}
i++;
}
}


What I have tried:

I have tried doing a question on while loop.!!
Posted
Updated 12-May-21 8:46am
Comments
jeron1 12-May-21 13:48pm
   
You break out of the while loop that prints the value of j when j equals 3.

First things first, fix up your indentation and brace formatting. ALWAYS STICK WITH ONE WAY OF USING BRACES. You've got a mix of them.
C++
#include<iostream>
using namespace std;

int main()
{
    int i = 1;
    while (i < 3 )
    {
        int j = 1;
        while (j < 5)
        {
            if (j == 3)
            {
                break;
            }
            cout << j << " ";
            j++;
        }
        i++;
    }
}
To figure out why it's giving you the output you're getting, use the debugger. Step through the code line-by-line, hovering the mouse over variables to see their values. Watch what's going on and you can figure this out.

The debugger is there to debug YOU and your understanding of the code and how it works.
   
Comments
k5054 12-May-21 14:30pm
   
To be fair, K&R also puts braces on the same line as an if, while, for, etc. but on a separate line for functions. And so does Stroustrup, so I think the OP is in good company with his brace style
Dave Kreskowiak 12-May-21 14:51pm
   
I hate the "brace on the same line" format. It makes it too easy to mismatch braces, especially so for beginners.
jeron1 12-May-21 15:34pm
   
Amen!
Richard MacCutchan 12-May-21 16:35pm
   
Me too.
k5054 12-May-21 16:59pm
   
I guess it's all what you're used to. I've been doing K&R style since before the days of ANSI C standard. Like many, I learned C programming from their book. While I use the "hanging brace" style for inner control blocks, I have a strong aversion to hanging braces on the same line as the function definition. That's just wrong. There's a wikipedia page about indentation styles, for the interested: Indentation style - Wikipedia[^]
Dave Kreskowiak 12-May-21 17:25pm
   
Allman all the way.

Looking at that list, Haskell is an abomination.
Greg Utas 13-May-21 8:41am
   
Every one of them, save Allman, is an abomination! :)
Quote:
I am getting an output of 1 2 1 2 ...why not 1 2 only..?

Because you print from inside a loop inside a loop.
Obviously, you don't understand your code, if it is yours.
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900