Lecture 9 – Decisions (if-else)


Lecture 9 – Decisions (if-else)


if-else structure


We have another construct with which we can achieve similar functionality. That is if-else.

Syntax


if ( condition )

{

//Statements if the condition is true

}

else

{

//Statements if the condition is false

}

// This program demonstrates the use of multiple if’s

# include <stdio.h>




Output

Please enter your marks: 75

Congratulations! You’ve passed
# include <conio.h>




void main()

{

int marks;




Output

Please enter your marks: 50

Congratulations! You’ve passed

 




Output

Please enter your marks: 25

Sorry! You didn’t make it :(

clrscr();                                                                                                

printf(“\nPlease enter your marks: ”);

scanf(“%d”, &marks);

if(marks>=50)

{

printf(“\nCongratulations! You’ve passed”);

}

else

{

printf(“\nSorry! You didn’t make it :(”);

}

getch();

}

Example 18 – Use of multiple if-else

Comments

Popular posts from this blog

Lecture 17 – Functions (continued)

Lecture 6 – Operators in C (continued)

Lecture 10 – Decisions (if-else-if)