0% found this document useful (0 votes)
18 views5 pages

Chapter 4 Control Structures Lecture 2

C++ programming

Uploaded by

sabahamza97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

Chapter 4 Control Structures Lecture 2

C++ programming

Uploaded by

sabahamza97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

IF ELSE STATEMENT

Q. What is the use of if-else statement? Write down its syntax and explain with example?
If-else statement
The if else statement allows making decision between two courses of action based on a
condition
Syntax
The following is the syntax of if else statement
If (condition)
{
Block of statements-1 // if the condition is true
}
else

{
Block of statements-2 // if the condition is false
}
Working:
1. The condition is evaluated.
2. If the result of evaluation is true. The first block of statements -1 is executed, the second
block of statements -2 is skipped and then control is transferred to the next statement.
3. If the condition evaluates to false, the first block of statements-1 is skipped and the second
block of statements -2, following the keyword else is executed
4. If there is a single statement to be executed whether the condition is true or false then
braces are not required.
Example:
The following program reads a number and prints whether it is even or odd numbers
#include<iostream.h>
#include<conio.h>
void main()
{
int n,r;
cout<<"\nEnter a number: ";
cin>>n;
r = n % 2;
If(r==0);
cout<<n<<"is even number":
else
cout<<n<< "is odd number":
getch();
}

Output of the Program


The following is the execution of the program
Enter a number: 15
15 is odd number
Explanation
When this program is executed it prompts the user to enter a new number. The number is
stored in variable n. The modulo operator (%) gives the remainder after division of the entered
number by 2 and it is stored in variable r. if the remainder r is equal to 0 then the program will
print n is even otherwise it is odd.
Braces are not used in if else statement in this program because a single statements is to be
executed whether the condition is true or false.
ELSE IF STATEMENT
Q What is the se of else if statement? Write down its syntax and explain with example?
Else if statement
The else if statement is used in situation where a decision is to be made from several
alternatives based on various conditions.
Syntax:
The following is the syntax of else if statement
If (condition-1)
{
Block of statements-1
}
else if (conditions -2)
{
Block of statements-2
}
else
{
Block of statements-n
}
Working:
1. The condition -1 is evaluated.
2. If it is true the Block of statements-1 is executed and control is transferred to the next
statement.
3. If the condition-1 is false then condition-2 is evaluated. If it is true then the Block of
statements -2 following condition-2 is executed.
4. In this manner, conditions are evaluated one by one. When any condition is true, the block of
statements following that condition is executed rest of the code is skipped and control is
transferred to the next statement.
5. If none of the conditions is true then the last block of statements following the keyword else
is executed the else block is optional.
6. If a single statement is to be executed instead of a block of statements then braces are not
required.
Example:
The following program reads a number and prints the message whether it is positive number
negative number or it is equal to zero.
#include<iostream.h>
#include<conio.h>
void main()
{
int n;
cout<<"\nEnter a number: ";
cin>>n;
If (n > 0)
Cout<<n<< "Is a positive number";
else if (n < 0)
Cout<<n<< "is negative number":
else
Cout<<n<<"is equal to zero";
getch();
}
Output of the Program
The following is the execution of the program
Enter a number: -6
-6 is negative number
[Link] a program that prints grade based on the marks obtained according to the given
scheme
Marks Grade
80 Marks ≤ 100 A
70≤ Marks ≤ 79 B
60 Marks ≤ 69 C
50 Marks ≤ 59 D
0≤ Marks ≤ 49 F

#include<iostream.h>
#include<conio.h>
void main()
{
int marks;
char grade;
cout<<"\nEnter the marks (max 100): ";
cin>>marks;
If (marks >=80)
grade = 'A';
else If (marks >=70)
grade = 'B';
else if (marks >=60)
grade = 'C';
else if (marks >=50)
grade = 'D';
else
grade = 'F';
cout<<:\n Your grade is" <<grade;
getch();
}
Output of the Program
The following is the execution of the program
Enter the marks: 74
Your grade is B

You might also like