You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/cpp/if-else-statement-cpp.md
+60-59
Original file line number
Diff line number
Diff line change
@@ -15,14 +15,15 @@ ms.workload: ["cplusplus"]
15
15
# if-else Statement (C++)
16
16
17
17
Controls conditional branching. Statements in the *if-block* are executed only if the *if-expression* evaluates to a non-zero value (or TRUE). If the value of *expression* is nonzero, *statement1* and any other statements in the block are executed and the else-block, if present, is skipped. If the value of *expression* is zero, then the if-block is skipped and the else-block, if present, is executed. Expressions that evaluate to non-zero are
18
+
18
19
- TRUE
19
20
- a non-null pointer,
20
21
- any non-zero arithmetic value, or
21
22
- a class type that defines an unambiguous conversion to an arithmetic, boolean or pointer type. (For information about conversions, see [Standard Conversions](../cpp/standard-conversions.md).)
22
23
23
24
## Syntax
24
25
25
-
```
26
+
```cpp
26
27
if ( expression )
27
28
{
28
29
statement1;
@@ -70,49 +71,49 @@ using namespace std;
70
71
classC
71
72
{
72
73
public:
73
-
void do_somthing(){}
74
+
void do_something(){}
74
75
};
75
76
voidinit(C){}
76
77
bool is_true() { return true; }
77
78
int x = 10;
78
79
79
80
int main()
80
81
{
81
-
if (is_true())
82
-
{
83
-
cout << "b is true!\n"; // executed
84
-
}
85
-
else
86
-
{
87
-
cout << "b is false!\n";
88
-
}
89
-
90
-
// no else statement
91
-
if (x == 10)
92
-
{
93
-
x = 0;
94
-
}
95
-
96
-
97
-
C* c;
98
-
init(c);
99
-
if (c)
100
-
{
101
-
c->do_something();
102
-
}
103
-
else
104
-
{
105
-
cout << "c is null!\n";
106
-
}
82
+
if (is_true())
83
+
{
84
+
cout << "b is true!\n"; // executed
85
+
}
86
+
else
87
+
{
88
+
cout << "b is false!\n";
89
+
}
90
+
91
+
// no else statement
92
+
if (x == 10)
93
+
{
94
+
x = 0;
95
+
}
96
+
97
+
C* c;
98
+
init(c);
99
+
if (c)
100
+
{
101
+
c->do_something();
102
+
}
103
+
else
104
+
{
105
+
cout << "c is null!\n";
106
+
}
107
107
}
108
108
```
109
+
109
110
## <a name="if_with_init"></a> if statement with an initializer
110
111
111
112
**Visual Studio 2017 version 15.3 and later** (available with [/std:c++17](../build/reference/std-specify-language-standard-version.md)): An **if** statement may also contain an expression that declares and initializes a named variable. Use this form of the if-statement when the variable is only needed within the scope of the if-block.
112
113
113
-
```cpp
114
114
## Example
115
115
116
+
```cpp
116
117
#include <iostream>
117
118
#include <mutex>
118
119
#include <map>
@@ -129,28 +130,28 @@ void unsafe_operation() {}
129
130
int main()
130
131
{
131
132
132
-
if (auto it = m.find(10); it != m.end())
133
-
{
134
-
cout << it->second;
135
-
return 0;
136
-
}
133
+
if (auto it = m.find(10); it != m.end())
134
+
{
135
+
cout << it->second;
136
+
return 0;
137
+
}
137
138
138
-
if (char buf[10]; fgets(buf, 10, stdin))
139
-
{
140
-
m[0] += buf;
141
-
}
139
+
if (char buf[10]; fgets(buf, 10, stdin))
140
+
{
141
+
m[0] += buf;
142
+
}
142
143
143
-
if (lock_guard<mutex> lock(mx); shared_flag)
144
-
{
145
-
unsafe_operation();
146
-
shared_flag = false;
147
-
}
144
+
if (lock_guard<mutex> lock(mx); shared_flag)
145
+
{
146
+
unsafe_operation();
147
+
shared_flag = false;
148
+
}
148
149
149
-
string s{ "if" };
150
+
string s{ "if" };
150
151
if (auto keywords = { "if", "for", "while" }; any_of(keywords.begin(), keywords.end(), [&s](const char* kw) { return s == kw; }))
151
-
{
152
-
cout << "Error! Token must not be a keyword\n";
153
-
}
152
+
{
153
+
cout << "Error! Token must not be a keyword\n";
154
+
}
154
155
}
155
156
```
156
157
@@ -166,18 +167,18 @@ The **else** clause of an `if...else` statement is associated with the closest p
Copy file name to clipboardExpand all lines: docs/cpp/member-access-control-cpp.md
+20-20
Original file line number
Diff line number
Diff line change
@@ -35,7 +35,7 @@ protected: // Declare protected function for derived classes only.
35
35
36
36
The default access is **private** in a class, and **public** in a struct or union. Access specifiers in a class can be used any number of times in any order. The allocation of storage for objects of class types is implementation dependent, but members are guaranteed to be assigned successively higher memory addresses between access specifiers.
37
37
38
-
###Member-Access Control
38
+
## Member-Access Control
39
39
40
40
|Type of Access|Meaning|
41
41
|--------------------|-------------|
@@ -73,40 +73,40 @@ The following example illustrates this:
73
73
classBaseClass
74
74
{
75
75
public:
76
-
int PublicFunc(); // Declare a public member.
76
+
int PublicFunc(); // Declare a public member.
77
77
protected:
78
-
int ProtectedFunc(); // Declare a protected member.
78
+
int ProtectedFunc(); // Declare a protected member.
79
79
private:
80
-
int PrivateFunc(); // Declare a private member.
80
+
int PrivateFunc(); // Declare a private member.
81
81
};
82
82
83
83
// Declare two classes derived from BaseClass.
84
84
classDerivedClass1 : publicBaseClass
85
85
{
86
-
void foo()
87
-
{
88
-
PublicFunc();
89
-
ProtectedFunc();
90
-
PrivateFunc(); // function is inaccessible
91
-
}
86
+
void foo()
87
+
{
88
+
PublicFunc();
89
+
ProtectedFunc();
90
+
PrivateFunc(); // function is inaccessible
91
+
}
92
92
};
93
93
94
94
classDerivedClass2 : privateBaseClass
95
95
{
96
-
void foo()
97
-
{
98
-
PublicFunc();
99
-
ProtectedFunc();
100
-
PrivateFunc(); // function is inaccessible
101
-
}
96
+
void foo()
97
+
{
98
+
PublicFunc();
99
+
ProtectedFunc();
100
+
PrivateFunc(); // function is inaccessible
101
+
}
102
102
};
103
103
104
104
intmain()
105
105
{
106
-
DerivedClass1 derived_class1;
107
-
DerivedClass2 derived_class2;
108
-
derived_class1.PublicFunc();
109
-
derived_class2.PublicFunc(); // function is inaccessible
106
+
DerivedClass1 derived_class1;
107
+
DerivedClass2 derived_class2;
108
+
derived_class1.PublicFunc();
109
+
derived_class2.PublicFunc(); // function is inaccessible
0 commit comments