:float a=1.2; ==>
float a=1.2f; exp(a)*exp(3) ==> exp(a)*exp(3.0) ==>
exp(a)*exp(3.0f)std::exp(a)*std::exp(3.0f) int n1, n2;
...
for(unsigned int i=n1; i<=n2;
++i)unsigned int
n1, n2; for(int
i=n1;i<=n2;++i)for(unsigned int i=static_cast<unsigned
int>(n1);i<=static_cast<unsigned int>(n2);++i)float D, E;
...
if (D==E)
// Nur True falls jedes bit identisch ist.
// Das kann bei Gleitkommzahlen wegen Rundungsfehlern evtl.
nie eintretenif ( std::abs(D-E) < 1e-6 )
//
absolut nahe genug (auch 1e-5 o.ä.) if ( std::abs(D-E) <
0.5e-6*( std::abs(D)+std::abs(E)) ) // relativ nahe
genug bool is_palindrom(const string s)
{ ... }bool is_palindrom(const string&
s)
float d;
...
if ()
{ // d wird nur in diesem Scope
benutzt
d = ...
x = sqrt(d+2);
} // d wird außerhalb dieses Scopes
nicht mehr benutzt....
if ()
{
float d = ...
x = sqrt(d+2);
} }while(Bedingung = 0); }while(0 ==
Bedingung); // Sicherer: konstanter Ausdruck als
linker Operand (keine Zuweisung möglich)
int a=-2;
// outer scope variable
float b;
// outer scope variable
...
for (int a=0; ...) // inner scope
variable
{
double b;
... // inner scope variable
cout << a
<< b ;// inner
scope variables are used
}
cout << a << b
; // outer scope variables are valid
againvoid fkt(int a, float b)
// outer scope variables
{
int a;
// inner scope variable
double b;
...
// inner scope variable
cout << a
<< b
;
// inner scope variables are used
} int n;
....
int k5 =
floor(n/5);int
k5 =
static_cast<int>(floor(n/5));
int k5
= n/5; case 's': v =
partial_sum(m);
case 't': .... ;
case 's': v = partial_sum(m);
break;
case 't': .... ;break;[[fallthrough]];
gekennzeichnet werden (C++-17). if (i==1);
{...}
// als if-Zweig gedacht if (i==1) { };
{...}
// als if-Zweig gedacht gelesen, d.h. der "empty body" {} wird als Anweisungsblock des if-Zweiges genommen und der dafür gedachte Block wird unabhängig vom Test '1==i' stets ausgeführt.
Lösung: Weg mit dem ';' .
Aufpassen: Dasselbe passiert beifor (...) ;
else ;do ;