CONDITIONAL STRUCTURE(IF,IF-ELSE,ELSE-IF)COM.pptx

f2023105233 8 views 11 slides Sep 12, 2024
Slide 1
Slide 1 of 11
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11

About This Presentation

CONDITIONAL STRUCTURES


Slide Content

PROGRAMING FUNDAMENTALS PRESENTATION NAME: WALI AHMAD ID: F2023105233 TOPIC: Conditional Statements( IF,IF- ELSE,ELSE-IF)

Introduction to Conditional Statements What are Conditional St atements ? Conditional statements allow a program to make decisions. They execute different blocks of code based on specified conditions. The common conditional structures in C++: if else if else

IF-STATEMENT SYNTAX OF IF STATEMENT: If(condition) { //BLOCK OF STATEMENTS TO BE EXECUTED } HOW IT WORKS ? When this statement is executed, the condition is evaluated. If the condition is true then the block of statements within the braces will be executed. If the condition is false then the block of statements within the braces will be skipped and the control will be transferred to the next statement if any exists.

EXAMPLE PROGRAM: #include <iostream> using namespace std; int main() { int number; cout << "Enter a number: "; cin >> number; if (number > 0) { cout << "The number is positive." << endl ; } cout <<number<<" "<<"is negative"<< endl ; return 0; } OUTPUT:

IF-ELSE SYNATX OF IF-ELSE: If(condition) { //BLOCK OF STATEMENTS } else { //BLOCK OF STATEMENTS }

IF-ELSE HOW IT WORKS? The if-else statement is used in situation where some code is to be executed if acondition is true and some other code is to be executed if the condition is false. When if-else statement is executed, the condition is evaluated. If the condition is true then the block of statements following if will be executed and the block of statements following else will be skipped If the condition is false then the block of statements following if will be skipped and the block of statements following else will be executed.

EXAMPLE PROGRAM: #include <iostream> using namespace std; int main() { int number; cout << "Enter a number: "; cin >> number; if (number % 2 == 0) { cout << "The number is even." << endl ; } else { cout << "The number is odd." << endl ; } return 0; } OUTPUT

ELSE-IF SYNTAX OF ELSE-IF: If(condition){ //BLOCK OF STATEMENT } else if(condition){ //BLOCK OF STATEMENTS } . . . . else{ //BLOCK OF STATEMENTS }

ELSE-IF HOW IT WORKS? The else-if statement is a type of conditional statement that combines morethan two conditions. It allows the programmer to make a decision based on severalconditions . When this statement is executed, condition-1 is evaluated, if it is true then theblock of statements following if is executed and if it is false, the next condition isevaluated If any condition is true then following block of statements is executed If none of the conditions is true then the block of statements following else isexecuted automatically.

EXAMPLE PROGRAM: #include <iostream> using namespace std; int main() { int score; cout << "Enter your score (0-100): "; cin >> score; if (score >= 90) { cout << "Grade: A" << endl ; } else if (score >= 80) { cout << "Grade: B" << endl ; } else if (score >= 70) { cout << "Grade: C" << endl ; } else { cout << "Grade: F" << endl ; } return 0; } OUTPUT:
Tags