If-statement
The if statement is used to check a condition and if the condition is true, the statement in the if gets executed, if the condition is false, it does not enter the body of if.
Syntax:-
if(condition)
{
statement 1;
statement 2;
---------------
statement n;
}
NOTE:- No need to give parentheses as given in syntax of if, when only one statement is given in the body. Parentheses is given when more than one statement is in the body of if or else.
Example:-
Write a program to enter candidate age and check if his/her age is greater than 18.
#include<stdio.h>
void main()
{
int age;
printf("Enter age of the candidate:");
scanf("%d", &age);
if(age>18)
printf("Age of candidate is greater than 18");
}
Here, parantheses is not used after if because only one statement is in the body of if.
If-else:-
The if-else statement is used to carry a logical test and then take one of two possible actions depending on the outcomes of the test. If the condition gets true, the if satement gets executed otherwise the else statement executes.
Syntax:-
if(condition)
{
statement 1;
statement 2;
---------------
statement n;
}
else
{
statement 1;
statement 2;
---------------
statement n;
}
Example:-
Write a program to enter candidate age and check if his/her age is greater than 18 and print eligible for voting else print not eligible for voting.
#include<stdio.h>
void main()
{
int age;
printf("Enter age of the candidate:");
scanf("%d", &age);
if(age>18)
printf("Eligible for voting");
else
printf("Not eligible for voting");
}
QUESTIONS:-
- Write a program to enter a number and check whether it is even or odd.
- Write a program to enter length and breadth of a room and check if it's a rectangular or square room.
- Write a program to enter radius of circle and print perimeter if its radius is less than 5 otherwise print area of circle.
- Write a program to find maximum between given two numbers.
- Write a program to check whether a character is alphabet or not.
- Write a program to check whether a year is leap or not.
- Write a program to calculate profit or loss.
- Write a program to input any alphabet from the user and check whether it is consonant or vowel.
Nested if:-
Nested if statement means an if statement inside another if statement.
Syntax:-
if(condition1)
{
if(condition2)
{
body;
}
else
{
body;
}
}
else
{
body;
}
QUESTIONS:-
- Write a program to find maximum between three numbers.
- Write a program to find maximum between four numbers.
- Write a program to input id from the user. If the id is correct, ask for the password else print "INCORRECT USER ID". If id and pass inputted are correct print "LOGIN SUCCESSFUL" else print "INCORRECT PASSWORD".("id='A' and pass= 101").
- Write a program to input year and print check whether it is a leap or non leap year.
If-else-if ladder:-
This statement is used when multiple conditions are given. It checks the condition from top to bottom. When it finds the conditions true, it executes that part and the rest part is bypassesd. if none of the conditions are true, then the final else will be executed.
Syntax:-
if(condition)
{
body;
}
else if(condition)
{
body;
}
else if(condition)
{
body;
}
else
{
body;
}
QUESTIONS:-
- Write a program to check whether a number is positive, negative or zero.
- Write a program to input any character and check whether it is alphabet, digit or special character.
- Write a program to input week number and display week day.
- Write a program to input month number and display number of days in that month.
- Write a program to input sum of 5 subjects and display percentage and division.
- Write a program to input sides of triangle and display if it is a right-angled triangle using Pythagora's Theorem.
- Write a program to find maximum between three numbers.
Switch Case:-
The switch statement allows us to execute one code block among many alternatives. The case which will get true will get executed and if no condition matches then the control goes to the default part.
Keyword break is used to terminate the processing of a particular case within a switch statement. If it is not used then also the program will not display any error but we will understand it through an example.
Syntax:-
switch(integer expression)
{
case constant1:
do this;
break;
case constant2:
do this;
break;
case constant3:
do this;
break;
-----------------
default:
do this;
}
0 Comments