Click for Table of Contents
The C Primer
Book and Reference Collection
Copyright © 2024 by Rance D. Necaise
Table Of Contents
The C Primer
Copyright © 2024
Rance D. Necaise

3.2 If Statements

C like Python provides the if statement for making decisions, but the syntax is different. Consider the code segment below which illustrates a simple if statement:

float avg;
avg = (grade1 + grade2 + grade3) / 3.0;

if(avg >= 90)
  printf("Excellent Work!\n");

printf("Your average is %0.2f\n", avg);

If the condition is true, that is, the average is greater than 90, then the message Your grade is an A. will be printed. If the condition is false, the printf function will be skipped and the next statement following the if statement will be executed. The flowchart or control flow diagram in XYZ illustrates the branching behavior of the if statement.

if(condition)
statement
Page last modified on February 10, 2024, at 02:08 PM