CourcesC++Chapter 3 in C++ 2014-2015 - Chapter Statements and flow control Introduction A simple C statement is each of the individual instructions of a

CourcesC++Chapter 3 in C++ 2014-2015 - Chapter Statements...

This preview shows page 1 - 4 out of 28 pages.

Chapter Statements and flow control Introduction: A simple C++ statement is each of the individual instructions of a program, like the variable declarations and expressions seen in previous sections. They always end with a semicolon (;), and are executed in the same order in which they appear in a program. But programs are not limited to a linear sequence of statements. During its process, a program may repeat segments of code, or take decisions. For that purpose, C++ provides flow control statements that serve to specify what has to be done by our program, when, and under which circumstances. Many of the flow control statements explained in this section require a generic (sub)statement as part of its syntax. This statement may either be a simple C++ statement, -such as a single instruction, terminated with a semicolon (;) - or a compound statement. A compound statement is a group of statements (each of them terminated by its own semicolon), but all grouped together in a block, enclosed in curly braces: {}: { statement ; statement ; statement ; } The entire block is considered a single statement (composed itself of multiple substatements). Whenever a generic statement is part of the syntax of a flow control statement, this can either be a simple statement or a compound statement. The if Statement The if statement can cause other statements to execute only under certain conditions. The programs you have written so far are like a "path" of execution for the program to follow. If a program has more than one "path" of execution, i.e. the program can execute some statements only under certain circumstances, we can use the if statement. In the next example,
Image of page 1
the user enters three test scores and the program calculates their average. If the average is greater than , the program congratulates the user on obtaining a high score. cin >> scorel >> score >> score ; average = (scorel + score + score ) / ; if (average > ) cout << "Congratulations! That's a high score!\n"; The general format of the simple if statement: if (expression) statement; The if statement is simple in the way it works: if the expression inside the parentheses is true, the next statement is executed. Otherwise, it is skipped. Be Careful with Semicolons Semicolons do not mark the end of a line, but the end of a complete C++ statement. The if statement isn't complete without the conditionally-executed statement that comes after it. So, you do not put a semicolon after the if (expression) portion of an if statement. If you put a semicolon after the if part, the compiler will assume you are placing a null statement (an empty statement that does nothing) there. if (expression); No semicolon allowed here statement; Comparing Floating Point Numbers You should be careful when using the equality operator (==) to compare floating point values. Because of round-off errors, a number that should be mathematically equal to another
Image of page 2
might not be. As an example, if is multiplied by , a decimal version of , we expect it to be . The program, however, disagrees. As it will gives instead of .
Image of page 3
Image of page 4

You've reached the end of your free preview.

Want to read all 28 pages?

  • Spring '16
  • mohamed attia
  • Control flow, loop

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes