Questions tagged [control-structure]

Within an imperative programming language, a control flow statement is a statement whose execution results in a choice being made as to which of two or more paths should be followed.

Within an imperative programming language, a control flow statement is a statement whose execution results in a choice being made as to which of two or more paths should be followed.

The kinds of control flow statements supported by different languages vary, but can be categorized by their effect:

  • continuation at a different statement (unconditional branch or jump),
  • executing a set of statements only if some condition is met (choice - i.e., conditional branch),
  • executing a set of statements zero or more times, until some condition is met (i.e., loop the same as conditional branch),
  • executing a set of distant statements, after which the flow of control usually returns (subroutines, coroutines, and continuations),
  • stopping the program, preventing any further execution (unconditional halt).

from Wikipedia

138 questions
9
votes
18 answers

Is there a programming language with no controls structures or operators?

Like Smalltalk or Lisp? EDIT Where control structures are like: Java Python if( condition ) { if cond: doSomething doSomething } Or …
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
7
votes
4 answers

Control structures beyond standard conditionals and loops?

Structured programming languages typically have a few control structures, like while, if, for, do, switch, break, and continue that are used to express high-level structures in source code. However, there are many other control structures that have…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
7
votes
2 answers

Disposal Order in C# Using Blocks

I'm really bothered by having to nest using blocks in C#. It's not elegant and it takes up a lot of space. In some cases it appears to be unavoidable because I need to declare variables of different data types, but it seems like the single-type case…
Philip Hanson
  • 1,847
  • 2
  • 15
  • 24
7
votes
5 answers

Do if(){ } while() statement

I am currently working on somebody's else code, with a statement like this if(x.start()) do if(y.foo(x)) { // Do things }while(x.inc()) here x is custom class that holds information on y and allows iteration through its element in a special…
Three Diag
  • 585
  • 2
  • 7
  • 21
7
votes
2 answers

ruby catch-throw and efficiency

catch in Ruby is meant to jump out of deeply nested code. In Java e.g. it is possible to achieve the same with Java's try-catch meant for handling exceptions, it is however considered poor solution and is also very inefficient. In Ruby for handling…
wrzasa
  • 1,113
  • 10
  • 20
6
votes
2 answers

Lookup table in Latex

I have a bunch of automatically generated LaTeX code with hypertargets of the form "functionname_2093840289fad1337", i.e the name of a function with a hash appended. I would like to refer to those functions from the rest of the document by only…
Laserallan
  • 11,072
  • 10
  • 46
  • 67
6
votes
2 answers

Prolog if-then-else constructs: -> vs *-> vs. if_/3

As noted in another StackOverflow answer that I can't seem to find anymore, this pattern emerges frequently in practical Prolog code: pred(X) :- guard(X), ... pred(X) :- \+ guard(X), ... and many people try to condense this…
6
votes
4 answers

Scala: custom control structures with several code blocks

Is it possible to create a custom control structure with several code blocks, in the fashion of before { block1 } then { block2 } finally { block3 }? The question is about the sugar part only - I know the functionality can be easily achieved by…
Vilius Normantas
  • 3,708
  • 6
  • 25
  • 38
6
votes
10 answers

Can the for loop be eliminated from this piece of PHP code?

I have a range of whole numbers that might or might not have some numbers missing. Is it possible to find the smallest missing number without using a loop structure? If there are no missing numbers, the function should return the maximum value of…
Ben Harold
  • 6,242
  • 6
  • 47
  • 71
6
votes
3 answers

Is break; required after die() php

I've been thinking to much... In the area of switch case is break; required after die() Example: switch($i){ case 0: die('Case without break;'); case 1: die('Case with break;'); break; }
Evan Stoddard
  • 718
  • 1
  • 7
  • 21
5
votes
3 answers

Smalltalk Variadic functions

Does Smalltalk(especially Squeak/Pharo) have some form of variadic functions? I was just reading about the power of designing your own control statments in smalltalk and while I'm a big fan of ifTrue: ifFalse: I was having a hard time coming up with…
4
votes
2 answers

Can you loop a Perl 6 block that's in a variable?

I keep wanting to do something like this: my $block := { state $n = 0; say $n++; last if $n > 3; }; loop $block; Or even: $block.loop; I'm not expecting that this is possible but it would sure be cool if it was. How would I find…
brian d foy
  • 129,424
  • 31
  • 207
  • 592
4
votes
3 answers

Is it possible that a variable declared after the main has file scope?

After running this code: #include int x; int main(void) { printf("%d\n",x); return 0; } int x=5; I expected the output should be 0. Because of the sequence control structure of the program int x; should be executed first and…
haccks
  • 104,019
  • 25
  • 176
  • 264
4
votes
3 answers

How to implement redo statement (as in Perl and Ruby) in Lisp

Code that requires break statements or continue statements in other languages can be done with block & return-from or catch & throw in Common Lisp and Emacs Lisp. Then there is code that requires redo statements, or at least best written with redo.…
Jisang Yoo
  • 3,670
  • 20
  • 31
4
votes
5 answers

c# Nested dictionary of different depths

Essentially what I need is the mapping of different int variables to a dictionary. Or at least that's the way that I was thinking of doing it. The easiest way that I can think to do it slash explain what I want is using switch statements. string s =…
DanielCardin
  • 545
  • 2
  • 8
  • 17
1
2
3
9 10