0% found this document useful (0 votes)
43 views

3 Conditional Structure - If and Switch

The document discusses various control structures in programming including sequential logic, selection logic, and iteration logic. It provides examples of different conditional statements like if, if-else, nested if, and multi-if/else-if statements. Key control structures covered are sequence, selection, and repetition. Examples are given to illustrate how to use if, if-else, and else-if statements to make programming decisions based on different conditions.

Uploaded by

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

3 Conditional Structure - If and Switch

The document discusses various control structures in programming including sequential logic, selection logic, and iteration logic. It provides examples of different conditional statements like if, if-else, nested if, and multi-if/else-if statements. Key control structures covered are sequence, selection, and repetition. Examples are given to illustrate how to use if, if-else, and else-if statements to make programming decisions based on different conditions.

Uploaded by

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

Programming Fundamentals

Control Structure
Books

 Deitel & Deitel :– C++ How to Program


 Robert Lafore : Object-Oriented Programming in C++
 IT Series : Object Oriented Programming Using C++
Control Structures

 Control Structures are just a way to specify flow of control in programs.


 Any algorithm or program can be more clear and understood if they
use self-contained modules called as logic or control structures.
 It basically analyzes and chooses in which direction a program flows
based on certain parameters or conditions.
 There are four basic types of logic, or flow of control, known as:
1. Sequence logic, or sequential flow
2. Selection logic, or conditional flow
3. Iteration logic, or repetitive flow
4. Function Call
1. Sequential Logic / Sequential Flow

 In Sequential logic/flow, statements are executed in the


same order in which they are specified in the program.
Entry
 Control moves from one statement to another statement in
a logical sequence.
Statement 1

 All statements are executed exactly once. Statement 2


 It means that no statement is skipped or repeated more than one
time.
Statement 3
 Note : All the Programs covered in previous lectures are the
example of sequential logic.
End
2. Selection logic, or conditional flow

 A Selection Structure selects a statement or block of statements to


execute on the basis of a condition.
 Statement(s) are executed or ignored on the basis of a condition.
 Statement(s) are executed if the condition is True.
 Statement(s) are ignored if the condition is False.
If statement

 if statement is the most simple decision making statement.


 It is used to decide whether a certain statement or block of
statements will be executed or not
 i.e if a certain condition is true then a block of statement is executed
otherwise not.
Syntax

if(condition)
{
// Statements to execute
// if condition is true
}
If statement - Example

 Write a program #include "iostream"


using namespace std;
 that input two numbers int main()
{
int num1 , num2;
 and validates that both are cout<<"Enter First Number : ";
equal. cin>> num1;

cout<<"Enter Second Number : ";


cin>> num2;

if ( num1 == num2 )
{
cout<<"Both Numbers are equal "<<endl;
}

cout<<"End of Program ! ";


return 1;
}
If – else statement

 The if statement alone tells us that if a condition is true it will


execute a block of statements and if the condition is false it won’t.
 But what if we want to do something else if the condition is false.
 Here comes the else statement. We can use the else statement
with if statement to execute a block of code when the condition is
false.
 Syntax
if (condition)
{
// Executes this block if condition is true
} else
{
// Executes this block if // condition is false
}
If – else statement - Example

 Write a program which input marks of a student #include "iostream"


using namespace std;
 And decides whether the student is Pass or Fail int main()
{
 Marks equal or above 40 is considered as pass. int marks;
cout<<"Enter Marks of the Student : ";
 Marks below 40 is considered as Fail. cin>> marks;

if ( marks >= 40 )
{
cout<<"Student is Pass !"<<endl;
}
else
{
cout<<"Student is Fail !"<<endl;
}

cout<<"End of Program ! ";


return 1;
}
Conditional or Ternary Operator (?:)

 The conditional operator is kind of similar to the if-else statement


 it does follow the same algorithm as of if-else statement
 but the conditional operator takes less space
 and helps to write the if-else statements in the shortest way possible.

if( expression1)
{
variable = expression2;
}
else
{
variable = expression3; variable = Expression1 ? Expression2 : Expression3
}
Conditional or Ternary Operator (?:)

 // C++ program to find largest among two numbers using ternary operator

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
variable = Expression1 ? Expression2 : Expression3
5. int n1 = 5, n2 = 10, max;
6. max = (n1 > n2) ? n1 : n2; If ( n1 > n2 )
7. cout << "Largest number = " << max; {
max = n1;
8. return 1;
} else
9. } {
max = n2;
}
Nested if statement

 A nested if in is an if statement that is the target of another if


statement.
 Nested if statements means an if statement inside another if
statement. we can place an if statement inside another if
statement.
 Syntax

if (condition1)
{
// Executes when condition1 is true
if (condition2) {
// Executes when condition2 is true
}
}
Multi-if or if-else-if statement

 A user can decide among multiple options.


 The if statements are executed from the top down.
 As soon as one of the conditions controlling the if is true, the
statement associated with that if is executed
 and the rest of the else-if statements are bypassed.
 If none of the conditions are true, then the final else statement will
be executed. if (condition1)
 Syntax // Executes when condition 1 is true
else if (condition2)
// Executes when condition 2 is true
…..

