Fifth Chapter Lesson-14: Conditional Control Statements in C language

At the end of this lesson-

  • 1. You will be able to explain control statement. 
  • 2. You will be able to explain different control statements. 
  • 3. You will be able to explain conditional control statement details. 
  • 4. You will be able to write program using conditional control statement. 

 

Control statements: A control statement is a statement that determines whether other statements will be executed. Control statements control the flow of program. They make it possible to make decisions, to perform tasks repeatedly or to jump from one section of code to another.

Control Statements are:

  • 1. Conditional Control Statements / Decision control statements
  • 2. Loop Control Statements
  • 3. Jumping Control Statements

 

Decision control statements: In decision control statements, group of statements are executed when condition is true.  If condition is false, then else part statements are executed. Decision control statements are:

  • if statements
  • if-else statements
  • else if statements
  • nested if-else statements
  • switch statements

 

if statements: It is used to decide whether a certain statement or block of statements will be executed or not. The ‘if’ statement evaluates the condition inside the parenthesis. If the condition is true statement(s) inside the body of ‘if’ is executed. If the condition is false statement(s) inside the body of ‘if’ is skipped from execution. Syntax of ‘If’ statement:

if(condition)
{
  statement(s)
}

Program for determining a number is positive.

#include<stdio.h>
#include<conio.h>

main()
{
    int a;
    printf("Enter a number");
    scanf("%d",&a);
    if(a>0)
        printf("The given number is Positive");
    getch();
}

 

if-else statements: In if-else control statement, group of statements are executed when condition is true. If condition is false, then else part statements are executed. There is no condition for else statement. Syntax of ‘if-else’ statement:

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

Program for determining a number is whether positive or negative.

#include<stdio.h>
#include<conio.h>

main()
{
    int a;
    printf("Enter a number");
    scanf("%d",&a);
    if(a>=0)
        printf("The given number is Positive");
    else
        printf("The given number is Negative");
    getch();
}

 

আরো পড়ুন ::  Computer Network : PAN, LAN, CAN, MAN, WAN

else if statements: The else..if statement is useful when you need to check multiple conditions within the program, nesting of if-else blocks can be avoided using else..if statement. Syntax of else..if statement:

if(Condition1) 
{ 
  statement(s)
} 
else if(Condition2) 
{  
  statement(s)
} 
  …………… 
  ……………

else
{ 
  statement(s)
}

 

Program for determining a number is whether zero, positive or negative.

#include<stdio.h>
#include<conio.h>

main()
{
    int a;
    printf("Enter a number");
    scanf("%d",&a);
    if(a==0)
        printf("The given number is Zero");
    else if(a>0)
        printf("The given number is Positive");
    else
        printf("The given number is Negative");
    getch();
}

 

nested if-else statements: It is possible to include if-else statement(s) inside the body of another if-else statement. Syntax of nested if-else:

if(Condition1) 
{ 
  if(Condition2) 
  { 
     statement(s)
  } 
}

 

Program for determining a year whether leap year or not.

#include<stdio.h>
#include<conio.h>

main()
{
    int y;
    printf("Enter a year");
    scanf("%d",&y);
    if(y%4==0)
    {
      if(y%100!=0)
         printf("The given year is a leap year");
    }
    else if(y%400)
        printf("The given year is a leap year");
    else
        printf("The given year is not a leap year");
    getch();
}

 

switch statements: The switch statement is a multiway branch statement. A switch statement tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed. If a case match is found, then the default statement is executed, and the control goes out of the switch block. Syntax of switch statement:

switch(expression)
{
 case constant_1:
     statements_for_constant_1;
     break;

 case constant_2:
     statements_for_constant_2;
     break;
------------------
------------------
 case constant_n:
     statements_for_constant_n;
     break;

 default: // Default case is optional
     default_statements;
     break;
}

 

The following rules apply to a switch statement: 

  • 1. Case constants must be either char(‘A’, ‘B’../ ‘a’,’b’…/’+’,’-‘ , etc) or int(1,2,3 – -) type. If it is char type it should be enclosed with single quotation(‘ ‘).
  • 2. Case constants must be unique.
  • 3. Case constants must end with a colon ( : ).
  • 4. A break keyword must be present in each case.
  • 5. It can be same code block for multiple cases.
  • 6. There can be only one default label.
আরো পড়ুন ::  Boolean Algebra, Boolean Postulates and Boolean Theorems

 

Example-1: Program using switch statement that can perform addition, subtraction, multiplication, division.

#include<stdio.h>
#include<conio.h>

main()
{
    char ope;
    int a,b;
    printf("Enter Operator Either + or - or * or /");
    scanf("%c",&ope);
    printf("Enter Two numbers");
    scanf("%d %d",&a,&b);

    switch(ope)
    {
    case '+':
        printf("Sum=%d",a+b);
        break;
    case '-':
        printf("Subtraction=%d",a-b);
        break;
    case '*':
        printf("Multiplication=%d",a*b);
        break;
    case '/':
        printf("Quotient=%d",a/b);
        break;
    default:
        printf("Your Operation is not matched");
        break;
    }
    getch();
}

 

Example-2: Write a program using switch statement to determine a letter is whether vowel or consonant.

 

Lesson Evaluation-

Knowledge Based Questions:

  • a. What is control statement?
  • a. What is conditional control statement?
  • a. What is switch statement?

Comprehension Based Questions:

  • a. When switch statement is more convenient to use?

Creative Questions:

Multiple Choice Questions:

 


Written by,

Spread the love

One thought on “Fifth Chapter Lesson-14: Conditional Control Statements in C language

Leave a Reply

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