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
1
vote
1 answer

Need to create a matrix with a specific selection of elements

I'm being given the following information: where "routenummer" stands for roadnumber, "gemeente" for city, "afstand" for distance in km and "moeilijkheidsgraad" for difficulty. I need to create a matrix titled difficultRoads with all of the given…
Nicolas
  • 89
  • 1
  • 8
1
vote
1 answer

convert sas if-else statement to R

I have a SAS code similar to the below logic. I would like to convert it to R script using if-else or ifelse method. Here is the SAS code example: if cat1=1 then do; if cat2 eq 'U' then do; if var1 =. then var1 = var3; end; else if cat2='A'…
R. zqkal
  • 57
  • 4
1
vote
1 answer

Does Using goto to Escape Control Structures Ever Produce Different Assembly?

There is a lot of debate about the goto command, this question is not about the rightness or wrongness of its use, but more simply a question of whether it ever actually creates different assembly. I'm specifically looking at Visual Studio 2013, but…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
1
vote
1 answer

Testing for null object using short-circuit logical operator

I'm reading through the Mozilla Developer Network's page on Javascript, and am confused by a line I see. Here's the description and line in question: The && and || operators use short-circuit logic, which means whether they will execute their…
1
vote
4 answers

Why is an empty value in a shorthand if-then-else returning `true`?

This is not exactly a "problem", but more a "why" question. Based on the following example: echo 'test' . ( true ? : 'some-test' ); Why is the result of this: test1 instead of what one might expect: test. Or in other words: Why is an empty return…
Damien Overeem
  • 4,487
  • 4
  • 36
  • 55
1
vote
10 answers

Correct order for control structure logic (true/false, false/true)?

I am new to programming, and am wondering if there is a correct way to order your control structure logic. It seems more natural to check for the most likely case first, but I have the feeling that some control structures won't work unless they…
meleyal
  • 32,252
  • 24
  • 73
  • 79
1
vote
4 answers

Run A and then B or run C

I'm looking for a control structure to deal with potentially multiple blocks of code or a NONE block. The most basic case is when there are only two conditions, A and B: +-------+-------+------------+ | A | B | Blocks Run…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
1
vote
1 answer

Is a Self-Modifying Function Bad?

If I want a function to do something different the first time it's run, I could check each time it's run to determine whether it's the first time, or I could change the function; something like this: foo=function(a,b){ ...do something1... …
user1277170
  • 3,127
  • 3
  • 19
  • 19
1
vote
1 answer

Check multiple exceptions in Scala retry control structure

I'm trying to develop a control structure that retries on some declared exceptions but throw others. The control structure works nicely but I have problem checking if the exception caught belongs to the types of declared exceptions. In more generic…
sabz
  • 21
  • 2
1
vote
4 answers

Error handling without pattern matching in Haskell

I'm trying to write a program that takes two integers on the command line and does something interesting with them. I would like write the reading/parsing of the integers as easily and imperatively as possible since it should be relatively simple…
illabout
  • 3,517
  • 1
  • 18
  • 39
1
vote
4 answers

In PHP: if a matching case is found, are any of the remaining case statements evaluated?

I have a switch statement in a tight loop that looks like this: switch(true) { case /*expensive comparison */: case /*another expensive comparison */: case /*different expensive comparison */: return true; } return false; I…
Bryan Agee
  • 4,924
  • 3
  • 26
  • 42
1
vote
2 answers

What syntax is for (var i = 0, item; item = a[i++];)

In the Re-Introduction to Javascript, the syntax for (var i = 0, item; item = a[i++];) is explained as the middle "item" being a conditional test for truthtiness/falsiness. However, I had assumed the syntax to be (start; condition test; control…
thetrystero
  • 5,622
  • 5
  • 23
  • 34
1
vote
2 answers

Is there a language with higher order conditionals?

Sometimes, I have a control structure (if, for, ...), and depending on a condition I either want to use the control structure, or only execute the body. As a simple example, I can do the following in C, but it's pretty ugly: #ifdef APPLY_FILTER if…
jdm
  • 9,470
  • 12
  • 58
  • 110
1
vote
2 answers

Setting switch() to not break after a matched case and instead continuing on to all matched cases

I'm really just curious about this, and I don't plan on implementing it, but I do think it would be a cool control structure to use should the appropriate conditions arise. I have an array of booleans that represent which types of data a user is…
Kavi Siegel
  • 2,964
  • 2
  • 24
  • 33
1
vote
1 answer

default argument value for break

Can anybody tell whats the default argument value for break in control structures instead of break 1 , break 2 ,.. Example :- for($i=0;$i<10;$i++){ for($j=0;$j<$i;$j++){ if($j == 4){ break; }else{ echo…
Vinu
  • 167
  • 4
  • 14