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
3
votes
5 answers

Advanced constructs in Javascript

I found one interesting project on github which deals with pdf rendering in the browser. I tried to read the code because I'm interested in this topic but I realized that my javascript knowledge is poor (insuficient). There are constructs like: var…
xralf
  • 11,703
  • 34
  • 110
  • 167
3
votes
7 answers

break in for loop

Assume you have this code: function doSomething($array) { for($i = 0; $i < sizeof($array); $i++) { if ($array[$i] == "ok") return true; } return false; } Note that I'm not talking about PHP specific (this applies to all…
Bv202
  • 3,624
  • 11
  • 41
  • 76
3
votes
4 answers

Custom control structures in Scala?

There are a number of times I've run into a simple pattern when programming in Java or C++ for which a custom control structure could reduce the boilerplate within my code. It goes something like: if( Predicate ){ Action return Value …
wheaties
  • 34,173
  • 12
  • 82
  • 126
3
votes
4 answers

How to transfer the control of goto statement in C#

I'm beginner in programming and I'm trying this simple program of getting user name and sorting it and so on. class Program { static void Main(string[] args) { int number; dynamic y; string[] answer = new…
vikramnr
  • 83
  • 2
  • 11
3
votes
3 answers

PHP bracket less IF condition not accepting more than one statement

I've never been a fan of brackets in control structures and only today I realised how it only accepts one statement within a bracket less if condition, if I have more than one statement it will throw a syntax error. Is this how PHP works or can it…
André Ferraz
  • 1,473
  • 11
  • 24
3
votes
4 answers

Is it ever okay to not use an ELSE statement if you have a return or throw inside the IF statement?

I often write code such as the following bool myFunct (...) { if (something) { return false; } // .... more code .... } The alternative is bool myFunct (...) { if (something) { return…
Lily Carter
  • 163
  • 1
  • 1
  • 5
3
votes
3 answers

Can I iterate through a for loop randomly instead of sequentially?

If there is a for loop like for ( int i = 0; i <= 10; i++ ) { //block of code } What I want to achieve is, after first iteration i value need not to be 1, it can be anything from 1 to 10, i should not be 0 again and similarly for other…
Code Enthusiastic
  • 2,687
  • 5
  • 21
  • 36
2
votes
2 answers

How to convert do while loop to while loop in C++?

I have written a C++ program to let the users entering positive numbers using a do while loop. Notwithstanding, when I try to convert the do while loop into a while loop, the expected output is not the same as do while loop. The code is as…
Section 28
  • 39
  • 6
2
votes
1 answer

asymptotic bounding and control structures

So far in my learning of algorithms, I have assumed that asymptotic boundings are directly related to patterns in control structures. So if we have n^2 time complexity, I was thinking that this automatically means that I have to use nested loops.…
Miroslav Trninic
  • 2,986
  • 4
  • 26
  • 48
2
votes
2 answers

Display the sum of all raised odd and even number using loop and control structures

I need to create a java program that will accepts 10 integers and if the entered number is odd raise it to ^ 1 power and if the user input another odd number raised it again to the next power ^ 2, same with Even numbers however the power starts at…
X4c3
  • 109
  • 1
  • 11
2
votes
0 answers

Difference between grouping parentheses and `begin` ... `end`

What is the difference between parentheses used for grouping: (some_method_chain; another_method_chain) 2 * (3 + 4) # => 14 and the begin ... end structure: begin some_method_chain; another_method_chain end 2 * begin 3 + 4 end # => 14 I notice…
sawa
  • 156,411
  • 36
  • 254
  • 350
2
votes
4 answers

Translate flowchart to c++ code

I have a problem translating this flowchart below to C++ code. It should probably look somewhat like this (I know it's wrong for now): do { I1; if(!W1) { I2; ... } } I1, I2, I3 are instructions. I think I should use boolean variables…
Euro Pe
  • 73
  • 1
  • 7
2
votes
2 answers

try/catch vs. if/then/else - Specific Case

Before everyone espouses code correctness, I realize that the generally correct way to do things is to not use try/catch for control flow. However, this is a bit of an edge case, and I'd like some input on what others would do in this…
2
votes
2 answers

Count Control structures

I want to count the used control structures (IF - statmens, switch case,..) in a simple Java class and simply save the amount in a variable. Do you guys have an idea how I can do that?
Ricky77719
  • 43
  • 1
  • 5
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
1 2
3
8 9