Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

Jump Statements – goto, break, continue

Typecasting
Debugging
Variable declaration and scope- Local and Global
C++ goto Statement
What is it? And why it should be avoided !!
• goto statement is used to alter the normal
sequence of program execution by
transferring control to some other part of the
Syntax
program.

Reason to Avoid goto Statement


• The goto statement gives power to jump to any part of
program but, makes the logic of the program complex and
tangled.
• In modern programming, goto statement is considered a
label is an identifier
harmful construct and a bad programming practice.
• The goto statement can be replaced in most of C++ program
with the use of break and continue statements.
BREAK
CONTINUE
Typecasting
• A type cast is basically a conversion from one
data type to another.
Debugging your program
Syntax and semantic errors
• A syntax error occurs when you write a
statement that is not valid according to the
grammar of the C++ language. This includes
errors such as missing semicolons, undeclared
variables, mismatched parentheses or braces
etc.
Debugging your program
• A semantic error occurs when a statement is
syntactically valid, but does not do what the
programmer intended.
Program will crash at runtime due to divide by zero error

Compiler will not be able to catch these types of problems,


because the compiler only knows what you wrote, not what
you intended.
Debugging your program
• A debugger is a computer program that allows the
programmer to control how a program executes and watch
what happens as it runs.
• For example, the programmer can use a debugger to
execute a program line by line, examining the value of
variables along the way.

• Stepping is a debugger feature that lets you execute (step


through) your code line by line. This allows you to examine
each line of code in isolation to determine whether it is
behaving as intended.
• There are actually 3 different stepping commands: step
into, step over, and step out
• Run to cursor: This command executes the
program like normal until it gets to the line of
code selected by your cursor. Then it returns
control to you so you can debug starting at
that point.

• A breakpoint is a special marker that tells the


debugger to stop execution of the program at
the breakpoint when running in debug mode.
VARIABLE DECLARATION AND SCOPE
Whenever there is a local variable defined
with same name as that of a global
variable then the compiler will give
precedence to the local variable

So OUTPUT will be 2
What if you want to access the global variable which has the same
name as local variable, use scope resolution operator ::

#include<iostream>
using namespace std;

// Global x Output
int x = 0; Value of global x is 0
Value of local x is 10
int main()
{
// Local x
int x = 10;
cout << "Value of global x is " << ::x;
cout<< "\nValue of local x is " << x;
return 0;
}
THANKS

You might also like