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

What is important or implication of using or not using braces in PHP?

I am just wondering if there are reasons or an advantage of writing PHP snippet with or without using curly braces. I have these three approach of getting ordinal suffix of numbers. What should be the best approach in this case Approach 1: Without…
ShapCyber
  • 3,382
  • 2
  • 21
  • 27
0
votes
1 answer

How to convert list.files() output to an int?

The output when checking an empty directory with R's list.files() is a character string... > list.files('/home/someDir') character(0) But I would like to test its length and use it in a control structure like > current <- '/home/someDir' > dirPick…
d8aninja
  • 3,233
  • 4
  • 36
  • 60
0
votes
1 answer

PHP Auto Increment a (float) column from another column data

My program starts like this: START | B | S | P | 4000 | 215.05 | 4182.72 | 182.72 | where: B = Buy S = Sell and P = Profit. I'm calculating the values this way: bv = 18.60; //fixed value sv = 19.45; //fixed value START =…
0
votes
2 answers

Checking for no input in control structure

I am writing this basic control structure for a lesson and I am getting some unexpected behavior. var answer = prompt('what is your age'); if (answer >= 21) { alert('good to go!'); } else if (answer < 21) { alert('sorry not old…
MadCatm2
  • 951
  • 4
  • 24
  • 41
0
votes
3 answers

Ternary Operators(Java)

I was recently introduced to ternary operators. I managed to make it through a year and a half of CS school without a professor ever mentioning ternary operators. This is my first quarter where my professor is using them regularly. They seem great…
0
votes
1 answer

Gotolike structure / improved continue - is goto always evil?

I just stumbled over a problem while programming which would really benefit from a goto like structure. Consider this example: //tries to find a solution for a problem 3 times //in this process copies elements from old to newList foreach (var tryNbr…
Xlaech
  • 456
  • 3
  • 19
0
votes
2 answers

Can the internal counter of a Scratch Repeat block be accessed?

I assume that the Repeat() block in Scratch has some kind of internal counter that increments or decrements at the beginning or end of each time around the block. Can the value of this counter be accessed in any way? I realize this can be done to…
Adám
  • 6,573
  • 20
  • 37
0
votes
2 answers

Control Structure in Python

I am wondering why this piece of code: wordlist = ['cat','dog','rabbit'] letterlist=[] for aword in wordlist: for aletter in aword: if aletter not in letterlist: letterlist.append(aletter) print(letterlist) prints ['c', 'a',…
faceless
  • 436
  • 1
  • 4
  • 11
0
votes
1 answer

Does real programming follow the same basic control structures?

I'm still a beginner and learning. In tutorials there's usually examples like: using "If...else" to check if the login password is correct, else show "incorrect password" or anything like that. My question is: do programmers really use such basic…
Aria
  • 11
  • 2
0
votes
1 answer

Pause a while loop until a confirm button is pressed in NetBeans

I'm trying to create a frame in Java using NetBeans. It will display some data and at the same time get data from the user in a while loop. Can I pause the while loop until a confirm button has been pressed? I found that someone may suggest sleep,…
0
votes
3 answers

Is there a way to create a "custom" loop or control structure in PHP?

I'm creating a very large page that at various parts needs to loop through a data array like this: $processed = array(); foreach( $data as $program ) { if( $program['f_enabled'] == 'N' ) continue; $pid = $program['f_programId']; if(…
Andrew Cheong
  • 29,362
  • 15
  • 90
  • 145
0
votes
2 answers

Is There a Range Based case-Like Control Structure

Say at run-time I establish some events with occurrence times. Now I have certain entities in the system and I need to establish which entities were impacted by these events. So as an example say that I have: Entity1 intialized at time 1 EventRed…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
0
votes
1 answer

Mini-game. Ruby the hard way exercise 35

I'm currently learning ruby from the Learn Ruby the hard way tutorial. And in that exercise, the author ask us to add things to a simple game. However, I was trying this to improve the bear_room method by doing something like this: while true print…
0
votes
5 answers

Simple rock, paper, scissors game

I wrote a "Rock, paper, scissors" game: puts "Hello, this is a rock, papers, scissors game. Let's play." puts "Player 1, plase enter your choice: \n" puts "r for rock. \np for paper. \ns for scissors." p1 = gets.chomp.downcase puts "Player 2,…
0
votes
1 answer

Drawing a Vertical Pyramid in C

I have to get the desired output as follows: 1 2 6 3 7 10 4 8 11 13 5 9 12 14 15 But I can't seem to figure out how to do it. All I get is: 1 2 6 3 7 6 4 8 7 6 5 9 8 7 6 Here is my code: #include int main() { int n,i,j; …