Question

C Programming Can someone please explain me while the following (c) code prints out 6? for...

C Programming

Can someone please explain me while the following (c) code prints out 6?

for (i=1; x<=5; i++);

printf ("%d",i);

i=1 initializes the the loop, it is being tested whether it is <= 5 or not. This statement is true, so it goes into the loop and prints 1.

After that, i++, means 1+1=2, i=2. This statement is being tested and it is true, we go into the loop and print 2.

Then i=3, true, loop prints 3 --> i=4, true, loop prints 4 --> i=5, true, loop prints 5 ---> i=6, false, no loop, don't print 6??!!

I have tried and changed the testing condition into i==0, and it still prints 1. Why would it do that? The Expression i=1 is not true?

Thanks

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please notice the semicolon ';' after the for loop.

This means that the statement "printf ("%d",i);" is not a part of the body of the for loop.

Instead this statement is executed when the for loop execution is over.

Let us see how the for loop executes.

Iteration 1: i = 1. Clearly, 1 <= 5 is true, hence increment i to 2

Iteration 1: i = 2. Clearly, 2 <= 5 is true, hence increment i to 3

Iteration 1: i = 3. Clearly, 3 <= 5 is true, hence increment i to 4

Iteration 1: i = 4. Clearly, 4 <= 5 is true, hence increment i to 5

Iteration 1: i = 5. Clearly, 5 <= 5 is true, hence increment i to 6

Iteration 1: i = 6. Clearly, 6 <= 5 is false, hence exit the loop

Hence, after the loop exits, the value of i is 6.

Therefore, printf ("%d",i) outputs 6.

Add a comment
Know the answer?
Add Answer to:
C Programming Can someone please explain me while the following (c) code prints out 6? for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can someone explain these three questions for me? Thanks. Answers are provided Use the code segment...

    Can someone explain these three questions for me? Thanks. Answers are provided Use the code segment below for problems 6-7 int x = 1; int y - 0 int z 1: if(! (x && y) ) x 0; İf ( ! (x 11 y) ) y+= (x + y) % 2 == 0 ? 1 : 0; printf ("Total #1 : %d\n", x + y + z); printf ("Total #2: %d\n", !x + !y + !2); 6. Which of the...

  • Can someone help me with this Python code Summary In this lab, you work with the...

    Can someone help me with this Python code Summary In this lab, you work with the same Python program you worked with in Labs 5-1 and 5-3. As in those earlier labs, the completed program should print the numbers 0 through 10, along with their values multiplied by 2 and by 10. However, in this lab you should accomplish this using a while loop with a break statement. Instructions Make sure that the file NewestMultiply.py is selected and open. Write...

  • Please help in C: with the following code, i am getting a random 127 printed in...

    Please help in C: with the following code, i am getting a random 127 printed in front of my reverse display of the array. The file i am pulling (data.txt) is: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 when the reverse prints, it prints: 127 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 not sure why...please...

  • Please can someone help me to finish this program? It is a C programming not a...

    Please can someone help me to finish this program? It is a C programming not a C++, when programming the program in linux I keep getting expected expression before || token, also please check if everything else is fine, maybe there is something else worng in my program and may ask me later to fix it, I am concern about the sin and cosine. The program is supposed to do all this: 1.Display a welcome message. 2.Prompt for the height...

  • in c code please 10 Question 6 The following program should print out the multiplication tables...

    in c code please 10 Question 6 The following program should print out the multiplication tables in reverse, but it is not working correctly. Fix the program so that it prints a times table of digits 1 to 9 (inclusivel. int main() for (int 1-1 ; ) for (int j = "; 1 ; I--) printf("220", 11) > return; Upload Choose a File

  • #Python Help please Modify the mistakes in my code , so that it prints the same...

    #Python Help please Modify the mistakes in my code , so that it prints the same statement four times. Using four distinct techniques—hard-coding, commas, concatenation, and string formatting. The hard-coding is already done. The other three techniques have been started but they contain mistakes. These techniques should each use all three of the provided variables. Once the mistakes have been corrected, run the script and upload a picture of the program running to make sure that it prints the statement...

  • i have no idea how to do floating point comparison this is c programming CHALLENGE 3.16.1:...

    i have no idea how to do floating point comparison this is c programming CHALLENGE 3.16.1: Floating-point comparison: Print Equal or Not equal Write an expression that will cause the following code to print "Equal" if the value of sensorReading is "close enough" to targetValue. Otherwise, print "Not equal". Ex: If targetValue is 0.3333 and sensorReading is (1.0/3.0), output is Equal 1 #include <stdio.h 2 #include <math.h> 4 int main(void)f 6 8 scanf("%lf", &targetValue); 1 test passed double targetValue; double...

  • 2. What is the output of the following code fragment? n = 1; while (n <=...

    2. What is the output of the following code fragment? n = 1; while (n <= 5) { n++; cout << n << ' '; a.1 2 3 4 5 b. 1 2 3 4 c. 1 1 1 forever d. 2 3 4 5 e. 2 3 4 5 6 3. What is the termination condition for the following While loop? while (beta > 0 && beta < 10) { cout << beta << endl; cin >> beta; }...

  • Need help answering this java programming question. Thank you in advance! g. Re-code the following as...

    Need help answering this java programming question. Thank you in advance! g. Re-code the following as a do-while loop with a switch statement for the if-elses, Assume goingToMovies is declared and has been set to true, so it can enter the loop. Assume choice, movie, Cont and input are already declared. while(goingToMavies) Sustem.aut printf("%nChoose a number from 1 through 5 for the movie" + "you want to watch: ") choice inputnextint) iffchoice 1) movie "Shazam!" else iffchoice 2) movie "A...

  • I know the answer is D, but can someone explain to me how to do this...

    I know the answer is D, but can someone explain to me how to do this problem? Thanks. D The following pedigree corresponds to two families that carry a non-threatening autosomal dominant condition. What are the chances of individuals 11-3 and 11-4 to have a child with the genetic condition? A. 3/4 B. 1/6 C. 5/6 D. 8/9 E. 4/36 ?

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT