0% found this document useful (0 votes)
23 views31 pages

If Else

The document discusses relational operators and conditional statements in programming. Relational operators compare values and return true or false. Common relational operators include ==, !=, <, >, <=, >=. Conditional statements like if-else and switch statements allow executing different blocks of code based on conditional expressions. If-else checks one condition, while switch can check multiple conditions against a variable.

Uploaded by

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

If Else

The document discusses relational operators and conditional statements in programming. Relational operators compare values and return true or false. Common relational operators include ==, !=, <, >, <=, >=. Conditional statements like if-else and switch statements allow executing different blocks of code based on conditional expressions. If-else checks one condition, while switch can check multiple conditions against a variable.

Uploaded by

syednajafturab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Conditional

Structure
Relational Operators
• The relational operators are used to specify condition in
Program.
• A relational computer compares two values.
• It produces result as true of false.
• The relational operator are sometimes called the conditional
operator or comparison operator.

• == equal to operator
• != not equal to operator
• < less than operator
• > Greater than operator
• >= Greater than or equal to
• <= Less than or equal to
Relational Operators
• Examples
• 100>50
• 45<90
• 31<=32
• 44>=44
• 1!=5
• 6==6
• X==X
• Y!=Y
Flowchart Symbols

CIS162AD
Input or Output
Begin or End

Processing Decision

4
Branch or Direction of Flow
If Statement
• Syntax
if (Expression)
(statements)
Expression
• If the Expression is true then
• execute statements

• It may be a single statement


or a group of statements within
statements
braces
• Pseudo code
if (condition)
statement;

if(condition)
{
statement 1;
statement 2;
statement N;
}
• Syntax
• if (condition)
• { statement; }

• if (condition)
• {
Statement1;
Statement2;
StatementN;
• }
if Selection Statement
• Selection statements
• Pseudocode example
• If student’s grade is greater than or equal to 60
Print “Passed”
• If the condition is true
• The print statement executes then the program continues to next statement
• If the condition is false
• The print statement ignored then the program continues
if Selection Statement
• Translation into C++
if ( grade >= 60 )
{ cout << "Passed";}

• Any expression can be used as the condition


if Selection Statement
int m = 5;
int n = 10;

if (m < n)
++m;
++n;

cout << " m = " << m << " n = " n << endl;
if Selection Statement
• Inputs two number and find if both are equal
• main()
• {
• int a,b;
cin>>a;
cin>>b;
if(a==b)
{
cout<<“both are equal”;

}
• System(“pause”);

• }
The If-Else Statement
• Syntax
if (Expression)
Action1
else
Expression
Action2
• If Expression is true then execute false
true
Action1 otherwise execute Action2

if (v == 0) {
Action1 Action2
cout << "v is 0";
}
else {
cout << "v is not 0";
}
The If-Else Statement

• If else statement is another type of if statement . It execute


one block of statements when the condition is true and the
other when it is false.
• In any situation , one block is executed and the other is
skipped
The If-Else Statement
• if(condition)
• { statement; }
else
{ statement; }
The If-Else Statement
• Inputs two number and find if both are equal or not
• main()
• {
• int a,b;
cin>>a;
cin>>b;
if(a==b)
{
cout<<“both are equal”;
}
else

{
cout<<“both are not equal ”;
}
• system(“pause”);

}
if / else if / else Selection
Structures
• The actual syntax for the multiple if / else if / else selection
structure is as follows:
if ( this logical expression is true )
statement ;
else if ( this logical expression is true )
statement ;
else if ( this logical expression is true )
statement ;
else
statement ;
Simple Program Using if /
else if / else
#include <stdio.h>
int main ( )
{
int a , b ;
cout<<"Enter values for a " ;
cin>> a;
cout<<“enter value for b”;
cin>>b ;

Winter Quarter
if ( a < b )
{ cout<<"a is less than\n" ;
}
else if ( a == b )
{ cout<< " a is equal to b\n" ;
}
else Lect
{ cout<<"a is larger than b\n" ; 8
}
} P. 19
Logical Operators

• The binary logical operators combine two boolean expressions


into one. Operator Meaning Kind
&& AND Binary
|| OR Binary
! NOT Unary

• Binary logical operators have lower precedence than relational


operators (they will be evaluated after)
• NOT has the same precedence as negation.
if Selection Statement
• Logical AND (&&) Operator
• Consider the following if statement
• Gender=1;
• Age=66;
• Females=1;

if ( gender == 1 && age >= 65 )


Females++;

• Combined condition is true


• If and only if both simple conditions are true
• Combined condition is false
• If either or both of the simple conditions are false
if Selection Statement
• Logical OR (||) Operator
• Consider the following if statement
• semseterAverage=89;
• finalExam=89;
if ((semesterAverage >= 90) ||( finalExam >= 90 )
cout << “Student grade is A” << endl;
• Combined condition is true
• If either or both of the simple conditions are true
• Combined condition is false
• If both of the simple conditions are false
The switch Statement
• Similar to if statements
• Can list any number of branches
• Used in place of nested if statements
• Avoids confusion of deeply nested ifs
The switch Statement
switch (expression) {
case value1:
statement1;
break;
case value2:
statement2;
break;

case valuen:
statementn;
break;
default:
statement;
}
The switch Statement
cin>>grade;
//H
switch (grade)
{
case ‘A’:
cout << “Grade is between 90 & 100”;
break;
case ‘B’:
cout << “Grade is between 80 & 89”;
break;
case ‘C’:
cout << “Grade is between 70 & 79”;
break;
case ‘D’:
cout << “Grade is between 60 & 69”;
break;
case ‘E’:
cout << “Grade is between 0 & 59”;
break;
default:
cout << “You entered an invalid grade.”;
}
The switch Statement
* * * * Menu * * * *

1. Nablus
2. Rammallah
3. Tolkarm
4. Jenien

Choose either 1, 2, 3 or 4:
The switch Statement
switch (choice) switch (choice)
{ {
case 1: case 1:
cout << “Nablus”; cout << “Nablus”;
case 2: break;
cout << “Rammallah”; case 2:
case 3: cout <“Rammallah”;
cout << “Tolkarm”; break;
case 4: case 3:
cout << “Jenien”; cout << “Tolkarm”;
default: break;
cout<<“ invalid choice”; case 4:
} cout << “Jenien”;
break;
default:
cout<<“ invalid choice”;
}
The switch Statement
#include<iostream>
Void main ( )
{
int value
cout << “Enter 1- Palestine 2- Egypt 3- USA”;
cin >> value;
switch (value)
{
case 1: cout << “No of population is 5 million”; break;
case 2: cout << “No. of population is 70 million”; break;
case 3: cout << “No. of population is 180 million”; break;
default: cout<<“invalid choice”;
}
}
switch Multiple-Selection
Statement
• switch statement
• Used for multiple selections

• Tests a variable or expression


• Compared against constant integral expressions to decide on action
to take
The switch Statement
• switch statement
• Controlling expression
• Expression in parentheses after keyword switch
• case labels
• Compared with the controlling expression
• Statements following the matching case label are executed
• Braces are not necessary around multiple statements in a case
label
• A break statements causes execution to proceed with the first
statement after the switch
• Without a break statement, execution will fall through to the
next case label
The switch Statement
• switch statement (Cont.)
• default case
• Executes if no matching case label is found
• Is optional
• If no match and no default case
• Control simply continues after the switch

You might also like