Home > Articles > Programming > C/C++

  • Print
  • + Share This
This chapter is from the book

Summary

Relational operators compare two values to see whether they're equal, whether one is larger than the other, and so on. The result is a logical or Boolean (type bool) value, which is true or false. False is indicated by 0, and true by 1 or any other non-zero number.

There are three kinds of loops in C++. The for loop is most often used when you know in advance how many times you want to execute the loop. The while loop and do loops are used when the condition causing the loop to terminate arises within the loop, with the while loop not necessarily executing at all, and the do loop always executing at least once.

A loop body can be a single statement or a block of multiple statements delimited by braces. A variable defined within a block is visible only within that block.

There are four kinds of decision-making statements. The if statement does something if a test expression is true. The if...else statement does one thing if the test expression is true, and another thing if it isn't. The else if construction is a way of rewriting a ladder of nested if...else statements to make it more readable. The switch statement branches to multiple sections of code, depending on the value of a single variable. The conditional operator simplifies returning one value if a test expression is true, and another if it's false.

The logical AND and OR operators combine two Boolean expressions to yield another one, and the logical NOT operator changes a Boolean value from true to false, or from false to true.

The break statement sends control to the end of the innermost loop or switch in which it occurs. The continue statement sends control to the top of the loop in which it occurs. The goto statement sends control to a label.

Precedence specifies which kinds of operations will be carried out first. The order is unary, arithmetic, relational, logical, conditional, assignment.

  • + Share This
  • 🔖 Save To Your Account