How are control and iteration statements able to read from code outside the parameter list? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

How are control and iteration statements able to read from code outside the parameter list?

The control (if,etc) and iteration (while,etc) execute the data written exactly below them. Eg- while(1) { cout<<"1"; //Not in parenthesis, but the loop executes this... } So, is there a way, a user can create a function which runs data below it continously like a control or iteration statement? //Called via expression...(x<5,etc.) I don't want to use the execution statements inside parameters, i.e. want to make the call similar to a while loop, etc. Please tell me if anything is possible...

3rd Apr 2017, 12:39 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
2 Answers
+ 5
In 3, I actually wanted to say that I don't want something like this: #define while2(x,y){START:{if(!x)goto END; y; goto START;}}END: Here the call would be like: while2(1,{cout<<"10";}) //Execution Statements inside parameter parenthesis... While a true while loop will look like: while(1) { cout<<"10"; //Execution statements outside parameter parenthesis... }
5th Apr 2017, 1:25 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 3
1. How are control and iteration statements able to read from code outside the parameter list? Ans. The terminology used to describe the above scenario is 'Scope'. Specifically a control or iteration statement is able to read code 'data' from outside it's scope, if variables or constants declared above its scope is used inside its scope (or jurisdiction) unless another variable or constant within the functions scope properly hides it. 2. You said: " is there a way, a user can create a function which can run data below it like a control or iteration statement?" Ans. If you mean, a function that does not access any data from its callee; yes that's possible. If this is not the kind of answer you were expecting, kindly rephrase your question. (This is because when you call a function you really want to execute some of its functionality.) 3. You said: "I don't want to use the execution statements inside parameters." Ans. You need to rephrase your question. As far as my knowledge goes, a parameter is just a name given to value or reference that's passed on into a function. Did you mean, I don't want to use the execution statements inside functions? Yes possible, the short answer is; define carefully a macro. An example would be my code (the below links) 1 - https://code.sololearn.com/clW4V5TThei6/#cpp 2 - https://code.sololearn.com/chfu151I35Xy/#cpp You went on to say, this is possible if a person can define his own keywords, which is not possible. This is possible if you're good with compiler construction. new keyword means either: (1) you create a new programming language, or (2) You're extending an existing language (say like C++). I would like to point out that there do exist something called 'Domain Specific Language' or DSL's. In that, you get to define the rules of the world you're creating.
4th Apr 2017, 2:51 PM
Renjith M
Renjith M - avatar