Machine-translated by Google. Questions are authored in English.
Top

Discussion

#include <stdio.h>   
int main() 
{ 
  int i;   
  for (i = 1; i != 10; i += 2) 
    printf(" GeeksQuiz "); 
  return 0; 
}

 

  • A.GeeksQuiz GeeksQuiz GeeksQuiz GeeksQuiz GeeksQuiz
  • B.GeeksQuiz GeeksQuiz GeeksQuiz .... infinite times
  • C.GeeksQuiz GeeksQuiz GeeksQuiz GeeksQuiz
  • D.GeeksQuiz GeeksQuiz GeeksQuiz GeeksQuiz GeeksQuiz GeeksQuiz

Answer: B

Correct answer: GeeksQuiz GeeksQuiz GeeksQuiz .... infinite times

i starts at 1 and rises by 2, so it takes only the odd values 1, 3, 5, 7, 9, 11, … and never equals 10. The test i != 10 is therefore never false. This is why < is safer than != in a loop condition whose variable moves by more than one.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.