jeudi 15 décembre 2016

Why does the program give me a different result when I use if statement

Why does the program give me a different result when I use if statement.

If I use else if statement It prints a 5. However. If I change else if to a if statement it prints a completely different picture. Could anyone tell me why is that?

#include<iostream>
using namespace std;

 // Print 5.
 int main() {
 int n=5;
 for(int row=1;row<=2*n-1;row++)    
  {
 for(int col=1;col<=n;col++)
  {
    if(row==1||row==n||row==2*n-1)
    cout<<"*";
     else if (col==1&&row<n||col==n&&row>n)
    cout<<"*";
    else
    cout<<" ";
 } 

 cout<<endl;
   }
 return 0;
 }

I always thought if and else if are the same.

Aucun commentaire:

Enregistrer un commentaire