Control Statements | if-else | nested if-else | switch case | #Tut-04
Points to be cover..
1. Statements and blocks
2. Control Statements
- If statements.
- Switch Statement.
- Conditional Operator Statement
Decision Making Statements...
1. if and if - else statement
Flowchart :
Here the conditon is checked and if the condition is true then and then only the body of if is executed otherwise the body of else is executed.
Syntax :
Code Snippet :
#include <stdio.h>
int main()
{
int a = 3, b = 2;
if (a > b)
{
printf("a is greater..\n");
}
else
{
printf("this is else condition..");
}
return 0;
}
Ouput :
this is else condition..
Points to remember..!💡
2. Nested if-else and else-if ladder
Flowchart :
Here the if - else condition is given
inside of if and else condition.
And we can have any number of
nested if else condition.
When we want to check for multiple
conditions then we make use of
nested if - else.
Code Snippet :
#include <stdio.h>
int main()
{
int data = 200;
int lecture = 0; // 1 means yes..
if (data >= 500)
{
if (lecture == 1)
{
printf("save your data..as there are your lectures..\n");
}
else
{
printf("download pushpa..\n");
}
}
else
{
if (data < 300)
{
printf("do nothing..\n");
}
else
{
printf("watch tutorial..");
}
}
return 0;
}
Output :
do nothing..
let us see else-if ladder..!
code snippet :
#include <iostream>
using namespace std;
// task for you is
// enter the age of children..
// if age > 18 --> go for adults parties
// if age == 18 --> you are eligible for student party
// if age < 18 and > 1 --> go for child parties
// if age == 0 --> not allowd for parties
// if age < 0 --> you deserve a special party
// here keep in mind that we need to add the else-if ladder
// inside of "else" condition only..💡
int main()
{
int age;
cout << "enter your age : ";
cin >> age;
if (age > 18)
{
cout << "you can vote..!" << endl;
}
else
{
if (age == 18)
{
cout << "you are just a year away from the eligible criteria.." << endl;
}
else
{
if (age == 0)
{
cout << "you are not borned yet.." << endl;
}
else
{
if (age < 18 && age >= 1)
{
cout << "you are kid now.." << endl;
}
else
{
if (age == -1)
{
cout << "you are not from this planet..😂" << endl;
}
}
}
}
}
return 0;
}
output :
enter your age : 0
you are not borned yet..
3. Switch Case Statements..
syntax :
The switch case statement is the best alternative for the else-if ladder.
- Here we use switch keyword
- the break keyword is must
- we can have number of cases and statements.
- the default case is optional.
- we can pass only integer and character type of arguments to the switch case.
Code Snippet :
#include<stdio.h>
int main()
{
int age;
printf("enter the age : ");
scanf("%d", &age);
switch (age)
{
case 18:
printf("You can vote..");
break;
case 0:
printf("you are not bored yet..");
break;
case 16: // this is a blank case and it is allowed
case 20:
printf("you can vote");
break;
case -1:
printf("you deserve special party..");
break;
default:
printf("no any case is matched..");
}
return 0;
}
output :
enter the age : 18
You can vote..
Points to remember..!💡
Comments
Post a Comment