Piece of Information - Conditional Statements
Main | Registration | Login | RSSSaturday, 2016-12-10, 11:21 PM

PiEcE oF InFoRmAtIoNs

Site menu
Search
Statistics

Total online: 1
Guests: 1
Users: 0

Conditional Statements

Conditional statements are statements that check an expression then may or may not execute a statement or group of statements depending on the result of the condition.

In Turbo C the following are the types of conditional statements:


THE IF STATEMENT

Syntax of if statementSyntax of if statement with block statement

if (expression)
statement;

if (expression)
{
statement_sequence;
}


Where:

if - is a reserve word in Turbo C

expression - is relational or boolean expression that evaluates to a TRUE (1) or FALSE (0) value

statement - may either be a single Turbo C statement or a block of Turbo C statements


In an if statement, if the expression evaluates to TRUE (1), the statement or the block of statements that forms the target of the if statement will be executed. Otherwise, the program will ignore the statement or the block of statements.


*** Example 1

Write a program that determines if the input age is qualified to vote.

Source code
--------------------------------------------------
#include <stdio.h>
main()
{
     int age;
     clrscr();

     printf("Enter age: ");
     scanf("%d", &age);

     if (age >= 18)
          printf("Qualify to vote.");

     getch();
}
--------------------------------------------------

Output

Enter age: 18

Qualify to vote


*** Example 2

Write a program that will ask for a price. If the price is greater than 1000, compute a 10% discount from the original price. Display the computed discount.

Source code
--------------------------------------------------
#include <stdio.h>
main()
{
     float price, discount;
     clrscr();

     printf("Enter value for price: ");
     scanf("%f", &price);

     if (price >= 1000)
     {
          discount = price * 0.10;
          printf("Discount is %.2f", discount);
     }

     getch();
}
--------------------------------------------------

Output

Enter value for price: 2000

Discount is 200.00

NOTE: A format specifier %.2f can be used to limit the output being displayed into two decimal places only.


THE IF-ELSE STATEMENT

Syntax of if-else statementSyntax of if-else statement with block statement
if (expression)
statement_1;
else
statement_2;
if (expression)
{
statement_1;
}
else
{
statement_2;
}


*** Example 1

Write a program that determines if the input age is qualified to vote or not. The qualifying age is 18 years old and above.

Source code
--------------------------------------------------
#include <stdio.h>
main()
{
     int age;
     clrscr();

     printf("Enter age: ");
     scanf("%d", &age);

     if (age >= 18)
          printf("Qualify to vote.");
     else
          printf("Too young!");

     getch();
}
--------------------------------------------------

Output

Enter age: 10

Too young


*** Example 2

Write a program that displays an equivalent color once an input letter match its first character. For example b for Blue, r for Red, and so on. Here are the given criteria:

Letters                 Color
'B' or 'b'              Blue
'R' or 'r'               Red
'G' or 'g'              Green
'Y' or 'y'              Yellow
other letters        "Unknown Color"

Source code
--------------------------------------------------
#include <stdio.h>
main()
{
char Let;
clrscr();

printf("Enter a letter: ");
scanf("%c", &Let);

if((Let == 'B') || (Let == 'b'))
printf("Blue");
else if((Let == 'R') || (Let == 'r'))
printf("Red");
else if((Let == 'G') || (Let == 'g'))
printf("Green");
else if((Let == 'Y') || (Let == 'y'))
printf("Yellow");
else
printf("Unknown Color");

getch();
}
--------------------------------------------------

Output

Enter a letter: G

Green


*** Example 3

Write a program to assist a teacher in calculating student grades at the end of the semester. It accepts a numerical grade as output, based on the given scale:

Range                Grade
90 and above         A
80 – 89                 B
70 – 79                 C
60 – 69                 D
below 60               F


