control-structure

Defining variables in control structures

你说的曾经没有我的故事 提交于 2020-01-17 05:05:28
问题 According to the standard, what is the difference in behavior between declaring variables in control structures versus declaring variables elsewhere? I can't seem to find any mention of it. If what I'm referring to isn't clear, here's an example: if (std::shared_ptr<Object> obj = objWeakPtr.lock()) As you can see, I'm declaring and initializing a local variable, obj , in the if block. Also, is there any technical reason as to why this syntax isn't given any special behavior when used in place

Is it possible that a variable declared after the main has file scope?

半世苍凉 提交于 2019-12-29 08:46:29
问题 After running this code: #include <stdio.h> int x; int main(void) { printf("%d\n",x); return 0; } int x=5; I expected the output should be 0 . Because of the sequence control structure of the program int x; should be executed first and then 0 is printed and finally int x=5; should be executed. But it is giving the output 5 . How is the program accesses 5 for the x in printf ? 回答1: The first acts as a forward declaration, and the later acts as the actual definition. 回答2: Variable default

When, where & why should I use the “alternative syntax” for Control Structures?

随声附和 提交于 2019-12-22 04:37:16
问题 A simple PHP if / else condition looks like this: if(true) { echo("Hello World!"); } else { echo("Goodbye World!"); } The "alternative syntax" looks like this: if(true): echo("Hello World!"); else: echo("Goodbye World!"); endif; I have a few questions regarding this "alternative syntax" : When / in what case should I use it? Where / in which part of my script should I use it? Why should I use it / what purpose does it serve & are there any benefits to using it? Why was it created, does the

Program simple simulation in R

╄→гoц情女王★ 提交于 2019-12-13 06:22:51
问题 Editing this post for simplification according to @agstudy I am trying to develop a model that simulates a polymer using a random uniform distribution. The model has 2 states State 1 (probability of state 1 if in state 2 is .003): growth probability, A = .01 shrink probability, B = .0025 State 2 (probability of state 2 if in state 1 is .0003): growth probability, A = .01 shrink probability, E = .05 Simulation starts in State 1 While in State 1, sample random numbers from data.frame1, if # <

PHP bracket less IF condition not accepting more than one statement

浪尽此生 提交于 2019-12-12 17:20:17
问题 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 be something wrong with my IDE? Obviously the error is clear but I just want to make sure this is normal. If you have any other any links to other alternate syntax let me know please. Bellow is just something I pasted from a project am doing and

Ternary Operators(Java)

天涯浪子 提交于 2019-12-12 04:30:17
问题 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 for shortening code. So, this is a question that will help me understand the bounds of ternary operators and when/how they can be used. Is there a way to shorten the following code block using one long statements using a ternary operator? if(age <

Run A and then B or run C

落爺英雄遲暮 提交于 2019-12-11 12:11:38
问题 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 | +-------+-------+------------+ | false | false | C | | false | true | B | | true | false | A | | true | true | A + B | +-------+-------+------------+ The best thing I have so far is: if( A ) { // Run Block A } if( B ) { //Run Block B } else if( !A ) { //Run Block C } 回答1:

Strange PHP syntax

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 12:38:37
问题 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 anyone enlighten me? Thanks to all :) 回答1: That style of syntax is more commonly used when embedding in HTML, especially for template/display logic. When embedded this way, it's a little easier to read than the curly braces syntax. <div> <? if ($condition): ?> <ul>

Python - If not statement with 0.0

回眸只為那壹抹淺笑 提交于 2019-12-06 17:01:00
问题 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 been entered. It works fine, except when 0.0 is the keyword's value. I understand this is because 0 is one of the things that is considered 'not'. My code is probably too long to post, but this is an analogous (albeit simplified) example: def

ruby catch-throw and efficiency

安稳与你 提交于 2019-12-06 02:09:21
问题 catch in Ruby is meant to jump out of deeply nested code. In Java e.g. it is possible to achieve the same with Java's try-catch meant for handling exceptions, it is however considered poor solution and is also very inefficient. In Ruby for handling exceptions we have begin-raise-rescue and I assume it is also to expensive to use it for other tasks. Is Ruby's catch-throw really a more efficient solution then begin-raise-rescue or are there any other reasons to use it to break nested blocks