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
2
votes
1 answer

control structures - common applications

What are the most common applications of each control structure. I am trying to get at a reference along the lines of: Control Structure - common application Conditions - true / false distinction Selections - case differentiation…
Sebas
  • 411
  • 1
  • 3
  • 10
2
votes
4 answers

Which is faster: "null == myObject" or "myObject == null"?

In both Java and .Net, I've heard that using null first if (null == myObject) is more performant than using the object first if (myObject == null). While I think this is probably true, I'm not certain and would like to know from SO users.…
johntrepreneur
  • 4,514
  • 6
  • 39
  • 52
2
votes
1 answer

PHP Control Structure :Declare()

I'm having a hard time understanding the PHP control structure declare() and where/how it would be used. http://us.php.net/manual/en/control-structures.declare.php I was hoping someone could explain this to me. Thank you in advance.
Scott
  • 11,046
  • 10
  • 51
  • 83
2
votes
2 answers

Does python have a better control structure or other feature to simplify the code?

I'm new to Python and trying to write a validate function in the simple chest game to achieve: the a piece should move it's full extend (which mean in it's moving direction like B7:E4, have a another piece F4 blocked, which is a legal move…
mko
  • 21,334
  • 49
  • 130
  • 191
2
votes
5 answers

Multiple foreach vs multiple if inside foreach

Which would be more optimal of the two? I can't test it on my computer, I can't rely on it. foreach($links as $link){ if($a){ //do something } if($b){ //do something } if($c){ //do something } …
Jürgen Paul
  • 14,299
  • 26
  • 93
  • 133
1
vote
1 answer

control structure + Python

the following is an exercise on "Control Structures". I am sure it can be done in many different ways and that my (incomplete) solution is far from ELEGANT. At the moment I can include all the "cases" except when a string includes one or more…
1
vote
5 answers

How can I continue a JavaScript if.. else if statement until I receive valid input?

How can I continue prompting a user for a valid response using if... else if statements? My script currently works once, but then breaks: var enterNum = prompt("Please enter a number between 1 and 100", ""); if (isNaN(enterNum)){ enterNum =…
Annie
  • 51
  • 2
  • 4
1
vote
1 answer

The correct ways to use rand() in selection structure

Can anyone identify why the line : if(code == rand() && attempt != 3) not executed even I'm entered a correct code that generated by the rand(). #include #include #include int main(void) { int code, attempt = 0; …
1
vote
3 answers

Python; If statements, For Loops, File Reading

To be clear, I am not asking anyone to do this for me. I am simply asking a question seeking guidance so I can continue working on this. We are given a file that gives various weights of packages; 11 25 12 82 20 25 32 35 40 28 50 51 18 48 90 I have…
1
vote
1 answer

How can I calculate the number of objects that heap memory will hold?

I want to code/design an algorithm in java in which i started with an input of variable int i=0; while (i==0){i==1;} create or start again with var i in new memory location set value again to 1; loop to assign value 1 to var and jump to next…
user9678759
1
vote
1 answer

Modifying variable within conditional expression in Python

In Java, it is possible to write code like this: int number = 1; while((number++)<10){ System.out.println(number); } I tried doing the same in Python, but got a syntax error. Is there any similar feature in Python where the value of a variable…
Daniel
  • 1,599
  • 1
  • 16
  • 19
1
vote
5 answers

Break the loop of an Array looping function (map, forEach, etc.)

How can I break (similar to the break statement) from an implicit loop on an array? The Array.prototype.map, Array.prototype.forEach, etc. functions imply a loop over the elements of the array. I want to conditionally break that loop early. This…
bignose
  • 30,281
  • 14
  • 77
  • 110
1
vote
2 answers

Eclipse autocomplete parentheses in control structures

I have a quick question that might save me a few seconds of annoyance every day. I know that eclipse can do a lot of autocomplete magic, so this might be possible: As a programmer who learned with python, I constantly forget to surround contitions…
Julius Naeumann
  • 422
  • 1
  • 10
  • 19
1
vote
1 answer

tail call memory managment in haskell

I'm using the following control structure(which I think is tail recursive) untilSuccessOrBigError :: (Eq e) => (Integer -> (Either e a)) -> Integer -> e -> (Either e a) untilSuccessOrBigError f count bigError = case f count of Right x ->…
1
vote
2 answers

Simplifying a nested If...Else statement; flexible array

So first off, here is the code: $greens = $_REQUEST['greens']; $squash = $_REQUEST['squash']; $tomatoes = $_REQUEST['tomatoes']; $single = $greens xor $squash xor $tomatoes; if (isset($greens,$squash,$tomatoes)) { $array =…