Click here to Skip to main content
14,331,729 members
Rate this:
Please Sign up or sign in to vote.
See more:
using break to input numbers till the value of n but max=10 but output of the program is 1 whatever be the value of n

What I have tried:

#include<stdio.h>
int main()
{
int i=1,n;
printf("enter value of n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
    printf("%d",i);
    if(i=10)
    break;
}
}

output
enter value of n7
1
Posted
Updated 21-Feb-17 10:31am
Comments
jeron1 21-Feb-17 15:41pm
   
Take a good look at this line.
if(i=10)

It doesn't do what you think it does.
Member 13015247 21-Feb-17 16:06pm
   
THATS OK AND REPLACING THAT WITH == SOLVES THE PROBLEM BUT HOW = AFFECTS THE LOOP THATS THE THING IAM ASKING
jeron1 21-Feb-17 16:16pm
   
Find the caps lock will you? and no that's not what was asked. '==' is a comparison operator as in "is i equal to 10?", and '=' is as assignment operator, as in "set i equal to 10"
Rate this:
Please Sign up or sign in to vote.

Solution 1

You really need to stop posting these questions; this is not the way to learn programming. Get yourself a good book on C or C++ and start reading.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

You seriously need to pick up beginners book on C/C++ and work through it.

You also need to learn how to use the debugger, set breakpoints and inspect variable contents. The debugger is there to debug YOU and your understanding of the code. Right now, you're just guessing at what the code is doing. You have no understanding of what you're writing and posting question after question on forums is not helping solve that problem. You're bypassing actual learning.
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 5

i=10 assign 10 to variable i and
i==10 compares 10 to variable i.
that is very basic C language, you need to learn it seriously.

Here is links to references books on C and C++ by the authors of the languages. Note than C is the ancestor of C++, so knowing C is always useful with C++.
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]

C++ Programing Language[^]

Learning to use a debugger will help too.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger 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, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

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 find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
   
Rate this:
Please Sign up or sign in to vote.

Solution 4

A note to help you in future endeavors. When doing conditionals (including loops where applicable), its often easier to debug when placing comparison values that cannot change on the left side of the check.

Take the below code for example. It does do a check but after it sets the value of 10 to the variable i. Use double equals for equality checking.
if (i = 10) 


However the point I want to make is if you placed the non changeable value on the left, the compiler will yell at you for mistakes like this.

if (10 = i) 
// Results in compiler error.

if (10 == i)
// The above is valid now that the debugger threw the exception for you to fix.

I do this will most conditionals, including null checks as the same thing would occur for that. It cannot be 'set' to anything, so compiler exception is thrown.

And as Dave and Richard have said, getting a good book and/or going through some tutorials may help you debug your issues.
   
Comments
Ramza360 21-Feb-17 16:36pm
   
Why the down vote, it explains the issue, and a good programming concept.
Dave Kreskowiak 21-Feb-17 17:13pm
   
Probably because you repeated an answer(s) that already covered what you said.
Ramza360 21-Feb-17 17:17pm
   
Everyone on here repeated the previous solution / comments. I added an extra piece of *help* that most tutorials will not point out about reversing the comparison to have non-modifiable values on left side.

Regardless, the OP has a few answers to *hopefully* learn from :)
Dave Kreskowiak 21-Feb-17 18:25pm
   
You haven't been keeping track of what this guy is doing. He's not really putting in the effort to learn anything. He's a help vampire.
Ramza360 22-Feb-17 12:21pm
   
O.o gotcha
Rate this:
Please Sign up or sign in to vote.

Solution 3

Hello,

If you do not have any books, MSDN can help you.

Have a look at this article:
[^]

You will notice that "=" does not mean equal, as in equality, it means assignment. To test equality you must use "==".

what is happening in your code is that in the first iteration of your loop you assign i to 10 and when it goes into the test of the for it breaks out of the loop (as i is now 10)

what you should have done is used if (i==10) and then it works.

Valery
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100