Cpp.IfStatement Class
Namespace: Cpp
Superclasses: AstNodeProperties
Represents the if_statement nodes in the syntax tree of your code
Since R2026a
Description
The PQL class Cpp.IfStatement represents the node if_statement in the syntax tree of your code.
extern void foo(); extern void bar();
int f(int x) {
if (x > 0) {
foo();
} else {
bar();
}
return 0;
}The if (x > 0) { ... } else { ... } in the example corresponds to the if_statement node represented by Cpp.IfStatement.
Predicates
| Type | Raisable | Printable |
|---|---|---|
IfStatement
| Yes | No |
This class defines these predicates that act on the objects of this class. In addition, objects of this class can access the predicates defined by the base class AstNodeProperties. An object of this class is an object of AstNodeProperties class.
| Predicates | Description | Example |
|---|---|---|
is(required IfStatement &ifStatement)
| Matches an if_statement node and returns it as the output
variable ifStatement. Use this to select an if-statement for
further predicates. | This PQL defect checks for the presence of any
defect hasIf =
when
Cpp.IfStatement.is(&ifStmt)
raise "Found an if-statement"
on ifStmtIn this C++ code the defect finds the top-level if-statement in the function.
void foo() {
if (true) { }
} |
cast(Cpp.Node.Node node, required IfStatement &cast)
| Converts a generic Node to an
if_statement and returns it as cast when the
node is an if_statement. | This PQL defect checks whether a previously obtained
defect castNodeToIf =
when
Cpp.Node.is(&n,&,&,&)
and Cpp.IfStatement.cast(n, &ifCast)
raise "Node successfully cast to if_statement"
on ifCastIn this C++ code the defect finds a
void bar() {
if (0) { }
} |
isa(Cpp.Node.Node node)
| Returns true when the provided generic Node is an
if_statement. Use not Cpp.IfStatement.isa(n)
to negate. | This PQL defect checks whether a defect nodeIsIf =
when
Cpp.IfStatement.is(&tmpIf)
and tmpIf.toNode(&n)
and Cpp.IfStatement.isa(n)
raise "Node is an if_statement"
on tmpIfIn this C++ code the defect finds a
int baz() {
if (1) return 1;
return 0;
} |
condition(IfStatement self, Cpp.Node.Node &cond)
| Returns cond as the condition expression node of the
self. | This PQL defect checks the condition expression of an
defect ifConditionText =
when
Cpp.IfStatement.is(&ifS)
and ifS.condition(&cond)
and cond.nodeText(&t)
raise "If condition: {t}"
on ifSIn this C++ code the defect extracts the
void g(int x) {
if (x > 0) { }
} |
consequence(IfStatement self, Cpp.Node.Node &consequence)
| Returns the statement executed when the condition is true as
consequence. | This PQL defect checks the then-branch of an
defect ifConsequence =
when
Cpp.IfStatement.is(&ifS)
and ifS.consequence(&thenNode)
and thenNode.nodeText(&text)
raise "Consequence: {text}"
on ifSIn this C++ code the defect reports the
statement
extern void foo();
void h(int x) {
if (x) foo();
} |
alternative(IfStatement self, Cpp.Node.Node &alternative)
| Returns the else-branch node of self as
alternative when an else branch exists. | This PQL defect checks the else-branch of an
defect ifAlternative =
when
Cpp.IfStatement.is(&ifS)
and ifS.alternative(&alt)
and alt.nodeText(&t)
raise "Else-branch text: {t}"
on ifSIn this C++ code the defect finds the
extern void bar();
void k(int x) {
if (x) { }
else bar();
} |
hasAlternative(IfStatement self)
| True when self has an else-branch. Use to guard accesses to
alternative or elseBranch. | This PQL defect checks whether an defect hasElseCheck =
when
Cpp.IfStatement.is(&ifS)
and ifS.hasAlternative()
raise "If has else"
on ifSIn this C++ code the defect detects the presence
of the
void m() {
if (0) { }
else { }
} |
thenBranch(IfStatement self, Cpp.Node.Node &thenStatement)
| Returns the then-branch node of self as
thenStatement. | This PQL defect checks the then-branch statement of an
defect thenBranchText =
when
Cpp.IfStatement.is(&ifs)
and ifs.thenBranch(&tb)
and tb.nodeText(&txt)
raise "Then branch: {txt}"
on ifsIn this C++ code the defect reports the
single-statement then-branch
extern void foo();
void n() {
if (true) foo();
} |
elseBranch(IfStatement self, Cpp.Node.Node &elseStatement)
| Returns elseStatement to the else-branch node of
self. | This PQL defect checks and extracts the else-branch node from an
defect elseBranchText =
when
Cpp.IfStatement.is(&ifs)
and ifs.elseBranch(&eb)
and eb.nodeText(&txt)
raise "Else branch: {txt}"
on ifsIn this C++ code the defect extracts the else-branch.
extern void bar();
void o() {
if (false) { }
else bar();
} |
hasElse(IfStatement self)
| Returns true when self has an else-branch. | This PQL defect checks whether an defect checkHasElse =
when
Cpp.IfStatement.is(&ifs)
and ifs.hasElse()
raise "If has else"
on ifsIn this C++ code the defect detects the else clause presence.
void p() {
if (1) { }
else { }
} |
getEnclosingIfStatement(Cpp.Node.Node child, required IfStatement
&parent)
| Returns the nearest ancestor if_statement that encloses the
child node. | This PQL defect checks which defect findEnclosingIf =
when
Cpp.Node.is(&n,&,&,&)
and Cpp.IfStatement.getEnclosingIfStatement(n, &parentIf)
raise "Found enclosing if"
on parentIfIn this C++ code the defect finds an if statement that encloses any syntax node.
void q() {
if (true) {
if (false) { }
}
} |
isEnclosedInIfStatement(Cpp.Node.Node child)
| Returns true when the child node has any ancestor of type
if_statement. | This PQL defect checks whether a given node is anywhere inside an
defect checkEnclosedInIf =
when
Cpp.Node.is(&n,&,&,&)
and Cpp.IfStatement.isEnclosedInIfStatement(n)
raise "Node is enclosed in an if"
on ifsIn this C++ code the defect find the syntax
nodes that are enclosed in an
void r() {
if (true) {
int x = 0;
}
} |
Version History
Introduced in R2026a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)