Concept explainers
Why should you indent the statements in the body of a loop?
Explanation of Solution
Reason for statement indention:
Some of the reasons for “indenting C++ statements in the body of a loop” are:
- The body of a loop contains C++ statements and it gets executed once the expression is valid. Otherwise it won’t execute.
- Multiple loop statements are enclosed by braces ({}) and should be intended, because, it separates the condition statements from surrounding code.
- Hence, it helps the programmer to easily identify the statements that are conditionally-executed.
Example:
Consider the following example program contains conditionally-executed statements:
Program code:
//Include required header files
#include <iostream>
#include<iomanip>
using namespace std;
//Main function
int main()
{
//declare the variable “n” as integer set “n” as 5
int n = 5;
//loop i values for 5 times
for (int i = 1;i <= n;i++)
{
//check the number is even
if (i % 2 == 0)
{
//if so, display it
cout << "\n"<<i<<" is even";
}
}
cout << endl;
return 0;
}
The highlighted statements are indented within the loop.
2 is even
4 is even
Want to see more full solutions like this?
Chapter 5 Solutions
Starting Out with C++ from Control Structures to Objects (9th Edition)
Additional Engineering Textbook Solutions
Starting Out with Java: Early Objects (6th Edition)
Absolute Java (6th Edition)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Software Engineering (10th Edition)
Starting Out with Python (4th Edition)
Java: An Introduction to Problem Solving and Programming (7th Edition)
- We use For loop instead of a while loop when we know exactly how many times you want to loop through a block of code. True Falsearrow_forwardhow does a programmer decide to use while loop versus a for loop?arrow_forwardTrue or False It is possible that the body of a while loop might not execute at all.arrow_forward
- Is it true or false? When a variable is declared in a for loop's initialization expression, its scope is restricted to the loop.arrow_forwardWhat will this For loop will print on the screen? FOR x = 1 TO 5 Print x * x Next xarrow_forwardWhat is the difference between For Loop, Do While Loop, Do Until Looparrow_forward
- In C++ What type of loop requires the user to manually terminate the loop? a. counter-controlled while loop b. sentinel-controlled while loop c. eof-controlled while loop d. counter-controlled for looparrow_forwardBecause it is repeated so many times in the code, the statement or block of an endless loop is known as an infinite loop's block.arrow_forwardYou need to write a loop that will repeat exactly 125 times. Which is the preferred loop construct to use? A. while B. do while loop C. for looparrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT