Like many other procedural languages, C++ provides different forms of statements for different purposes. Declaration statements are used for defining variables. Assignment-like statements are used for simple, algebraic computations. Branching statements are used for specifying alternate paths of execution, depending on the outcome of a logical condition. Loop statements are used for specifying computations which need to be repeated until a certain logical condition is satisfied. Flow control statements are used to divert the execution path to another part of the program. We will discuss these in turn.
In this you learn about Control Statements
1. Selection Statements
i. If
ii. If-else
iii. Nested-if
iv. If-Elseif ladder
2. Looping Statements
i. while loop
ii. do-while loop
iii. For loop
3. Jumping Statements
i. break
ii. continue
iii return
In this you learn about Control Statements
1. Selection Statements
i. If
ii. If-else
iii. Nested-if
iv. If-Elseif ladder
2. Looping Statements
i. while loop
ii. do-while loop
iii. For loop
3. Jumping Statements
i. break
ii. continue
iii return
In this presentation slides you will able to understand easily ,this slides contain loops of c++ programming language which contain for loop , while loop , do while loop and nested these all are describe with definition,examples and flow charts
Importance of loops in any programming language is immense, they allow us to reduce the number of lines in a code, making our code more readable and efficient.
INTRODUCTION
COMPARISON BETWEEN NORMAL FUNCTION AND INLINE FUNCTION
PROS AND CONS
WHY WHEN AND HOW TO USED?
GENERAL STRUCTURE OF INLINE FUNCTION
EXAMPLE WITH PROGRAM CODE
Decision Making in Java (if, if-else, switch, break, continue, jump) Decision Making in programming is similar to decision making in real life. A programming language uses control statements to control the flow of execution of program based on certain conditions.
In this presentation slides you will able to understand easily ,this slides contain loops of c++ programming language which contain for loop , while loop , do while loop and nested these all are describe with definition,examples and flow charts
Importance of loops in any programming language is immense, they allow us to reduce the number of lines in a code, making our code more readable and efficient.
INTRODUCTION
COMPARISON BETWEEN NORMAL FUNCTION AND INLINE FUNCTION
PROS AND CONS
WHY WHEN AND HOW TO USED?
GENERAL STRUCTURE OF INLINE FUNCTION
EXAMPLE WITH PROGRAM CODE
Decision Making in Java (if, if-else, switch, break, continue, jump) Decision Making in programming is similar to decision making in real life. A programming language uses control statements to control the flow of execution of program based on certain conditions.
The control statements enable us to specify the order in which the various instructions in a program are to be executed by the computer. They determine the flow of control in a program.
There are 4 types of control statements in C. They are:
a) Sequence control statements
b) Decision control statements or conditional statement
c) Case-control statements
d) Repetition or loop control statements
C Programming Language is the most popular computer language and most used programming language till now. It is very simple and elegant language. This lecture series will give you basic concepts of structured programming language with C.
As you know Sci Hub is sometimes down due to some technical issue, So you can visits the new Sci Hub New Domain with its Extension. ThesisScientist.com is New domain alternative of Sci Hub.
If Sci Hub website not working properly than you will move toward to others websites like ThesisScientist.com. It is the best website for downloading free papers and thesis and Free Journals
Computer network is a distributed system consisting of loosely coupled computers and other
devices. Any two of these devices, which we will from now on refer to as network elements or
transmitting elements, can communicate with each other through a communication medium. In
order for these connected devices to be considered a communicating network, there must be a set
of communicating rules or protocols each device in the network must follow to communicate wit
another device in the network. The resulting combination consisting of hardware and software is a computer communication network or computer network in short. Figure 1.1 shows a computer
network
Java is a general-purpose; object oriented programming language developed by Sun Microsystems of USA in 1991. This language was initially called “Oak” by James Gosling, but was renamed “Java” in 1995. Java (with a capital J) is a high-level, third generation programming language, like C, Fortran, Smalltalk, Perl, and many others.Java was initially designed to solve on a small scale could also be applied to the Internet on a large scale. This realization caused the focus of Java to switch from consumer electronic to Internet Programming.Java was designed for the development of software for consumer electronic devices like TVs, VCRs, Toasters, Microwaves ovens and such other electronics devices.Java is a first programming language that is not tied to any particular hardware or operating system. Programs developed in Java can be executed anywhere on any system.
Project Evaluation and Estimation in Software DevelopmentProf Ansari
Cost-benefit analysis
It mainly comprise two steps
Identify and estimating all of the costs and benefits of carrying out the project and operating the delivered application.
Expressing these costs and benefits in common units
We need to evaluate the net benefit, that is, the difference between the total benefit and the total benefit and the total cost of creating and operating the system.
We can categorize cost according to where they originate in the life of the project.
Stepwise Project planning in software developmentProf Ansari
The following activities are:
Identify objectives and practical measures of the effectiveness in meeting those objectives.
Establish a project authority
Stakeholder analysis – identify all stakeholders in the project and their interests
Modify objectives in the light of stakeholder’s analysis
Establish methods of communication with all parties
2.4
Entity Integrity Constraint:
It states that in a relation no attribute of a primary key (K) can have a null value. If a K consists of a single attribute, this constraint obviously applies on this attribute, so it cannot have the Null value. However, if a K consists of multiple attributes, then none of the attributes of this K can have the Null value in any of the instances.
Referential Integrity Constraint :
This constraint is applied to foreign keys. Foreign key is an attribute or attribute combination of a relation that is the primary key of another relation. This constraint states that if a foreign key exists in a relation, either the foreign key value must match the primary key value of some tuple in its home relation or the foreign key value must be completely null.
Normalisation in Database management System (DBMS)Prof Ansari
Normalization is a technique to organize the contents of the table for transactional database and data warehouse.
First Normal Form :
Seeing the data in the example in the book or assuming otherwise that all attributes contain the atomic value, we find out the table is in the 1NF.
Second Normal Form :
Seeing the FDs, we find out that the K for the table is a composite one comprising of empId, projName. We did not include the determinant of fourth FD, that is, the empDept, in the PK because empDept is dependent on empId and empID is included in our proposed PK. However, with this PK (empID, projName) we have got partial dependencies in the table through FDs 1 and 3 where we see that some attributes are being determined by subset of our K which is the violation of the requirement for the 2NF. So we split our table based on the FDs 1 and 3 as follows :
It is a semantic data model that is used for the graphical representation of the conceptual database design. The semantic data models provide more constructs that is why a database design in a semantic data model can contain/represent more details. With a semantic data model, it becomes easier to design the database, at the first place, and secondly it is easier to understand later. We also know that conceptual database is our first comprehensive design. It is independent of any particular implementation of the database, that is, the conceptual database design expressed in E-R data model can be implemented using any DBMS. For that we will have to transform the conceptual database design from E-R data model to the data model of the particular DBMS. There is no DBMS based on the E-R data model, so we have to transform the conceptual database design anyway.
The schemas as it has been defined already; is the repository used for storing definitions of the structures used in database, it can be anything from any entity to the whole organization. For this purpose the architecture defines different schemas stored at different levels for isolating the details one level from the other.
Different levels existing pat different levels of the database architecture pare expressed below with emphasis on the details of all the levels individually. Core of the database architecture is the internal level of schema which is discussed a bit before getting into the details of each level individually.
INTRODUCTION TO Database Management System (DBMS)Prof Ansari
shared collection of logically related data, designed to meet the information needs of multiple users in an organization. The term database is often erroneously referred to as a synonym for a “database management system DBMS)”. They are not equivalent and it will be explained in the next section.
Master thesis on Vehicular Ad hoc Networks (VANET)Prof Ansari
The increasing demand for wireless devices and wireless communication tends to research on self-organizing, self-healing networks without the interference of any pre-established or centralized infrastructure/authority [2]. The networks with the absence of any pre-established or centralized authority are known as Ad hoc networks [4]. Ad hoc Networks are the kind of wireless networks that uses multi-hop radio relay.There are many comparative studies and surveys that compare various ad hoc routing in VANET environment. The simulations performed in these comparative studies are very basic do not incorporate with a large number of nodes in real Vehicular Ad hoc Network environment. The main aim of our dissertation work is to firstly investigate the reactive and proactive routeing protocols than examine the performance of selected reactive routing protocols i.e. Destination Sequence Distance Vector Routing (DSDV), Ad hoc On-Demand Distance Vector (AODV), Optimized Link State Routing (OLSR) and Dynamic Source Routing (DSR)by taking three performance metrics like network load, throughput and end-to-end delay with varying number of mobile nodes or vehicle node densityOPNET: Optimized Network Engineering Tool (OPNET) is a commercial network simulator environment used for simulations of both wired and wireless networks [20]. Several different OPNET versions have been released over the last few years; the latest version of OPNET is the OPNET 16.0. At present OPNET is licensed under Riverbed technologies. It allows the user to design and study the network communication devices, protocols, individual applications and also simulate the performance of routing protocol. It supports many wireless technologies and standards such as, IEEE 802.11, IEEE 802.15.1, IEEE 802.16, IEEE 802.20 and satellite networks. OPNET IT Guru Academic Edition is available for free to the academic research and teaching community.
Master Thesis on Vehicular Ad-hoc Network (VANET)Prof Ansari
In present, many people during the public died each year in vehicle accidents, therefore in almost countries some safety data i.e. traffic lights & velocity limits are applied, simply however it is not a better solution. Also government and number of automation industries regarded that vehicular safety is real challenging task [1]. Then equally result, to enhance people traffic safety of a new progressed particular technology is formulated i.e. VANET [4]. It is progress type of MANET (Mobile Ad-hoc Network). VANET manages a network within which vehicles are act nodes and applied as mobile nodes to construct a robust infrastructure-less ad-hoc network. In Figure 1 illustrates the basic components of VANET architecture. It builds the network among Inter-Vehicle, Vehicle-to-Roadside and Inter-Roadside communicating networks [4]. Moreover, apart from accidental-safety and security types, there are also broad varieties of applications in VANET are available and potential that can extend passenger comfort like predictable mobility by GPS, web browsing and information modify and so on. Vehicular Ad-hoc Network (VANET) is a novel formulated form of Mobile Ad-hoc Network (MANET), where moving nodes are vehicles same automobiles, cars, buses etc [2].
Read/Write control logic:
The Read/Write Control logic interfaces the 8251A with CPU, determines the functions of the 8251A according to the control word written into its control register.
It monitors the data flow.
This section has three registers and they are control register, status register and data buffer.
The active low signals RD, WR, CS and C/D(Low) are used for read/write operations with these three registers.
When C/D(low) is high, the control register is selected for writing control word or reading status word.
HOST AND NETWORK SECURITY by ThesisScientist.comProf Ansari
Network management means different things to different people. In some cases, it involves a solitary network consultant monitoring network activity with an outdated protocol analyzer. In other cases, network management involves a distributed database, auto polling of network devices, and high-end workstations generating real-time graphical views of network topology changes and traffic. In general, network management is a service that employs a variety of tools, applications, and devices to assist human network managers in monitoring and maintaining networks.
SYSTEM NETWORK ADMINISTRATIONS GOALS and TIPSProf Ansari
The goal of network administration is to ensure that the users of networks receive the information and technically serves with the quality of services they expect.
Network administration means the management of network infrastructures devices (such as router and switches)
Network administration compromises of 3 majors groups:
1. Network provisioning
2. Network operations
3. Network maintenance
The VB6 IDE (Integrated Development Environment) is a very simple and fully featured IDE. If you start out programming in VB6 you may end up being too spoiled to ever appreciate a more complicated and less functional IDE like most C++ IDEs. One feature which sets VB6 apart from various IDEs is the simplicity of its approach to GUI (Graphical User Interface) design.
As a general rule: Play with it. You're very unlikely to break anything that matters, so just explore and experiment with the IDE, and you'll learn more.
ppt on blogging and In order to make money blogging you’re going to need to have a blog. While this is pretty obvious it is also a stumbling block for many PreBloggers who come to the idea of blogging with little or no technical background.blogging ppt presentation
Software Engineering is the set of processes and tools to develop software. Software Engineering is the combination of all the tools, techniques, and processes that used in software production. Therefore Software Engineering encompasses all those things that are used in software production like :
Programming Language
Programming Language Design
Software Design Techniques
Tools
Testing
Maintenance
Development etc.
These days object-oriented programming is widely being used. If programming languages will not support object-orientation then it will be very difficult to implement object-oriented design using object-oriented principles. All these efforts made the basis of software engineering.
E-Commerce is defined as the paperless exchange of business information using Electronic Data Interchange (EDI), electronic mail (e-mail), computer bulletin boards, Electronic Funds Transfer (EFT), and other, similar technologies.
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdffxintegritypublishin
Advancements in technology unveil a myriad of electrical and electronic breakthroughs geared towards efficiently harnessing limited resources to meet human energy demands. The optimization of hybrid solar PV panels and pumped hydro energy supply systems plays a pivotal role in utilizing natural resources effectively. This initiative not only benefits humanity but also fosters environmental sustainability. The study investigated the design optimization of these hybrid systems, focusing on understanding solar radiation patterns, identifying geographical influences on solar radiation, formulating a mathematical model for system optimization, and determining the optimal configuration of PV panels and pumped hydro storage. Through a comparative analysis approach and eight weeks of data collection, the study addressed key research questions related to solar radiation patterns and optimal system design. The findings highlighted regions with heightened solar radiation levels, showcasing substantial potential for power generation and emphasizing the system's efficiency. Optimizing system design significantly boosted power generation, promoted renewable energy utilization, and enhanced energy storage capacity. The study underscored the benefits of optimizing hybrid solar PV panels and pumped hydro energy supply systems for sustainable energy usage. Optimizing the design of solar PV panels and pumped hydro energy supply systems as examined across diverse climatic conditions in a developing country, not only enhances power generation but also improves the integration of renewable energy sources and boosts energy storage capacities, particularly beneficial for less economically prosperous regions. Additionally, the study provides valuable insights for advancing energy research in economically viable areas. Recommendations included conducting site-specific assessments, utilizing advanced modeling tools, implementing regular maintenance protocols, and enhancing communication among system components.
Hierarchical Digital Twin of a Naval Power SystemKerry Sado
A hierarchical digital twin of a Naval DC power system has been developed and experimentally verified. Similar to other state-of-the-art digital twins, this technology creates a digital replica of the physical system executed in real-time or faster, which can modify hardware controls. However, its advantage stems from distributing computational efforts by utilizing a hierarchical structure composed of lower-level digital twin blocks and a higher-level system digital twin. Each digital twin block is associated with a physical subsystem of the hardware and communicates with a singular system digital twin, which creates a system-level response. By extracting information from each level of the hierarchy, power system controls of the hardware were reconfigured autonomously. This hierarchical digital twin development offers several advantages over other digital twins, particularly in the field of naval power systems. The hierarchical structure allows for greater computational efficiency and scalability while the ability to autonomously reconfigure hardware controls offers increased flexibility and responsiveness. The hierarchical decomposition and models utilized were well aligned with the physical twin, as indicated by the maximum deviations between the developed digital twin hierarchy and the hardware.
Saudi Arabia stands as a titan in the global energy landscape, renowned for its abundant oil and gas resources. It's the largest exporter of petroleum and holds some of the world's most significant reserves. Let's delve into the top 10 oil and gas projects shaping Saudi Arabia's energy future in 2024.
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxR&R Consult
CFD analysis is incredibly effective at solving mysteries and improving the performance of complex systems!
Here's a great example: At a large natural gas-fired power plant, where they use waste heat to generate steam and energy, they were puzzled that their boiler wasn't producing as much steam as expected.
R&R and Tetra Engineering Group Inc. were asked to solve the issue with reduced steam production.
An inspection had shown that a significant amount of hot flue gas was bypassing the boiler tubes, where the heat was supposed to be transferred.
R&R Consult conducted a CFD analysis, which revealed that 6.3% of the flue gas was bypassing the boiler tubes without transferring heat. The analysis also showed that the flue gas was instead being directed along the sides of the boiler and between the modules that were supposed to capture the heat. This was the cause of the reduced performance.
Based on our results, Tetra Engineering installed covering plates to reduce the bypass flow. This improved the boiler's performance and increased electricity production.
It is always satisfying when we can help solve complex challenges like this. Do your systems also need a check-up or optimization? Give us a call!
Work done in cooperation with James Malloy and David Moelling from Tetra Engineering.
More examples of our work https://www.r-r-consult.dk/en/cases-en/
1. For more Https://www.ThesisScientist.com
UNIT 3
STATEMENTS
Like many other procedural languages, C++ provides different forms of
statements for different purposes. Declaration statements are used for defining
variables. Assignment-like statements are used for simple, algebraic
computations. Branching statements are used for specifying alternate paths of
execution, depending on the outcome of a logical condition. Loop statements
are used for specifying computations which need to be repeated until a certain
logical condition is satisfied. Flow control statements are used to divert the
execution path to another part of the program. We will discuss these in turn.
Simple and Compound Statements
A simple statement is a computation terminated by a semicolon. Variable
definitions and semicolon-terminated expressions are examples:
int i; // declaration statement
++i; // this has a side-effect
double d = 10.5; // declaration statement
d + 5; // useless statement!
The simplest statement is the null statement which consists of just a
semicolon:
; // null statement
Although the null statement has no side-effect, as we will see later in the
chapter, it has some genuine uses.
Multiple statements can be combined into a compound statement by
enclosing them within braces. For example:
{ int min, i = 10, j = 20;
min = (i < j ? i : j);
cout << min << 'n';
}
Compound statements are useful in two ways: (i) they allow us to put multiple
statements in places where otherwise only single statements are allowed, and
(ii) they allow us to introduce a new scope in the program. A scope is a part of
the program text within which a variable remains defined. For example, the
scope of min, i, and j in the above example is from where they are defined till
2. the closing brace of the compound statement. Outside the compound
statement, these variables are not defined. Because a compound statement may
contain variable definitions and defines a scope for them, it is also called a
block.
¨
The If Statement
It is sometimes desirable to make the execution of a statement dependent upon
a condition being satisfied. The if statement provides a way of expressing
this, the general form of which is:
if (expression)
statement;
First expression is evaluated. If the outcome is nonzero then statement is
executed. Otherwise, nothing happens.
For example, when dividing two values, we may want to check that the
denominator is nonzero:
if (count != 0)
average = sum / count;
To make multiple statements dependent on the same condition, we can
use a compound statement:
if (balance > 0) {
interest = balance * creditRate;
balance += interest;
}
A variant form of the if statement allows us to specify two alternative
statements: one which is executed if a condition is satisfied and one which is
executed if the condition is not satisfied. This is called the if-else statement and
has the general form:
if (expression)
statement1;
else
statement2;
First expression is evaluated. If the outcome is nonzero then statement1 is
executed. Otherwise, statement2 is executed.
For example:
if (balance > 0) {
interest = balance * creditRate;
balance += interest;
} else {
interest = balance * debitRate;
balance += interest;
}
4. The switch Statement
The switch statement provides a way of choosing between a set of
alternatives, based on the value of an expression. The general form of the
switch statement is:
switch (expression) {
case constant1:
statements;
...
case constantn:
statements;
default:
statements;
}
First expression (called the switch tag) is evaluated, and the outcome is
compared to each of the numeric constants (called case labels), in the order
they appear, until a match is found. The statements following the matching
case are then executed. Note the plural: each case may be followed by zero or
more statements (not just one statement). Execution continues until either a
break statement is encountered or all intervening statements until the end of
the switch statement are executed. The final default case is optional and is
exercised if none of the earlier cases provide a match.
For example, suppose we have parsed a binary arithmetic operation into
its three components and stored these in variables operator, operand1, and
operand2. The following switch statement performs the operation and stored
the result in result.
switch (operator) {
case '+': result = operand1 + operand2;
break;
case '-': result = operand1 - operand2;
break;
case '*': result = operand1 * operand2;
break;
case '/': result = operand1 / operand2;
break;
default: cout << "unknown operator: " << ch << 'n';
break;
}
As illustrated by this example, it is usually necessary to include a break
statement at the end of each case. The break terminates the switch statement
by jumping to the very end of it. There are, however, situations in which it
makes sense to have a case without a break. For example, if we extend the
above statement to also allow x to be used as a multiplication operator, we
will have:
5. For more Https://www.ThesisScientist.com
switch (operator) {
case '+': result = operand1 + operand2;
break;
case '-': result = operand1 - operand2;
break;
case 'x':
case '*': result = operand1 * operand2;
break;
case '/': result = operand1 / operand2;
break;
default: cout << "unknown operator: " << ch << 'n';
break;
}
Because case 'x' has no break statement (in fact no statement at all!), when
this case is satisfied, execution proceeds to the statements of the next case and
the multiplication is performed.
It should be obvious that any switch statement can also be written as
multiple if-else statements. The above statement, for example, may be written
as:
if (operator == '+')
result = operand1 + operand2;
else if (operator == '-')
result = operand1 - operand2;
else if (operator == 'x' || operator == '*')
result = operand1 * operand2;
else if (operator == '/')
result = operand1 / operand2;
else
cout << "unknown operator: " << ch << 'n';
The While Statement
¨
The while statement (also called while loop) provides a way of repeating an
statement while a condition holds. It is one of the three flavors of iteration in
C++. The general form of the while statement is:
while (expression)
statement;
First expression (called the loop condition) is evaluated. If the outcome is
nonzero then statement (called the loop body) is executed and the whole
process is repeated. Otherwise, the loop is terminated.
For example, suppose we wish to calculate the sum of all numbers from 1
to some integer denoted by n. This can be expressed as:
i = 1;
sum = 0;
6. while (i <= n)
sum += i++;
For n set to 5, Table 3.1 provides a trace of the loop by listing the values
of the variables involved and the loop condition.
Table 3.1 While loop trace.
Iteration i n i <= n sum += i++
First 1 5 1 1
Second 2 5 1 3
Third 3 5 1 6
Fourth 4 5 1 10
Fifth 5 5 1 15
Sixth 6 5 0
It is not unusual for a while loop to have an empty body (i.e., a null
statement). The following loop, for example, sets n to its greatest odd factor.
while (n % 2 == 0 && n /= 2)
;
Here the loop condition provides all the necessary computation, so there is no
real need for a body. The loop condition not only tests that n is even, it also
divides n by two and ensures that the loop will terminate should n be zero.
The do Statement
¨
The do statement (also called do loop) is similar to the while statement,
except that its body is executed first and then the loop condition is examined.
The general form of the do statement is:
do
statement;
while (expression);
First statement is executed and then expression is evaluated. If the outcome of
the latter is nonzero then the whole process is repeated. Otherwise, the loop is
terminated.
The do loop is less frequently used than the while loop. It is useful for
situations where we need the loop body to be executed at least once,
regardless of the loop condition. For example, suppose we wish to repeatedly
read a value and print its square, and stop when the value is zero. This can be
expressed as the following loop:
do {
cin >> n;
cout << n * n << 'n';
} while (n != 0);
7. For more Https://www.ThesisScientist.com
Unlike the while loop, the do loop is never used in situations where it
would have a null body. Although a do loop with a null body would be
equivalent to a similar while loop, the latter is always preferred for its superior
readability.
The For Statement ¨
The for statement (also called for loop) is similar to the while statement, but
has two additional components: an expression which is evaluated only once
before everything else, and an expression which is evaluated once at the end of
each iteration. The general form of the for statement is:
for (expression1; expression2; expression3)
statement;
First expression1 is evaluated. Each time round the loop, expression2 is
evaluated. If the outcome is nonzero then statement is executed and expression3
is evaluated. Otherwise, the loop is terminated. The general for loop is
equivalent to the following while loop:
expression1;
while (expression2) {
statement;
expression3;
}
The most common use of for loops is for situations where a variable is
incremented or decremented with every iteration of the loop. The following
for loop, for example, calculates the sum of all integers from 1 to n.
sum = 0;
for (i = 1; i <= n; ++i)
sum += i;
This is preferred to the while-loop version we saw earlier. In this example, i is
usually called the loop variable.
C++ allows the first expression in a for loop to be a variable definition. In
the above loop, for example, i can be defined inside the loop itself:
for (int i = 1; i <= n; ++i)
sum += i;
Contrary to what may appear, the scope for i is not the body of the loop, but
the loop itself. Scope-wise, the above is equivalent to:
8. int i;
for (i = 1; i <= n; ++i)
sum += i;
The continue statement
The continue statement terminates the current iteration of a loop and instead
jumps to the next iteration. It applies to the loop immediately enclosing the
continue statement. It is an error to use the continue statement outside a loop.
In while and do loops, the next iteration commences from the loop
condition. In a for loop, the next iteration commences from the loop’s third
expression. For example, a loop which repeatedly reads in a number, processes
it but ignores negative numbers, and terminates when the number is zero, may
be expressed as:
do {
cin >> num;
if (num < 0) continue;
// process num here...
} while (num != 0);
This is equivalent to:
do {
cin >> num;
if (num >= 0) {
// process num here...
}
} while (num != 0);
A variant of this loop which reads in a number exactly n times (rather than
until the number is zero) may be expressed as:
for (i = 0; i < n; ++i) {
cin >> num;
if (num < 0) continue; // causes a jump to: ++i
// process num here...
}
When the continue statement appears inside nested loops, it applies to the
loop immediately enclosing it, and not to the outer loops. For example, in the
following set of nested loops, the continue applies to the for loop, and not the
while loop:
while (more) {
for (i = 0; i < n; ++i) {
cin >> num;
if (num < 0) continue; // causes a jump to: ++i
// process num here...
}
//etc...
10. The break Statement
A break statement may appear inside a loop (while, do, or for) or a switch
statement. It causes a jump out of these constructs, and hence terminates
them. Like the continue statement, a break statement only applies to the loop
or switch immediately enclosing it. It is an error to use the break statement
outside a loop or a switch.
For example, suppose we wish to read in a user password, but would like
to allow the user a limited number of attempts:
for (i = 0; i < attempts; ++i) {
cout << "Please enter your password: ";
cin >> password;
if (Verify(password)) // check password for correctness
break; // drop out of the loop
cout << "Incorrect!n";
}
Here we have assumed that there is a function called Verify which checks a
password and returns true if it is correct, and false otherwise.
Rewriting the loop without a break statement is always possible by using
an additional logical variable (verified) and adding it to the loop condition:
verified = 0;
for (i = 0; i < attempts && !verified; ++i) {
cout << "Please enter your password: ";
cin >> password;
verified = Verify(password));
if (!verified)
cout << "Incorrect!n";
}
The break version is arguably simpler and therefore preferred.
¨
11. For more Https://www.ThesisScientist.com
The goto Statement
The goto statement provides the lowest-level of jumping. It has the general
form:
goto label;
where label is an identifier which marks the jump destination of goto. The
label should be followed by a colon and appear before a statement within the
same function as the goto statement itself.
For example, the role of the break statement in the for loop in the
previous section can be emulated by a goto:
for (i = 0; i < attempts; ++i) {
cout << "Please enter your password: ";
cin >> password;
if (Verify(password)) // check password for correctness
goto out; // drop out of the loop
cout << "Incorrect!n";
}
out:
//etc...
The return statement ¨
The return statement enables a function to return a value to its caller. It has
the general form:
return expression;
where expression denotes the value returned by the function. The type of this
value should match the return type of the function. For a function whose
return type is void, expression should be empty:
return;
The only function we have discussed so far is main, whose return type is
always int. The return value of main is what the program returns to the
operating system when it completes its execution. Under UNIX, for example,
it its conventional to return 0 from main when the program executes without
errors. Otherwise, a non-zero error code is returned. For example:
int main (void)
{
cout << "Hello Worldn";
return 0;
}