0% found this document useful (0 votes)
102 views4 pages

Lab 09 Conditional Control 1

The document discusses conditional statements in C++, including if statements, if-else statements, and multiple if-else-if statements. It provides the syntax and examples of each. The objective is to get familiar with these conditional structures. It also provides exercises to write programs using these conditional statements, such as determining if a number is even or odd, calculating grades based on percentage ranges, finding the maximum of three numbers, and presenting area formulas based on a selected shape.

Uploaded by

MUHAMMAD HAMZA
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)
102 views4 pages

Lab 09 Conditional Control 1

The document discusses conditional statements in C++, including if statements, if-else statements, and multiple if-else-if statements. It provides the syntax and examples of each. The objective is to get familiar with these conditional structures. It also provides exercises to write programs using these conditional statements, such as determining if a number is even or odd, calculating grades based on percentage ranges, finding the maximum of three numbers, and presenting area formulas based on a selected shape.

Uploaded by

MUHAMMAD HAMZA
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
You are on page 1/ 4

Experiment # 09

OBJECTIVE:

To experiment with conditional structures in C++

a) To be familiar with if statement.


b) To be familiar with if else statement.
c) To be familiar with Multiple if –else-if structure.

Theory:

If statement:

If is a keyword in C++ programming language. If statement is a decision making statement. It is


the simplest form of selection constructs. It is used to execute or skip a statement or set of
statements by checking a condition.

The condition is given as a relational expression. If the condition is true, the statement or set of
statements after if statement is executed. If the condition is false, the statement or set of
statements after if statement is not executed.

Syntax:

If (condition)
Statement(s);

Example:

Write a program that inputs marks and displays”congratulations! You have passed.” if the
marks are 40 or more.

#include<iostream>
void main( )
{
Int marks;
Cout<<”enter your marks:”;
Cin>>marks;
If(marks>=40)
Cout<<”congratulation! You have passed.”;
}
The … if else statement:

The if … else statement is used to carry out a logical test and then take one of two possible
actions, depending on the outcome of the test (i.e., whether the outcome is true or false).

The else portion of the if else statement is optional. Thus in its simplest general form, the
statement can be written as

If (expression) statement

The expression must be placed in the parenthesis, as shown above. In this form, the statement
will be executed only if the expression has a nonzero value (i.e. if expression is true. If the
expression has a value zero (i.e. if the expression is false then the statement will be ignored.

The general form of an if statement which include the else clause is:

if (expression) statement1 else statement2

If the expression has a nonzero value (i.e. if expression is true), then statement1 will be execute,
otherwise statement2 will be execute.

Syntax:

If(condition)
Statement;
else
Statement;

Example:
// A program for finding the smallest number
#include<iostream>
void main()
{
int a=2,b=3,small;
if(a<b)
small=a;
else
small=b;
cout<<small;}
Multiple if … else … if structure
This statement can be used to choose one block of statements from many blocks of statements. It
is used when there are many options and only one block of statements should be executed on the
basis of a condition.

Syntax:
if(condition)
{
Block 1;
}
Else if(condition)
{
Block 2;
}
Else if(condition)
{
Block 3;
}
.
.
.
Else
{
Block N;
}

Program:
A program that inputs test score of a student and display his grade according to the following
criteria:
Test score Grade
>= 90 A
80 - 90 B
70 - 79 C
60 - 69 D
Below 60 F

#include<iostream>

int main( )
{
int score;
Cout<<”enter your test score:”;
Cin>>score;
If(score>=90)
Cout<<”your grade is A.”;
Else if(score>=80)
Cout<<”your garde is B.”;
Else if(score>=70)
Cout<<”your grade is C.”;
Else if(score>=60)
Cout<<”your grade is D.”;
Else
Cout<<”your grade is F.”;
Return 0;
}

Exercise:

Q1. Write a program that determines whether an integer is even or odd and negative or positive.
Make use of modulo operator.

Q2. Write a Mark Sheet program that accepts the marks of the different subjects from the user.
Calculate total marks, percentage and grade.
If per<60 and per >50 then Grade is “C”
If per<70 and per >60 then Grade is “B”
If per<80 and per >70 then Grade is “A”
If per >80 then Grade is “A-1”
If per<50 then prints FAIL.

Q3. Write a program that finds the maximum number from three input numbers?

Q4. Create a program in which the user selects name of the figure and program presents the
formula that is used for calculating its area. Use a rectangle, triangle, circle, and parallelogram as
the figure choice?

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy