C++ Crash Course: a fast-Paced Introduction



Yüklə 7 Mb.
Pdf görüntüsü
səhifə50/71
tarix20.09.2023
ölçüsü7 Mb.
#145939
1   ...   46   47   48   49   50   51   52   53   ...   71
C Crash Course A Fast-Paced Introduction by Josh Lospinoso

15
Conditional Statements
Conditional statements allow you to make decisions in your programs. These 
decisions rest on Boolean expressions, which evaluate to true or false. For 
example, you can use comparison operators, such as “greater than” or “not 
equal to,” to build Boolean expressions. 
Some basic comparison operators that work with 
int
types appear in 
the program in Listing 1-2.
int main() {
int x = 0;
42 == x; // Equality
42 != x; // Inequality
100 > x; // Greater than
123 >= x; // Greater than or equal to
-10 < x; // Less than
-99 <= x; // Less than or equal to
}
Listing 1-2: A program using comparison operators
This program produces no output (compile and run Listing 1-2 to verify 
this). While the program doesn’t produce any output, compiling it helps to 
verify that you’ve written valid C++. To generate more interesting programs, 
you’d use a conditional statement like 
if
.
An 
if
statement contains a Boolean expression and one or more nested 
statements. Depending on whether the Boolean evaluates to true or false, 
the program decides which nested statement to execute. There are several 
forms of 
if
statements, but the basic usage follows:
if
(
u
boolean-expression

v
statement
If the Boolean expression 
u
is true, the nested statement 
v
executes; 
otherwise, it doesn’t.
Sometimes, you’ll want a group of statements to run rather than a 
single statement. Such a group is called a compound statement. To declare a 
compound statement, simply wrap the group of statements in braces 
{ }

You can use compound statements within 
if
statements as follows:
if (
u
boolean-expression
) { 
v
statement1
;
statement2
;
--
snip
--
}
If the Boolean expression 
u
is true, all the statements in the compound 
statement 
v
execute; otherwise, none of them do.


16
Chapter 1
You can elaborate the 
if
statement using 
else if
and 
else
statements. 
These optional additions allow you to describe more complicated branch-
ing behavior, as shown in Listing 1-3.
u
if (
boolean-expression-1

statement-1
v
else if (
boolean-expression-2

statement-2
w
else 
statement-3
Listing 1-3: An 
if
 statement with 
else if
 and 
else
 branches
First, 
boolean-expression-1
u
is evaluated. If 
boolean-expression-1
is true, 
statement-1
is evaluated, and the 
if
statement stops executing. If 
boolean 
-expression-1
is false, 
boolean-expression-2
v
is evaluated. If true, 
statement-2
is evaluated. Otherwise, 
statement-3
w
is evaluated. Note that 
statement-1

statement-2
, and 
statement-3
are mutually exclusive and together they cover 
all possible outcomes of the 
if
statement. Only one of the three will be 
evaluated.
You can include any number of 
else if
clauses or omit them entirely. As 
with the initial 
if
statement, the Boolean expression for each 
else if
clause 
is evaluated in order. When one of these Boolean expressions evaluates to 
true
, evaluation stops and the corresponding statement executes. If no 
else 
if
evaluates to 
true
, the 
else
clause’s 
statement-3
always executes. (As with 
the 
else if
clauses, the 
else
is optional.)
Consider Listing 1-4, which uses an 
if
statement to determine which 
statement to print.
#include
int main() {
int x = 0; 
u
if (x > 0) printf("Positive.");
else if (x < 0) printf("Negative.");
else printf("Zero.");
}
Zero.
Listing 1-4: A program with conditional behavior
Compile the program and run it. Your result should also be 
Zero
. Now 
change the 
x
value 
u
. What does the program print now?
N O T E
 
Notice that 
main
 in Listing 1-4 omits a return statement. Because 
main
 is a special 
function, return statements are optional.

Yüklə 7 Mb.

Dostları ilə paylaş:
1   ...   46   47   48   49   50   51   52   53   ...   71




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin