Professional Documents
Culture Documents
Screenshot 2024-05-25 at 1.36.23 PM
Screenshot 2024-05-25 at 1.36.23 PM
DR.MUMTAZ ZAHOOR
COURSE TITLE: PROGRAMMING FUNDAMENTALS
LECTURE NO 7
Overview
2
CONTROL STRUCTURE
Control Structure & its Types
4
IF STATEMENT
IF Statement & its Syntax
9
Syntax:
1. if( condition)
statement // statement will execute if the condition is true
2. If (condition)
{Statement 1
Statement 2
Statement n..}
Note: multiple statements are written in curly brackets.
#include <iostream>
using namespace std;
int main ()
{
int a = 10;
if( a < 20 )
cout << "a is less than 20“;
return 0;
}
Output
a is less than 20
#include <iostream>
using namespace std;
int main ()
{
int marks;
cout<<“enter marks”;
cin>>marks;
if( marks >= 40 )
{
cout << “Congratulations\n“;
cout<<“You have passed”;
}
return 0;
}
Output
Congratulations
You have passed
Example 1 Example 2
#include <iostream> int main ()
using namespace std; {
int main () int n1, n2, n3, max;
cout<<“enter first number”;
{
cin>>n1;
int m; cout<<“enter second number”;
cout<<“enter number”; cin>>n2;
cin>>m; cout<<“enter third number”;
if( m< 0) cin>>n3;
cout << “m is negative number“; max=n1;
if( m>0) if(n2>max)
cout << “m is positive number“; max=n2;
If (n3>max)
if( m== 0)
max=n3;
cout << “m is zero“; cout<<“Maximum number is”<<max;
return 0; return 0;
} }
IF ELSE STATEMENT
If(condition)
{
// statement(s) will execute if the Boolean
expression is true
}
else
{
// statement(s) will execute if the Boolean expression
is false
}
int main ()
{
int a = 100;
if( a < 20 )
cout << "a is less than 20" ;
else
cout << "a is not less than
20“ ;
return 0;
}
int main ()
{
int m;
cout<<“enter number”;
cin>>m;
if( m%2==0)
cout << “m is even number“;
else
cout << “m is odd number“;
return 0;
}
MULTIPLE IF ELSE IF
STATEMENT
Multiple IF Else IF Structure
23
if(boolean_expression 1)
{
// Executes when the Boolean expression 1 is true
}
else if( boolean_expression 2)
{
// Executes when the Boolean expression 2 is true
}
else if( boolean_expression 3)
{
// Executes when the Boolean expression 3 is true
}
else
{
// executes when the none of the above condition is true.
}
Example 1 Example 2
THANKS