C Control Statements

C supports an if...else statement that works a lot like the Visual Basic If...Else statement. Table 13.5 lists the relational operators with which you can com pare data.

Table 13.5. C Relational Operators
Relational Operator Description
== Equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
!= Not equal to

Use braces to group the body of an if statement as shown in the following statement:

if (age < 18)
 { printf("You cannot vote yet\n");
   yrs = 18 - age;
   printf('You can vote in %d years.\n", yrs);
 }
else
{
   printf("You can vote.\n");
}

The individual statements in the if statement end with semicolons, but not the if. The if...else spans several lines, so you don't put semicolons after ...

Get Absolute Beginner's Guide to Programming, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.