Source code - 1
--------------------------------------------------
#include <stdio.h>
main()
{
int g;
clrscr();

printf("Enter grade: ");
scanf("%d", &g);

if(g >= 90)
printf("A");
else if(g >= 80)
printf("B");
else if(g >= 70)
printf("C");
else if(g >= 60)
printf("D");
else
printf("F");

getch();
}
--------------------------------------------------

Output
Enter grade: 90

A


Source code - 2
--------------------------------------------------
#include <stdio.h>
main()
{
int g;
clrscr();

printf("Enter grade: ");
scanf("%d", &g);

if((g >= 90) && (g <= 100))
printf("A");
else if((g >= 80) && (g <= 89))
printf("B");
else if((g >= 70) && (g <= 79))
printf("C");
else if((g >= 60) && (g <= 69))
printf("D");
else if((g >= 50) && (g <= 59))
printf("F");
else
printf("Out-Of-Range");

getch();
}
--------------------------------------------------

Output
Enter grade: 90

A


THE SWITCH-STATEMENT

The switch statement is a multiple-branch decision statement.

Syntax of the switch statement
switch (variable)
{
case constant1:
statement_sequence_1;
break;

case constant2:
statement_sequence_2;
break;

default:
statement_sequence_default;
}

Where:

switch, case and default is a reserve word in Turbo C

In a switch statement, a variable is successively tested against a list of integer or character constants. If a match if found, a statement or block of statements is executed. The default part of the switch is executed if no matches are found.



*** Example 1

Write a program to assist a teacher in calculating student's grade at the end of the semester. It accepts numerical grade as input data then it will display the equivalent character grade as output based on the given scale:

Range               Grade
90 and above       A
80 – 89               B
70 – 79               C
60 – 69               D
50 – 59               F
Others       "Out-Of-Range"


Source code - 2
--------------------------------------------------
#include <stdio.h>
main()
{
int g;
clrscr();

printf("Enter grade: ");
scanf("%d", &g);

switch(g)
{
case 90: case 91: case 92: case 93: case 94:
case 95: case 96: case 97: case 98: case 99:
case 100: 
printf("A"); break;

case 80: case 81: case 82: case 83: case 84:
case 85: case 86: case 87: case 88: case 89:
printf("B"); break;

case 70: case 71: case 72: case 73: case 74:
case 75: case 76: case 77: case 78: case 79:
printf("C"); break;

case 60: case 61: case 62: case 63: case 64:
case 65: case 66: case 67: case 68: case 69:
printf("D"); break;

case 50: case 51: case 52: case 53: case 54:
case 55: case 56: case 57: case 58: case 59:
printf("F"); break;

default: printf("Out-Of-Range"); break;
}
getch();
}
--------------------------------------------------

Output

Enter grade: 89

B


Programming Exercises
  • Write a program that determines if the input letter is a VOWEL or CONSONANT. The vowels are: A E I O U. Your program must be able to handle a capital or small input letter.
1st solution: use the laddereized if-else-if conditional statements
2nd solution: use the switch conditional statement

  • Write a program that will ask the user to input 2 integers and a character (A, S, M or D). if the user inputs ‘A’ for the character, add the 2 numbers, if ‘S’ subtract the 2 numbers, if ‘M’ multiply, and if ‘D’ divide the numbers. Output the computed value.
1st solution: use the laddereized if-else-if conditional statements
2nd solution: use the switch conditional statement

  • Write a program that accepts dates written in numerical form and then output them as a complete form. For example, the input:
Enter month: 2
Enter day: 26
Enter year: 1986

February 26, 1986

1st solution: use the laddereized if-else-if conditional statements
2nd solution: use the switch conditional statement
Login form
Calendar
«  December 2016  »
SuMoTuWeThFrSa
    123
45678910
11121314151617
18192021222324
25262728293031
Site friends
CLICK BELOW THE SITE YOU WANT TO GO:

Create your own site

YAHOO Mail

FACEBOOK

GOOGLE

FRIENDSTER

Our poll
Rate my site
Total of answers: 25

Copyright MyCorp © 2016
Make a free website with uCoz