answersLogoWhite

0


Best Answer

Both.

for (int x=0; x<10; ++x) { // outer loop

for (int y=0; y<10; ++y) { // nested loop

std::cout << x*y << '\t';

} // end nested loop

std::cout << std::endl;

} // end outer loop

std::cout << std::endl;

void f(bool x, bool y) {

if (x==y) {

if (x) {

std::cout << "x and y are both true\n";

} else {

std::cout << "x and y are both false\n";

}

} else {

if (x) {

std::cout << "Only x is true\n"

} else {

std::cout << "Only y is true\n"

}

}

}

Note that the nested if within the else clause of the outer if can also be written as an else if statement:

void f(bool x, bool y) {

if (x==y) {

if (x) {

std::cout << "x and y are both true\n";

} else {

std::cout << "x and y are both false\n";

}

} else if (x) {

std::cout << "Only x is true\n"

} else {

std::cout << "Only y is true\n"

}

}

User Avatar

Wiki User

8y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: When writing code what can be nested for statement or if statement or both?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What does it mean to evaluate cobol?

The word evaluate in the programming language COBOL can be used to replace a nested if statement. Instead of long statement evaluate allows one to shorten the coding required and write cleaner code.


Can a try block have nested try blocks?

Yes, a try-catch block can be nested. It is placed around the code that might generate an exception.


Why should you indent the statement in the body of a loop?

In some programming languages like Python, the indentation of the text indicates how nested it is and is required for a loop to function properly. However, for most languages, indenting the body of the loop is simply for style and readability.


How can print both statement of if and else?

Simply print the whole source-code.


What is meant by the phrase one block of code can be nested in another?

It simply means one code block contains another code block. How you achieve this depends on the language, but in many languages we use opening and closing braces to define a code block. function foo () { // start of function block { // start of nested block // ... } // end of nested block } // end of function block Nested code blocks are typically used to define a new scope within an existing scope. Any variables declared within the nested block are local to the scope of that block and will fall from scope when we exit the nested block. This can be useful when we want to instantiate new variables within a larger scope but want to localise them to the code block that actually uses them. In some languages (such as C++) this also allows us to redefine names that were initially defined by the enclosing scope, temporarily hiding the original names from within the nested scope (only the local names are visible). When we exit the nested block, the original names become visible again. However, referring to two separate variables by the same name within two scopes of the same function can make code difficult to read, so this is best avoided.

Related questions

Would you be correct in saying that in an nested if statement it would read ----if and it is and it is then do this--- else do this?

What you are asking would be not be a nested if then else statement, in pseudocode what you are asking would be:if condition thendo thiselsedo that[this is pseudo code because the 'and' would be rendered differently in other languages and there potentially would be statement terminators, etc]A nested if statement would be:if condition1 thenif condition2 thendo thiselsedo thiselsedo thatThe second if statement is nested within the first one, clearly the nesting can go on quite deeply.


What does it mean to evaluate cobol?

The word evaluate in the programming language COBOL can be used to replace a nested if statement. Instead of long statement evaluate allows one to shorten the coding required and write cleaner code.


Can a try block have nested try blocks?

Yes, a try-catch block can be nested. It is placed around the code that might generate an exception.


How is it that writing code for computer programming is similar to writing an essay?

Both have different types of audiences.


Why should you indent the statement in the body of a loop?

In some programming languages like Python, the indentation of the text indicates how nested it is and is required for a loop to function properly. However, for most languages, indenting the body of the loop is simply for style and readability.


How can print both statement of if and else?

Simply print the whole source-code.


What is meant by the phrase one block of code can be nested in another?

It simply means one code block contains another code block. How you achieve this depends on the language, but in many languages we use opening and closing braces to define a code block. function foo () { // start of function block { // start of nested block // ... } // end of nested block } // end of function block Nested code blocks are typically used to define a new scope within an existing scope. Any variables declared within the nested block are local to the scope of that block and will fall from scope when we exit the nested block. This can be useful when we want to instantiate new variables within a larger scope but want to localise them to the code block that actually uses them. In some languages (such as C++) this also allows us to redefine names that were initially defined by the enclosing scope, temporarily hiding the original names from within the nested scope (only the local names are visible). When we exit the nested block, the original names become visible again. However, referring to two separate variables by the same name within two scopes of the same function can make code difficult to read, so this is best avoided.


What is a nested indent?

A nested indent refers to an indentation within another indentation in a document or piece of code. It is commonly used to show hierarchical relationships between different elements or sections. Nested indents are often seen in programming languages like Python, where they help clarify the structure of the code.


What is the syntax of a conditional statement in JavaScript?

In JavaScript we have the following conditional statements:if statement - you would use this statement to execute some code only if a specified condition is trueif...else statement - you would use this statement to execute some code if the condition is true and another code if the condition is falseif...else if....else statement - you would use this statement to select one of many blocks of code to be executedswitch statement - you would use this statement to select one of many blocks of code to be executedFor example: If StatementUse the if statement to execute some code only if a specified condition is true. Syntaxif (condition) {code to be executed if condition is true}If...else StatementUse the if....else statement to execute some code if a condition is true and another code if the condition is not true. Syntaxif (condition) {code to be executed if condition is true}If...else if...else StatementUse the if....else if...else statement to select one of several blocks of code to be executed. Syntaxif (condition1) {code to be executed if condition1 is true}else if (condition2){code to be executed if condition2 is true}else{code to be executed if condition1 and condition2 are not true}else{code to be executed if condition is not true}


What is an if or nested if statement?

Convenience here is in terms of the human reader of the program; a good optimizing compiler may treat both the switch and the nested if-else as pretty much the same thing. For me, using a switch is easier because it is easier to add additional cases to the switch. It can be (in my opinion) harder and more dangerous logically to fit additional test cases in a nested if. Finally, in some compilers the switch statement may be implemented in the hardware, so you end up with better performance.


What is writing Code?

Writing code is the process of implementing an algorithm in a specific programming language.


What are the difference's between a code of ethics and a ethics statement?

Very little really. In essence they are both the same - a non-binding statement of the ethics and performnace to which the organization aspires to hold itself. It is not legally binding.