Professional Documents
Culture Documents
SYNTAX OF GO TO STATEMENT
Go to label;
.. .. ..
.. .. ..
.. .
label :
statement;
.. .. ..
In syntax above, label is an identifier. When go to label; I encountered, the control of program jumps to label : and executes the code below it.
FORWARD JUMP
BACKWARD JUMP
Example 1 : GO TO STATEMENT
OUTPUT
You can write any C++ program with use of go to statement and it is generally considered good idea not to use go to statement
Reason to Avoid go to Statement
o
The go to statement gives power to jump to any part of program but, makes the logic of the program complex and tangled. In modern
programming, go to statement is considered a harmful construct and a bad programming practice.
The go to statement can be replaced in most of C++ program with the use of break and continue statements.
b. WHILE
o While (condition ) {Code to execute while the condition is true } The true represents a Boolean
expression which could be x== 1 or while (x !=7) (x does not equal 7). It can be any combination of
Boolean statements that are legal. Even, (while x==5 || v ==7) which says execute the code while x
equals five or while v equals 7. Notice that a while loop is the same a a for loop without the initialization
and update sections. However, an empty condition is not legal for a while loop as it is with a for loop.
Example :c). Break and Continue It causes the execution flow to jump around and because it can make
the flow of logic harder to follow Example :
The break; statement terminates the loop (for, while, and do.. while loop) and switch statement
immediately when it appears.
Syntax of break
Break;
In real practice, break statement is almost always used inside the body of conditional statement(ifelse) inside the loop.
Output
In this C++ program, the test expression is always true. The user is asked to enter anumber which is
stored in variable number . If the user enters any number other than 0,that number is added to Sum and
stored to it and again user is asked to enter anothernumber. When user enters 0, the test expression
inside if statement is false and body of else is executed which terminates the loop. Finally, the sum is
displayed.
d. While True
o I'm curious about using a while statement with a true condition. What is theadvantage of using a
while(true) statement with break to exit the while statementover something like a for loop? I guess I'm
just curious when a good time to usethis technique would be? I saw it demonstrated in a quick sort
algorithm at schooltoday and was just curious about it. Insight? Example : e) Do \ White Thedo...while
Loop is similar to while loop with one very important difference. Inwhile loop, check expression is
checked at first before body of loop but in case ofdo...while loop, body of loop is executed first then
only test expression ischecked. That's why the body of do...while loop is executed at least
once. Example :
e. Do while statements
o One interesting thing about the while loop is that if the loop condition is initially false, thewhile loop will
not execute at all. It is sometimes the case that we know we want a loop toexecute at least once, such
as when displaying a menu. To help facilitate this, C++ offersthe do-while loop:
The statement in a do-while loop always executes at least once. After the statement has been executed,
the do-while loop checks the condition. If the condition is true, the CPU jumps back to the top of the dowhile loop and executes it again. Here is an example of using a do-while loop to display a menu to the
user and wait for theuser to make a valid choice:
o
o One interesting thing about the above example is that the selection variable must
bedeclared outside of the do block. Why do you think that is?
o If the selection variable were to be declared inside the do block, it would be destroyed when the do
f.
block terminates, which happens before the while conditional is executed. But we need the variable to
use in the while conditional -- consequently, the selection variable must be declared outside the do
block.
Generally it is good form to use a do-while loop instead of a while loop when you intentionally want the
loop to execute at least once, as it makes this assumption explicit
however, its not that big of a deal either way.
Jump \ Loop
o Cause a certain piece of program to be executed a certain number of times. Consider these
scenarios:
-You want to execute some code/s certain number of time.
-You want to execute some code/s certain number of times depending upon input from user.
Example :
Output :
If the boolean expression evaluates to true, then the if block of code will be executed,otherwise else block of code will
be executed.
An if statement can be followed by an optional else if...else statement, which is very use full to test
various conditions using single if...else if statement.
When using if , else if , else statements there are few points to keep in mind.
-An if can have zero or one else's and it must come after any else if's.
- An if can have zero to many else if's and they must come before the else.
- Once an else if succeeds, none of he remaining else if's or else's will be tested.
Syntax:
o
Example :
When the above code is compiled and executed, it produces the following result: