Introduction

The if Statement

Syntax Diagram
Figure4.2 Syntax Diagram of if-else
Control-Flow Diagram
Figure 4.3 Control Flow Diagram of if-else

Equality and Assignment Operators

Multiple Statements within if

          #include<stdio.h>
          int main()
          {
            int num=19;
            if(num < 10)
            {	
              printf("Number is %d.\n",num);
              printf("The value is less than 10");
            }
            else
            {	
              printf("The value is greater than 10\n");
              printf("Number is %d.\n",num);
            }
            return 0;
          }
      
        Output:
        The value is greater than 10
        Number is 19.
        

The if-else Statement

Syntax Diagram of if-else
Figure4.2 Syntax Diagram of if-else
Figure4.3
Figure 4.3 Control Flow Diagram of if-else
      Illustration 4.1
          void main() {
          int marks;
          scanf("%d", &marks);
          if (marks >= 80)
            printf ("A") ;
          else if (marks >= 70)
            printf ("B") ;
          else if (marks >= 60)
            printf ("C") ;
          else printf ("Failed") ;
      }
      

Nested if-else

      Illustration 4.2
      int main () 
        {
        int a = 1;
        if( a < 10) 
        {
            if( a == 1) 
          {
              printf("Value of a is:%d\n",a);
            }
          else
          {
          printf("The value is greater than 100.");
          }
        }
        else
        {
        printf("The value is greater than 10.");
        }
        return 0;
      }
        
        Output:
Value of a is:1

The Terniary Operator ?:

This makes use of an expression that is either non-0 or 0.
An appropriate value is selected,depending on the value of the expression

Syntax:-

Condition ? True Statement : False Statement

Note:if condition will true then true statement will be executed else false statement will be executed.

Examples:
#include<stdio.h>
void main()
{
int marks=80;
if (marks >= 60)
printf("Passed \n");
else printf("Failed \n");
return 0;
}
#include<stdio.h>
void main()
{
int marks=80;
(marks >= 60) ? printf("Passed \n") : printf("Failed \n");
}
Output:Passed Output:Passed
#include<stdio.h>
void main()
{
int a=5; int b=3;
if((a > 10) && (b < 5))
x = a + b;
else x = a-b;
printf("x=%d",x);
return 0;
}
void main()
{
int a=5;
int b=3;
x = ((a > 10) && (b < 5)) ? a + b : a-b
printf("x=%d",x);
}
Output:x=2 Output:x=2

The switch statement

Behaviour of switch

Example

Figure4.4
Incorrect Way
Figure4.5
Correct Way

The break statement

Chapter 4 Quiz

Q1).Which loop is faster in C Language, for, while or Do While?
Q2).What will be the correct syntax for running two variables in for loop simultaneously?
Q3).In the context of "break" and "continue" statements in C, pick the best statement.
Q4).In _______, the bodies of the two loops are merged together to form a single loop provided that they do not make any references to each other.
Q5).If switch feature is used, then
Home Page
Top of Page
Go Next
Go Next