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

132 questions
43
votes
28 answers

Useful alternative control structures?

Sometimes when I am programming, I find that some particular control structure would be very useful to me, but is not directly available in my programming language. I think my most common desire is something like a "split while" (I have no idea…
A. Rex
  • 30,789
  • 21
  • 85
  • 95
38
votes
5 answers

Is there a way to write these ifs nicer?

I need to write these four ifs in Python. Notice what it does, is changing between four possible states in a loop: 1,0 -> 0,1 -> -1,0 -> 0,-1 and back to first. if [dx, dy] == [1,0]: dx, dy = 0, 1 if [dx, dy] == 0, 1: dx, dy = -1, 0 if [dx,…
manuel
  • 827
  • 6
  • 17
37
votes
8 answers

PHP "or" Syntax

I've seen this a lot: $fp = fopen($filepath, "w") or die(); But I can't seem to find any real documentation on this "or" syntax. It's obvious enough what it does, but can I use it anywhere? And must it be followed by die()? Are there any caveats to…
Andrew
  • 2,838
  • 2
  • 28
  • 35
25
votes
3 answers

Understanding PHP declare() and ticks

Today I was looking through the php manual and stumbled upon a control structure declare. The declare construct is used to set execution directives for a block of code This is what declare is supposed to do. To be honest I didn't understood…
Nandakumar V
  • 3,563
  • 2
  • 22
  • 44
24
votes
1 answer

What is the meaning of "break 2"?

I always used and seen examples with just "break". What is the meaning of this:
funguy
  • 1,914
  • 7
  • 24
  • 38
22
votes
6 answers

for loop vs while loop vs foreach loop PHP

1st off I'm new to PHP. I have been using for loop,while loop,foreach loop in scripts. I wonder which one is better for performance? what's the criteria to select a loop? which should be used when we loop inside another loop? the code which I'm…
Techie
  • 42,101
  • 38
  • 144
  • 232
20
votes
6 answers

Python - If not statement with 0.0

I have a question regarding if not statement in Python 2.7. I have written some code and used if not statements. In one part of the code I wrote, I refer to a function which includes an if not statement to determine whether an optional keyword has…
TheBoro
  • 223
  • 2
  • 10
20
votes
13 answers

Does the last element in a loop deserve a separate treatment?

When reviewing, I sometimes encounter this kind of loop: i = begin while ( i != end ) { // ... do stuff if ( i == end-1 (the one-but-last element) ) { ... do other stuff } increment i } Then I ask the question: would you write…
xtofl
  • 38,207
  • 10
  • 95
  • 177
17
votes
6 answers

How to implement decision matrix in c#

I need to make a decision based on a rather large set of 8 co-dependent conditions. | A | B | C | D | E | F | G | H -----------+---+---+---+---+---+---+---+--- Decision01 | 0 | 1 | - | 1 | 0 | 1 | - | 1 Decision02 | 1 | 0 | - | 0 | 0 | -…
Aether McLoud
  • 702
  • 7
  • 20
10
votes
5 answers

Strange PHP syntax

I've been working on PHP for some time but today when I saw this it came as new to me: if(preg_match('/foo.*bar/','foo is a bar')): echo 'success '; echo 'foo comes before bar'; endif; To my surprise it also runs without error. Can…
Joseph
  • 241
  • 2
  • 9
10
votes
3 answers

How to implement early exit / return in Haskell?

I am porting a Java application to Haskell. The main method of the Java application follows the pattern: public static void main(String [] args) { if (args.length == 0) { System.out.println("Invalid number of arguments."); …
Giorgio
  • 4,627
  • 6
  • 36
  • 65
9
votes
2 answers

SQL loop WHILE IF BREAK

In a SQL Server 2012 stored procedure, I have several nested structures. I want to break out of a single layer of them. I thought the description of BREAK in the msdn https://msdn.microsoft.com/en-CA/library/ms181271.aspx was on my side. But I'm…
Hsmith
  • 155
  • 1
  • 1
  • 9
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
  • 184,433
  • 106
  • 369
  • 548
9
votes
1 answer

OCaml is it possible to creat single if (without else)

is it possible to create single if (without else) ? It is very usable to can use one if
user2874757
  • 129
  • 1
  • 3
7
votes
3 answers

'break' from a switch, then 'continue' in a loop

Is it possible to break from a switch and then continue in a loop? For example: $numbers= array(1,2,3,4,5,6,7,8,9,0); $letters = array('a', 'b', 'c', 'd', 'e', 'f', 'g'); foreach($letters as $letter) { foreach($numbers as $number) { …
Drew
  • 1,506
  • 4
  • 22
  • 44
1
2 3
8 9