else
// Executes when None of above is true
Multi-if or if-else-if statement #include "iostream"
- Example using namespace std;
int main()
{
int marks;
cout<<"Enter Marks of the Student : ";
cin>> marks;
 Write a program which input marks of a student
if ( marks >= 64 )
 And decides Grade of the student
cout<<“A Grade !"<<endl;
1. Marks equal or above 64 considered as A Grade.
else if ( marks >= 52 )
2. Marks equal or above 52 considered as B Grade. cout<<“B Grade !"<<endl;

3. Marks equal or above 40 considered as C Grade. else if ( marks >= 40 )


cout<<“C Grade !"<<endl;
4. Marks equal or above 24 considered as D Grade.
else if ( marks >= 24 )
5. Marks Less than 24 considered as F Grade. cout<<“D Grade !"<<endl;

else
cout<<"Student is Fail !"<<endl;

cout<<"End of Program ! ";


return 1;
}
Flow Chart
Program

#include "iostream"
using namespace std;
int main()
{
int marks;
cout<<"Enter Marks of the Student : ";
cin>> marks;

if ( marks >= 64 )
cout<<“A Grade !"<<endl;

else if ( marks >= 52 )


cout<<“B Grade !"<<endl;

else if ( marks >= 40 )


cout<<“C Grade !"<<endl;

else if ( marks >= 24 )


cout<<“D Grade !"<<endl;

else
cout<<"Student is Fail !"<<endl;

cout<<"End of Program ! ";


return 1;
}
Switch statement

 The switch statement is a multiday branch statement.


 It provides an easy way to dispatch execution to different parts of
code based on the value of the expression.
 Its an alternative to multi-if statement.
 Works with single expression. switch (expression)
{
 Single variable. case value1:
 Its Fast but not flexible. statement1;
break;
case value2:
statement2;
break;
..
default:
statement;
}
Write a program which takes a character from user. It displays whether it’s a vowel or not.
vowels are = ‘a,e,i,o,u’

#include "iostream" #include "iostream"


multi – if structure using namespace std; Switch structure
using namespace std; int main()
int main() {
{ char c;
char c; cout<<"Enter Any Character : ";
cout<<“Enter Any Character : "; cin>> c;
cin>> c;
switch ( c )
{
if ( c == 'a' ) case 'a':
cout<< c << “ is a vowel !"<<endl; cout<<c<<" is a vowel "<<endl;
break;
else if ( c == ‘e' ) case 'e':
cout<< c << “ is a vowel !"<<endl; cout<<c<<" is a vowel "<<endl;
break;
case 'i':
else if ( c == ‘i' ) cout<<c<<" is a vowel "<<endl;
cout<< c << “ is a vowel !"<<endl; break;
case 'o':
else if ( c == ‘o' ) cout<<c<<" is a vowel "<<endl;
cout<< c << “ is a vowel !"<<endl; break;
case 'u':
cout<<c<<" is a vowel "<<endl;
else if ( c == ‘u' ) break;
cout<< c << “ is a vowel !"<<endl; default:
cout<<c<<" = Not vowel "<<endl;
else break;
cout<<“Not a vowel !"<<endl; }
return 1;
} }
Operators in C++
Logical Operators

Logical Operators in C++

Name Symbol Meaning

Not ! Logical inverse. True to false. False to true

AND && Logical Multiplication, Output is true if all inputs are true

OR || Logical Addition, Output is true if any inputs is true


Logical Operators
Write a program which takes a character from user. It displays whether it’s a vowel or not.
vowels are = ‘a,e,i,o,u’

#include "iostream" #include "iostream"


multi – if structure using namespace std; Switch structure
using namespace std; int main()
int main() {
{ char c;
char c; cout<<"Enter Any Character : ";
cout<<“Enter Any Character : "; cin>> c;
cin>> c;
switch ( c )
{
if ( c == 'a' ) case 'a':
cout<< c << “ is a vowel !"<<endl; cout<<c<<" is a vowel "<<endl;
break;
else if ( c == ‘e' ) case 'e':
cout<< c << “ is a vowel !"<<endl; cout<<c<<" is a vowel "<<endl;
break;
case 'i':
else if ( c == ‘i' ) cout<<c<<" is a vowel "<<endl;
cout<< c << “ is a vowel !"<<endl; break;
case 'o':
else if ( c == ‘o' ) cout<<c<<" is a vowel "<<endl;
cout<< c << “ is a vowel !"<<endl; break;
case 'u':
cout<<c<<" is a vowel "<<endl;
else if ( c == ‘u' ) break;
cout<< c << “ is a vowel !"<<endl; default:
cout<<c<<" = Not vowel "<<endl;
else break;
cout<<“Not a vowel !"<<endl; }
return 1;
} }
Vowels using
Logical Operators
#include "iostream"
using namespace std;
int main()
{
char c;
cout<<“Enter Any Character : ";
cin>> c;

if ( c == 'a' || c == ‘e' || c == ‘i' || c == ‘o' || c == ‘u' )


cout<< c << “ is a vowel !"<<endl;

else
cout<<“Not a vowel !"<<endl;
}
Home Work

 Comparison between if and switch ?

 Practice a program on ASCII Table given on the next slide.


 Program should Input a character from user and display a message that
it’s a Number, Upper case Letter or Lower Case Letter or some other
character.
 Number are digits between 0 to 9
 Upper case letter ( A to Z )
 Lower Case letter ( a to Z )
 Other symbol ( Any value other than above 3 categories. )
Programs for Practice.

1. Write a C++ program to check whether a given number is even or odd.

2. Write a C++ program to check whether a given number is positive or negative.

3. Write a C++ program to find whether a given year is a leap year or not.

4. Write a C++ program to read the age of a candidate and determine whether it is eligible for casting
his/her own vote. (18+ age is required to cast vote).

5. Write a C++ program which takes 3 numbers from user and find the largest of three numbers.

6. Write a program in C to read any day number in integer and display day name in the word.

7. Write a program in C to read any Month Number in integer and display the number of days for this
month.

You might also like