Iterationen (Schleifen)
Schreiben Sie ein Programm, das 10-mal "Hopp Schwiiz" schreibt. Schreiben Sie das Programm
- zunächst als Sequenz (10 Befehle hintereinander),
- danach als Iteration mit einer Variablen (integer) zaehler := 1 bis 10 und
- zuletzt mit einer Variablen i, beginnend bei i := (-5) bis (?) na, was wohl?
0 Kommentare
13 Lösung(en)
# Schlaufe
for i in range(10):
print "Hopp Schwiiz"
Lösung von: Martin Guggisberg (Universität Basel / PH FHNW)
/* author: philipp gressly freimann - dez. 2010 */
#include <stdio.h>
void main(int argc, char* args) {
int zaehler = 10;
while(zaehler > 0) {
printf("Hopp Schwiiz\n");
zaehler = zaehler - 1;
}
}
# author: philipp gressly freimann - dez. 2010
zaehler=10
while [ ${zaehler} -gt 0 ]
do
echo "Hopp Schwiiz"
zaehler=`expr ${zaehler} - 1`
done
10.times do puts "Hopp Schwiiz" end
print "Hopp Schwiiz\n" * 10
-5.upto(4).each do puts "Hopp Schwiiz" end
Lösung von: Name nicht veröffentlicht
package ch.programmieraufgaben.iteration;
/**
* @author Philipp Gressly (phi AT gressly DOT ch)
*/
public class Iterationen {
public static void main(String[] args) {
new Iterationen().top();
}
void top() {
int zaehler = -5;
while(zaehler < 5) {
System.out.println("Hopp Schwiiz");
zaehler = zaehler + 1;
}
}
} // end of class HoppSchwiiz
Lösung von: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)
HOPP
{
system.out.println("Hopp Schwiiz");
system.out.println("Hopp Schwiiz");
system.out.println("Hopp Schwiiz");
system.out.println("Hopp Schwiiz");
system.out.println("Hopp Schwiiz");
system.out.println("Hopp Schwiiz");
system.out.println("Hopp Schwiiz");
system.out.println("Hopp Schwiiz");
system.out.println("Hopp Schwiiz");
system.out.println("Hopp Schwiiz");
}
Lösung von: Name nicht veröffentlicht
print "Hopp Schwiiz\n" x 10;
for($i=0; $i<10; $i++){
print "Hopp Schwiiz\n";}
for($i=-5; $i<5; $i++){
print "Hopp Schwiiz\n";}
Lösung von: Name nicht veröffentlicht
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
int main()
{
for(int iZähler = 0; iZähler < 10; iZähler += 1) {
printf("Hopp Schwiiz\n");
}
getchar();
return 0;
}
Lösung von: Elias Zech (Optics Balzers)
public class HoppSchwizKlasse {
public static void main(String[] args) {
int iZahl = 10;
String str = "Hopp Schwiz";
for(int i = 0; i < iZahl; i += 1) {
System.out.println(str);
}
}
}
Lösung von: Elias Zech (Optics Balzers)
function schleife(i)
{
var zaehler = 1;
var s = ("");
while (zaehler <= i)
{
var s = (s + "Hopp Schwiiz\n");
var zaehler = zaehler * 1 + 1;
}
return s;
}
var i = prompt("Wie viele Male soll er den Text schreiben?", "10");
var s = schleife(i);
alert(s);
Lösung von: Pascal Gressly (Sekundarschule Thalwil)
<html>
<body>
<?php
//Nummer 1
echo "Hopp Schwiiz<br>";
echo "Hopp Schwiiz<br>";
echo "Hopp Schwiiz<br>";
echo "Hopp Schwiiz<br>";
echo "Hopp Schwiiz<br>";
echo "Hopp Schwiiz<br>";
echo "Hopp Schwiiz<br>";
echo "Hopp Schwiiz<br>";
echo "Hopp Schwiiz<br>";
echo "Hopp Schwiiz<br><br><br>";
//
//Nummer 2
$arr = array("Hopp Schwiiz", "Hopp Schwiiz", "Hopp Schwiiz", "Hopp Schwiiz", "Hopp Schwiiz", "Hopp Schwiiz", "Hopp Schwiiz", "Hopp Schwiiz", "Hopp Schwiiz", "Hopp Schwiiz");
foreach ($arr as $value)
{
echo $value . "<br>";
}
//
//
echo "<br><br>";
//
//Nummer 3
for ($i=-5; $i<5; $i++)
{
echo "Hopp Schwiiz<br>";
}
//
?>
</body>
</html>
Lösung von: Maik Scheiermann (Powercloud GmbH)
// sequenz
document.write("<p>Hopp Schwiiz<br>");
document.write("Hopp Schwiiz<br>");
document.write("Hopp Schwiiz<br>");
document.write("Hopp Schwiiz<br>");
document.write("Hopp Schwiiz<br>");
document.write("Hopp Schwiiz<br>");
document.write("Hopp Schwiiz<br>");
document.write("Hopp Schwiiz<br>");
document.write("Hopp Schwiiz<br>");
document.write("Hopp Schwiiz</p><hr>");
// iteration
for (var x = 1; x <= 10; x++) {
if (x == 1) document.write("<p>");
document.write("Hopp Schwiiz<br>");
if (x == 10) document.write("</p><hr>");
}
// variable
var i = -5;
while (i <= 4) {
if (x == -5) document.write("<p>");
document.write("Hopp Schwiiz<br>");
if (x == 4) document.write("</p>");
i++;
}
Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)
// C++ 14 | VS-2022
#include <iostream>
#include <vector>
#include <sstream>
auto iter1(const char* txt, int first, int last) {
for (auto i{first}; i <= last; ++i) std::cout << txt << "\n";
}
auto iter2(const char* txt, int first, int last) {
auto dif{ (first >= last ? first - last : last - first) + 1 };
while (dif--) std::cout << txt << "\n";
}
auto iter3(const char* txt, int max) {
while (max--) std::cout << txt << "\n";
}
auto iter4(const char* txt, int max) {
for(const auto i : std::vector<const char*>(max, txt)) std::cout << i << "\n";
}
auto iter5(const char* txt, int max) {
auto repeat{ [&txt, &max]() {std::ostringstream os;
std::fill_n(std::ostream_iterator<std::string>(os), max, txt);
return os.str(); } };
std::cout << repeat() << "\n";
}
int main() {
iter1("Hopp Schwiiz", 1, 10);
iter2("Hopp Schwiiz", -5, 4);
iter3("Hopp Schwiiz", 10);
iter4("Hopp Schwiiz", 10);
iter5("Hopp Schwiiz\n", 10);
}
Lösung von: Jens Kelm (@JKooP)
Aktionen
Neue Lösung hinzufügen
Bewertung
Durchschnittliche Bewertung:
Meta
Zeit: | 0.25 |
Schwierigkeit: | k.A. |
Webcode: | vrft-w7zr |
Autor: | Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch) |