C++ for Engineers and Scientists
C++ for Engineers and Scientists
4th Edition
ISBN: 9781133187844
Author: Bronson, Gary J.
Publisher: Course Technology Ptr
expand_more
expand_more
format_list_bulleted
Bartleby Related Questions Icon
Related questions
Question
Jb tujhe rok rha hu question nhi kro kyu kr rhe ho sab pe deslike dunga id band ho jayegi.
computer science.
Transcribed Image Text:6.1 INTRODUCTION
In Figure 6.1, we see a bus carrying the children to
school. There is only one way to reach the school. The
driver has no choice, but to follow the road one milestone
after another to reach the school. We learnt in Chapter
5 that this is the concept of sequence, where Python
executes one statement after another from beginning to
the end of the program. These are the kind of programs
we have been writing till now.
SCHOOL BUS
"Don't you hate code that's
not properly indented?
Making it [indenting] part of
the syntax guarantees that all
publish
van Rossum
to
School
Figure 6.1: Bus carrying students to school
Let us consider a program 6-1 that executes in
sequence, that is, statements are executed in an order
in which they are written.
The order of execution of the statements in a program
is known as flow of control. The flow of control can be
implemented using control structures. Python supports
two types of control structures-selection and repetition.
Output:
Enter value 1: 84
Enter value 2: 4
In this chapter
» Introduction to Flow
of Control
«
Selection
»
Indentation
» Repetition
»
Break and Continue
Statements
»>
Nested Loops
Enter any one of the operator (+, -,*, /): /
The result is 21.0
In the program, for the operators "-" and "/", there
exists an if.. else condition within the elif block.
This is called nested if. We can have many levels of
nesting inside if.. else statements.
6.3 INDENTATION
In most programming languages, the statements within
a block are put inside curly brackets. However, Python
uses indentation for block as well as for nested block
structures. Leading whitespace (spaces and tabs) at
the beginning of a statement is called indentation.
In Python, the same level of indentation associates
statements into a single block of code. The interpreter
checks indentation levels very strictly and throws up
syntax errors if indentation is not correct. It is a common
practice to use a single tab for each level of indentation.
In the program 6-4, the if-else statement has two
blocks of statements and the statements in each block
are indented with the same amount of spaces or tabs.
Program 6-4 Program to find the larger of the two
pre-specified numbers.
#Program 6-4
# Program to find large: of the two numbers
num1 = 5
num2 = 6
if numl > num2:
%23Blockl
print ("first number is larger")
print ("Bye")
else:
#Block2
print ("second number is larger").
print ("Bye Bye")
Output:
second number is larger
Bye Bye
Program 6-2 Program to print the positive difference
of two numbers.
#Program 6-2
#Program to print the positive difference of two numbers
numl = int(input ("Enter first number: "))
num2 = int(input("Enter second number: "))
if num1 > num2:
else:
diff = numl - num2
diff = num2 - num1
print ("The difference of", numl, "and", num2, "is", diff)
Output:
Enter first number: 5
Enter second number: 6
The difference of 5 and 6 is 1
using elif is as
pubished
The syntax for a selection struc
shown below.
if condition:
statement (s)
elif condition:
statement (
elif condition:
not to
else:
statement(s)
Example 6.2 Check whether a number is positive,
negative, or zero.
number = int(input("Enter a number: ")
if number > 0:
print ("Number is positive")
elif number < 0:
else:
print ("Number is negative")
print ("Number is zero")
Example 6.3 Display the appropriate message as per
the colour of signal at the road crossing.
signal input ("Enter the colour: ")
if signal == "red" or signal == "RED":
print ("STOP")
elif signal == "orange" or signal ==
"ORANGE":
Rationalised 2023-24
FL
The syntax of if statement is:
if condition:
statement (s)
In the following example, if the age entered by the
user is greater than 18, then print that the user is
eligible to vote. If the condition is true, then the indented
statement(s) are executed. The indentation implies that
its execution is dependent on the condition. There is no
limit on the number of statements that can appear as a
block under the if statement.
Example 6.1
age
int (input("Enter your age "))
if age >= 18:
print("Eligible to vote")
A variant of if statement called if..else statement
allows us to write two alternative paths and the control
condition determines which path gets executed. The
syntax for if..else statement is as follows.
if condition:
else:
statement(s)
statement (s)
and
NCE
Let us now modify the example on voting with the
condition that if the age entered by the user is greater
than 18, then to display that the user is eligible to vote.
Otherwise display that the user is not eligible to vote.
age = int(input("Enter your age: "))
if age >= 18:
else:
print ("Eligible to vote")
print ("Not eligible to vote")
Now let us use the same concept to modify program
6-1, so that it always gives a positive difference as the
output. From the flow chart in Figure 6.2, it is clear that
we need to decide whether num1 > num2 or not and
take action accordingly.
We have to specify two blocks of statements since
num1 can be greater than num2 or vice-versa as shown
in program 6-2.
Many a times there are situations that require
multiple conditions to be checked and it may lead to
many alternatives. In such cases we can chain the
conditions using if..elif (elif means else..if).
43. What is the output of the following code?
x, int y, int z[]);
void fun (int
const int CAPACITY = 8;
int main()
{
int x [CAPACITY] = (4,2,3,4,5};
fun (x[3], x[1],x);
for (int i = 0; i < CAPACITY; i++)
cout << x[i];
return 0;
}
void fun (int x, int y, int z[])
{ for (int i = x; i > 0; i--)
{
z[i]
y+=i;
= i;
}
x = y;
}
a. 412124000
b. 415211001
C. 41234000
d. 412541001
Please do your best to
explain the process you took
to find the answer. The
correct answer is A.
e. none of the above because there is a syntax error
Transcribed Image Text:6.1 INTRODUCTION
In Figure 6.1, we see a bus carrying the children to
school. There is only one way to reach the school. The
driver has no choice, but to follow the road one milestone
after another to reach the school. We learnt in Chapter
5 that this is the concept of sequence, where Python
executes one statement after another from beginning to
the end of the program. These are the kind of programs
we have been writing till now.
SCHOOL BUS
"Don't you hate code that's
not properly indented?
Making it [indenting] part of
the syntax guarantees that all
publish
van Rossum
to
School
Figure 6.1: Bus carrying students to school
Let us consider a program 6-1 that executes in
sequence, that is, statements are executed in an order
in which they are written.
The order of execution of the statements in a program
is known as flow of control. The flow of control can be
implemented using control structures. Python supports
two types of control structures-selection and repetition.
Output:
Enter value 1: 84
Enter value 2: 4
In this chapter
» Introduction to Flow
of Control
«
Selection
»
Indentation
» Repetition
»
Break and Continue
Statements
»>
Nested Loops
Enter any one of the operator (+, -,*, /): /
The result is 21.0
In the program, for the operators "-" and "/", there
exists an if.. else condition within the elif block.
This is called nested if. We can have many levels of
nesting inside if.. else statements.
6.3 INDENTATION
In most programming languages, the statements within
a block are put inside curly brackets. However, Python
uses indentation for block as well as for nested block
structures. Leading whitespace (spaces and tabs) at
the beginning of a statement is called indentation.
In Python, the same level of indentation associates
statements into a single block of code. The interpreter
checks indentation levels very strictly and throws up
syntax errors if indentation is not correct. It is a common
practice to use a single tab for each level of indentation.
In the program 6-4, the if-else statement has two
blocks of statements and the statements in each block
are indented with the same amount of spaces or tabs.
Program 6-4 Program to find the larger of the two
pre-specified numbers.
#Program 6-4
# Program to find large: of the two numbers
num1 = 5
num2 = 6
if numl > num2:
%23Blockl
print ("first number is larger")
print ("Bye")
else:
#Block2
print ("second number is larger").
print ("Bye Bye")
Output:
second number is larger
Bye Bye
Program 6-2 Program to print the positive difference
of two numbers.
#Program 6-2
#Program to print the positive difference of two numbers
numl = int(input ("Enter first number: "))
num2 = int(input("Enter second number: "))
if num1 > num2:
else:
diff = numl - num2
diff = num2 - num1
print ("The difference of", numl, "and", num2, "is", diff)
Output:
Enter first number: 5
Enter second number: 6
The difference of 5 and 6 is 1
using elif is as
pubished
The syntax for a selection struc
shown below.
if condition:
statement (s)
elif condition:
statement (
elif condition:
not to
else:
statement(s)
Example 6.2 Check whether a number is positive,
negative, or zero.
number = int(input("Enter a number: ")
if number > 0:
print ("Number is positive")
elif number < 0:
else:
print ("Number is negative")
print ("Number is zero")
Example 6.3 Display the appropriate message as per
the colour of signal at the road crossing.
signal input ("Enter the colour: ")
if signal == "red" or signal == "RED":
print ("STOP")
elif signal == "orange" or signal ==
"ORANGE":
Rationalised 2023-24
FL
The syntax of if statement is:
if condition:
statement (s)
In the following example, if the age entered by the
user is greater than 18, then print that the user is
eligible to vote. If the condition is true, then the indented
statement(s) are executed. The indentation implies that
its execution is dependent on the condition. There is no
limit on the number of statements that can appear as a
block under the if statement.
Example 6.1
age
int (input("Enter your age "))
if age >= 18:
print("Eligible to vote")
A variant of if statement called if..else statement
allows us to write two alternative paths and the control
condition determines which path gets executed. The
syntax for if..else statement is as follows.
if condition:
else:
statement(s)
statement (s)
and
NCE
Let us now modify the example on voting with the
condition that if the age entered by the user is greater
than 18, then to display that the user is eligible to vote.
Otherwise display that the user is not eligible to vote.
age = int(input("Enter your age: "))
if age >= 18:
else:
print ("Eligible to vote")
print ("Not eligible to vote")
Now let us use the same concept to modify program
6-1, so that it always gives a positive difference as the
output. From the flow chart in Figure 6.2, it is clear that
we need to decide whether num1 > num2 or not and
take action accordingly.
We have to specify two blocks of statements since
num1 can be greater than num2 or vice-versa as shown
in program 6-2.
Many a times there are situations that require
multiple conditions to be checked and it may lead to
many alternatives. In such cases we can chain the
conditions using if..elif (elif means else..if).
43. What is the output of the following code?
x, int y, int z[]);
void fun (int
const int CAPACITY = 8;
int main()
{
int x [CAPACITY] = (4,2,3,4,5};
fun (x[3], x[1],x);
for (int i = 0; i < CAPACITY; i++)
cout << x[i];
return 0;
}
void fun (int x, int y, int z[])
{ for (int i = x; i > 0; i--)
{
z[i]
y+=i;
= i;
}
x = y;
}
a. 412124000
b. 415211001
C. 41234000
d. 412541001
Please do your best to
explain the process you took
to find the answer. The
correct answer is A.
e. none of the above because there is a syntax error
Expert Solution
Check MarkThis question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
bartleby
Step by stepSolved in 2 steps with 1 images
Knowledge Booster
Background pattern image
Recommended textbooks for you
- Text book imageC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrText book imageC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningText book imageEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT
- Text book imageProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageText book imagePrinciples of Information Systems (MindTap Course...Computer ScienceISBN:9781285867168Author:Ralph Stair, George ReynoldsPublisher:Cengage LearningText book image
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
Principles of Information Systems (MindTap Course...
Computer Science
ISBN:9781285867168
Author:Ralph Stair, George Reynolds
Publisher:Cengage Learning
Text book image