Lecture 2 Week 4
Lecture 2 Week 4
FUNDAMENTAL
LAB
Umer Ahmad
Lecture 2 week 4
If
Condition
al
statement
s If- else
IF
STATEMEN
T
FLOWCHA
RT
• If the condition or Boolean expression
evaluates to true, then the code block inside
the if statement will be executed.
If statement
• If condition is false then compiler skip the if
body.
• #include<iostream>
• #include<conio.h>
• using namespace std;
• int main() {
• int marks;
• cout<< "Enter your marks you get in the exams"<<
endl;
Example
• cin>> marks;
program
• if (marks>=40) {
• cout<< "Congratulation, you have passed the exam.
";
• }
• getch();
• return 0;
• }
• #include <iostream>
• using namespace std;
• int main () {
• // local variable declaration:
• int a = 10;
• // check the boolean condition
If statement • if( a < 20 ) {
program • // if condition is true then print the following
• cout << "a is less than 20;" << endl;
• }
• cout << "value of a is : " << a << endl;
• return 0;
• }
Practice question
• Write a C++ program to accept two integers and check whether they are
equal or not.
• Data to be test : 8 8
desired Output :
Number1 and Number2 are equal
IF ELSE
FLOWCHA
RT
• If statement with an optional else statement.
In which, if statement is executed only when
If-else •
the condition is true.
statement • And else statement is executed only when
the condition is false.
• #include <iostream>
• int main () {
• int a = 100;
• if( a < 20 ) {
• } else {
• }
• return 0;
• }
PRACTICE
PROGRAM
Check Whether Number is Even or Odd using if else.
Hint: n % 2 == 0
LAB TASK
Check Vowel or a Consonant Manually