Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

The GNU C Programming Tutorial - if... else...

Node:if... else..., Next:, Previous:if, Up:Decisions



if... else...

Let's review the basic form of the if... else... statement:

if (condition)
{
  compound statement
}
else
{
  compound statement
}

As with the bare if statement, there is a simplified version of the if... else... statement without code blocks:

if (condition) statement else statement;

When the if... else... is executed, the condition in parentheses is evaluated. If it is true, then the first statement or code block is executed; otherwise, the second statement or code block is executed. This can save unnecessary tests and make a program more efficient:

if (my_num > 0)
{
  printf ("The number is positive.");
}
else
{
  printf ("The number is zero or negative.");
}

It is not necessary to test my_num in the second block because that block is not executed unless my_num is not greater than zero.

 
 
  Published under free license. Design by Interspire