“Perl pour la boucle” Réponses codées

boucles de perl

# The main loops in Perl are for, foreach, and while, though there are also
# do..while and until loops. Here's the syntax for each, with examples:
# Basic syntax of for loop:
for ( init; condition; increment ) {
    code to run;
}
# Example usage of for loop:
for (my $i = 5; $i < 10; $i += 1) {  
    print "Value of i: $i\n";
}

# Basic syntax of foreach loop:
foreach var (list) {
	code to run;
}
# Example usage of foreach loop:
@list = (2, 20, 30, 40, 50);
foreach $i (@list) {
    print "Value of i: $i\n";
}

# Basic syntax of while loop:
while( condition ) {
   code to run;
}
# Example usage of while loop:
my $i = 5;
while( $i < 10 ) {
   print "Value of i: $i\n";
   $i += 1;
}

# Basic syntax of do..while loop:
# Similar to while loop but always executes at least once
do {
   code to run;
} while( condition );
# Example usage of do..while loop:
$i = 5;
do {
   print "Value of i: $i\n";
   $i += 1;
} while( $i < 10 );

# Basic syntax of until loop:
# Sort of the opposite of a while loop, it loops over the code until the
# condition becomes true
until( condition ) {
   code to run;
}
# Example usage of until loop:
$i = 5;
until( $i > 10 ) {
   print "Value of i: $i\n";
   $i += 1;
}
Charles-Alexandre Roy

Perl pour la boucle

# Basic syntax:
for ( init; condition; increment ) {
    code to run;
}

# Example usage:
for (my $i = 5; $i < 10; $i += 1) {  
    print "Value of i: $i\n";
}
Charles-Alexandre Roy

Réponses similaires à “Perl pour la boucle”

Questions similaires à “Perl pour la boucle”

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code