Simple Conditional, Nested Conditional

conditional is an if … else statement. For example:

If you study, then you will succeed, or else you will fail. 

 

This example has three parts:

  1. the condition: If you study,
  2. if the condition is met: then you will succeed,
  3. if the condition is not met: or else you will fail.

Notice that there are different outcomes, whether the condition is met or not. This is the essence of a conditional.

Simple Conditional

A simple conditional consists of an if statement and an else statement.

 

Here is an example of a simple conditional. Notice the condition, the condition is met, and thecondition is not met:

#include <stdio.h>
void main( )
{
char answer;
printf(“\nAfter C, do you want to learn Perl (y or n)? “);
scanf(” %c”, &answer);
if(answer == ‘y’) 

printf(“\nGood, because I’m developing a Perl tutorial.”);

else

printf(“\nYou really should learn C++ next.”);

}

 

There are many important things to recognize in this program. First, look at the format specifier in the scanf line. There is a space before the %c. Whenever you are using a conditional with the %c format specifier, put a space before the %c.

Second, in the condition statement, we use a double equal sign (==). Whenever you are inside a conditional, you must use == to mean “equals”. In this case, we are saying “if the answer equals ‘y’ (yes)”.

Next, look at the statement if the condition is met: printf(“\nGood, because I’m developing a Perl tutorial.”);. Hit the Tab key on your keyboard to indent this statement. Why is this tabbed in? It is C convention, which means that this is the correct style for writing in C language. Really, though, it helps tremendously when you have to look through a long program and find errors. Believe me, if we wrote every line against the left margin, it would be nearly impossible to find errors in conditional statements — or even know where a conditional began or ended!

Then, the else statement is aligned with the if statement, and the statement printf(“\nYou really should learn C++ next.”); is tabbed in like the previous printf statement.

Also notice that there is a space before the %c in line 6. We add a space before %c in programs with conditionals and loops.

Type this source code in your editor and save it as cond.c then compile it, link it, and run it. When you run the program, answer the question with ‘y’ first. Then run the program again and test the ‘n’ answer. Both should work properly. If not, go back and check your code for typing errors. This is called WHITE BOX TESTING.

Nested Conditional

A nested conditional adds a second condition inside a first condition.

 

Here is an example of a nested conditional. Notice the second condition:

#include<stdio.h>
void main( )
{
int birth;
printf(“What year were you born? “);
scanf(“%d”, &birth);
if(birth < 1980)

printf(“\nYou must remember the Reagan years.”);
if(birth < 1960)
{

printf(“\nYou must also remember the Kennedy years.”);

 

}

}
else

printf(“\nYou are very young.”);

}

 

In this example, we ask what year the person was born, then we test the answer. If the person was born before 1980, we print “You must remember the Reagan years.” Then we test to see if the person was also born before 1960. This is the nested conditional. If the person is, then we say “You must also remember the Kennedy years.”

If the person was born after 1980, we simply say “You are very young.” The age is not tested by the nested conditional.

Type this source code in your editor and save it as nest.c then compile it, link it, and run it.

NOTE: Keep track of your curly braces { }. Make sure that if you open a curly brace, you close it. It is easy to forget them, so at the end of your script, count how many curly braces you have. If you don’t get an even number, then you missed one. Go back and add it in the correct place.

Continue on to Special Conditional Operators.

Published by Michael Boguslavskiy

Michael Boguslavskiy is a full-stack developer & online presence consultant based out of New York City. He's been offering freelance marketing & development services for over a decade. He currently manages Rapid Purple - and online webmaster resources center; and Media Explode - a full service marketing agency.

Join the Conversation

2 Comments

Leave a comment

Your email address will not be published. Required fields are marked *