You are here:   HomeC Programming (Theory)Control Structures in CWrite a C program to show working of continue.
by Dinesh Thakur Category: Control Structures

if (marks >= 0 && marks <= 100)

    printf("Valid marks");

    else printf("Invalid marks");

This program segment uses an if-else statement to test whether the marks entered are valid or not and print the appropriate message. It is assumed that for marks to be valid, it should be in the range 0 to 100, i.e., greater than or equal to zero and less than or equal to 100. We may rewrite the above code by using a test for invalid marks. The value of marks is invalid if it is outside the range 0 to 100, i. e., less than 0 or greater than 100. The if-else statement using such a test is given below.

if (marks < 0 || marks > 100)

    printf("Invalid marks");

    else printf("Valid marks");

Note that there is also another way to express the validity of marks: it should not be invalid, i. e., it should not be outside the range 0 to 100. This logic is used in the if statement given below.

if (! (marks < 0 || marks > 100))

      printf("Valid marks");

      else printf("Invalid marks");

Example: Validity of marks in a single subject

 

#include <stdio.h>

void main()

{

        int marks;

        clrscr();

        printf("Enter the Marks :\n");

        scanf("%d",&marks);

        if (marks >= 0 && marks <= 100)

           printf("Valid marks");

           else printf("Invalid marks");

           getch();

}



Subscribe To Free Daily Newsletter!

Get Free News Updates Delivered Directly To Your Inbox
About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to contact us.



What's New and Popular





Popular Article