PrevUpHomeNext

day-18: c++ condition, loop


if-statement - [c++ if-conditions]

if is a c++ condition statement.

Syntax:

if (<<condition-expression>>)
{
	<<evaluate-statement-1>>;
	<<evaluate-statement-2>>;
	// ... more statements
}
else
{
	<<evaluate-statement-1>>;
	<<evaluate-statement-2>>;
	// ... more statements
}

c++ example:

int x;
x = 4;
if (x == 4)
{
	std::cout << "x is 4" << std::endl;
}
else
{
	std::cout << "x is not 4" << std::endl;
}

Because x is 4, so x == 4 is true, so std::cout << "x is 4" << std::endl; will be executed, std::cout << "x is not 4" << std::endl; will be not executed.

switch branches - [c++ if-conditions]

switch is another c++ condition statement, switch is a more powerful if

Syntax:

switch (<<value-expression>>)
{
case <<value1>>:
	<<evaluate-statement-1>>;
	break;
case <<value2>>:
	<<evaluate-statement-2>>;
	break;
// more case ...
// more case ...
default:
	<<evaluate-statement-N>>;
	break;
}

c++ three-op-operator :? [c++ if-conditions]

three-op-operator :? is also another c++ if-condition. It is more short for value judgement.

?:

Syntax:

auto result = <<condition>> ? <<expression-1>> : <<expression-2>> ;

c++ example:

bool x = 3;
std::string result = (x==3)?"true":"false";

for-loop - [c++ loops]

c++ for loop

Syntax:

// for-loop:
for (<<init-statement>>; <<judge-condition>>; <<change-expression>>)
{ // loop-body begins
	<<evaluate-statement-1>>;
	<<evaluate-statement-2>>;
	// ... more statement
	// ... more statement
} // loop-body ends

Run orders:

c++ example:

for (int i=0; i<5; ++i)
{
	std::cout << "i is " << i << std::endl;
}

c++ Range-based for loop - [c++ loops]

c++ range-based for loop can be also called c++ for each.

Syntax:

for (<<declare-a-variable>>: <<initializing-value-list>>)
{ // loop-body begins
	<<evaluate-statement-1>>;
	<<evaluate-statement-2>>;
	// ... more statement
	// ... more statement
} // loop-body ends

c++ example:

for (std::string str: {"c++", "world", "hello world"})
{
	std::cout << "str is " << str << std::endl;
}

c++ while-loop [c++ loops]

c++ while loop

Syntax:

while (<<judge-condition>>)
{ // loop-body begins
	<<evaluate-statement-1>>;
	<<evaluate-statement-1>>;
	// ... more statement
	// ... more statement
} // loop-body ends

c++ example:

int x = 0;
while (x < 10)
{
	std:cout << "x is " << x << std::endl;
	++x;	// change-statement
}

do-while-loop - [c++ loops]

c++ do-while loop

Syntax:

do
{
	<<evaluate-statement-1>>;
	<<evaluate-statement-2>>;
	// ... more statement
	// ... more statement
}
while (<<judge-condition>>);

c++ example:

int i = 0;
do
{
	std::cout << "i is " << i << std::endl;
	++i;
}
while (i<10);







Written on Dec 06, 2024

Back to index

Index

cpp/c++

c++ std::exception:

std::cout.write(err.data(), err.size());

std::cout << std::endl;

caught:

  ===================================
  #  The c++ programming language.  #
  #                                 #
  #  Join c++ Discord: yZcauUAUyC   #
  #  Deck                           #
  ===================================

Home: cppfx.xyz

K


PrevUpHomeNext