Alvin-Pranata
Alvin-Pranata

Reputation: 23

How to Fix Blank Results when use for loop in c++

I am Alvin and I was a beginner in C++ Programing Languange and I have a code:

#include <iostream>
#include <string>

using namespace std;

int main(){
    for (int i=0; i == 5; i++){
    cout << i << ", ";
      }

system("pause"); // i add this code to avoid program close when i try to run it
return 0;
}

and when I compile it doesn't display an error message i.e is successfully compiled but, when I try to run it, it's doesn't display 'i' values i.e it's display blank screen. can somebody help me.

Upvotes: 2

Views: 118

Answers (2)

Dominique
Dominique

Reputation: 17493

You seem not to understand the meaning of the entries in a C for-loop:

for (int i=0; i==5; i++)

Means:

Start with i being zero (i=0)
Continue the loop, AS LONG AS i equals 5 (i==5)

In other words, it does NOT mean:

...
Continue the loop, UNTIL i equals 5

Hence, you need to replace i==5 by i<=5, because this means:

...
Continue the loop, AS LONG AS i is smaller or equal than 5 (i<=5)

Upvotes: 3

tdao
tdao

Reputation: 17678

You have a logic error in the loop test condition:

for (int i=0; i == 5; i++){  // the `==` will cause it to never enter the loop

should be:

for (int i=0; i <= 5; i++){

Upvotes: 0

Related Questions