Schleife + 4



  • Hallo kann man die vierer Reihe bis 20 nur so ausgeben lassen ?

    #include <iostream>
    using namespace std;
    
    int main()
    {
            for (int i = 0; i < 20; i+4)
            {  i=i+4;
                    cout << i <<endl;
            }
    
    system("PAUSE");
    }
    

    Wieso funktioniert das so nicht ?

    Ich bekomme immer nur 0 als Ausgabe.

    #include <iostream>
    using namespace std;
    
    int main()
    {
            for (int i = 0; i < 20; i+4)
            {
                    cout << i <<endl;
            }
    
    system("PAUSE");
    }
    


  • #include <iostream>
    using namespace std;
    
    int main()
    {
            for (int i = 0; i < 20; i+=4) // +=4, nicht +4!
            {
                    cout << i <<endl;
            }
    
    system("PAUSE");
    }
    

    Aber die einzige Möglichkeit ist das sicher nicht. Versuch es mal mit while 😉



  • l'abra d'or schrieb:

    #include <iostream>
    using namespace std;
    
    int main()
    {
            for (int i = 0; i < 20; i+=4) // +=4, nicht +4!
            {
                    cout << i <<endl;
            }
    
    system("PAUSE");
    }
    

    Aber die einzige Möglichkeit ist das sicher nicht. Versuch es mal mit while 😉

    oder

    for(int i = 1;i <=4;++i)
    {
    cout << i*4 << endl;
    }
    


  • Das ist aber nur 4,8,12,16, da fehlt die 0.



  • Einzeiler:

    cout << "0\n4\n8\n12\n16" << endl;
    

    *SCNR*



  • pumuckl schrieb:

    Einzeiler:

    cout << "0\n4\n8\n12\n16" << endl;
    

    *SCNR*

    Ist auch nicht das gleiche, da hier weniger geflusht wird. 😛


  • Mod

    Hier die obligatorische TMP Lösung:

    #include <iostream>
    
    using namespace std;
    
    template< int base, int from_zero_to_this_value, int step >
    class loop
    {
    public:
      static inline void exec(){
        cout <<  base  << '\n';
        loop< base+step,from_zero_to_this_value-step, step >::exec();
      }
    };
    
    template< int base, int step >
    class loop<base, 0,step>
    {
    public:
      static inline void exec(){
        cout << base << endl;
      }
    };
    
    int main()
    {
      loop<0,20,4>::exec();
    }
    

Anmelden zum Antworten