• No results found

Intro to ProgrammingWeek # 4If-else StatementLecture # 6

N/A
N/A
Protected

Academic year: 2021

Share "Intro to ProgrammingWeek # 4If-else StatementLecture # 6"

Copied!
46
0
0

Loading.... (view fulltext now)

Full text

(1)

Intro to Programming Week # 4

If-else Statement Lecture # 6

By: Saqib Rasheed

Department of Computer Science & Engineering Air University

(2)

Control Structures

3 control structures

Sequence structure

Programs executed sequentially by default

Selection structures (conditional instructions)

if, if/else, switch

Repetition structures (loop/jump instructions)

while, do/while, for

(3)

Flow Chart for if statement

(4)

if Statement

if (condition) {

statement ; -

- }

The if statement allows conditional execution

(5)

Practice Example # 1

main () {

int num;

cout<<“Enter a number less than 10:”;

cin>>num;

If (num <10) {

cout<<“What a obedient students”

} }

(6)

Relational Operators

The relational operators allow to compare two values

Whether they are equal to each other.

Unequal

Whether one is greater than the other.

(7)

Relational Operators

(8)

We can even use arithmetic expressions in the if statement.

if ( 3 + 2 % 5 ) {

cout<<"This works";

}

if ( a = 10 ) {

cout<<"Even this works";

}

if ( -5 ){

cout<<"Surprisingly even this works";

}

(9)

int var1, var2;

cout<<"Input the first number:";

cin>>var1;

cout<<"Input the second number:";

cin>>var2;

if(var1 == var2) {

cout<<"var1 is equal to var2";

}

Practice Example # 2

Apply all relational operators

(10)

if-else

Entry point for if-else block

Exit point for if-else block

(11)

if-else

if (condition) {

statement ; -

- } else {

statement ; -

- }

(12)

Practice Example # 4

cout<<“Enter a number less than Equal to 10:”;

cin>>num;

If (num <=10) {

cout<<“What a obedient students”

}

else {

cout<<“You are not obedient Student”;

}

(13)

int var1, var2;

cout<<"Input the first number:";

cin>>var1;

cout<<"Input the second number:";

cin>>var2;

if(var1 == var2) {

cout<<"var1 is equal to var2";

} else {

cout<<"var1 is not equal to var2";

}

Practice Example # 5

(14)

Example Nested if

int var1,var2,var3,var4;

var1=12;

var2=12;

var3=10;

var4=10;

if(var1 == var2) {

cout<<"\nOuter Loop Statement";

if (var3 == var4) {

cout<<"\nVar1 & Var2 are Equal and Var3 & var4 are Equal";

} }

cout<<"\nMain Statement";

(15)

Largest Number among 3

Write a program in C++ that take input of three integers numbers from user.

Find the largest number among three of them.

(16)

Largest Number among 3

(Code)

#include<iostream.h>

void main () {

int a,b,c, larg;

cout<<"Enter First Integer=";

cin>>a;

cout<<"Enter Second Integer=";

cin>>b;

cout<<"Enter Third Integer=";

cin>>c;

(17)

Largest Number among 3

(Code)

if (a > b)

larg = a;

else

larg = b;

if (larg > c )

cout<<"Largest is ="<<larg<<endl;

else

cout<<"Largest is ="<<c<<endl;

}

(18)

Nested if else Statement

If (condition) {

do this;

}

else if(condition) {

do this;

}

(19)

Nested if else Statement

If (condition) {

do this;

}

else if(condition) {

do this;

} else {

do this;

}

(20)

Nested if else

Example of nested if else

marks >= 90  Grade A marks >= 80  Grade B marks >=70  Grade C marks >=60  Grade D

(21)

Nested if else (Code)

int main ()

{ //start of main function int marks;

cout<<"Enter the grade of student=";

cin>>marks;

if ( marks >= 90 )

cout<< " Grade A \n";

else if (marks >= 80 )

cout<<" Grade B \n";

(22)

Nested if else (Code)

else if ( marks >=70 )

cout<<" Grade C \n";

else if (marks >=60)

cout<<" Grade D \n";

else {

cout<<" Grade F \n";

cout<<" You have to take the classes again\n";

cout<<" Work Hard To Get Good Grade\n";

}

return 0;

} // end of main function

(23)

Nested if / if-else example

int n1,n2,n3;

cout << "Enter three integers: ";

cin >> n1 >> n2 >> n3;`

if (n1 < n2)

if (n1 < n3) `

cout << "Their minimum is " << n1 << endl;

else

cout << "Their minimum is " << n3 << endl;

else if (n2 < n3)

cout << "Their minimum is " << n2 << endl;

else

cout << "Their minimum is " << n3 << endl;

(24)

Forms of if

The if statement can take any of the following forms:

(a) if ( condition )

do this ;

(b) if ( condition ) {

do this ; and this ; }

(25)

(c) if ( condition ) do this ;

else if (condition) do this ;

Forms of if -else

(26)

(c) if ( condition ) do this ;

else if (condition) do this ;

else

do this;

Forms of if -else

(27)

(d) if ( condition ) {

do this ; and this ; }

else {

do this ; and this ; }

Forms of if -else

(28)

(e) if ( condition ) do this ;

else {

if ( condition ) do this ;

else {

do this ; and this ; }

}

Forms of if -else

(29)

(f) if ( condition ) {

if ( condition ) do this ;

else {

do this ; and this ; }

} else

do this ;

Forms of if -else

(30)

(d) if ( condition ) ; {

do this ; and this ; }

Logical error

Semicolon does not come hear

(31)

Logical Operators

AND &&

OR ||

Not !

Bitwise Operator

|

&

(32)

Logical Operators

If a is greater than b

AND c is greater than d

In C++

if(a > b && c> d)

if(age > 18 || height > 5)

(33)

The Conditional Operators

The conditional operators ? and : are sometimes called ternary operators

since they take three arguments

expression 1 ? expression 2 : expression 3

(34)

Example of Condition Operator

int x, y ; cin>>x;

y = ( x > 5 ? 3 : 4 ) ;

This statement will store 3 in y if x is greater than 5, otherwise it will store 4 in y.

(35)

Hierarchy of Operators

(36)

Assignment

Write a program to calculate the salary as per the following table

(37)

Practice examples with code

(38)

Vowel / Consonant

Write a program in C++ to input a single character and print a message“ It is

vowel" if it is vowel otherwise print message "It is a "consonant“ Use if-else structure and

OR (||) operator only

(39)

Vowel / Consonant (Code)

int main() { char input;

cout<<"Input a single character-->";

cin>>input;

if (input == 'a' || input == 'e'|| input == 'i' ||input == 'o'|| input == 'u‘) cout<<"Its a VOWEL\n“;

else

cout<<"Its a CONSONANT\n“;

}

(40)

Odd/ Even

Write a Program in C++ that take an Integer value’s from the user and tell that the number Is EVEN or ODD

(41)

Odd/ Even (Code)

int value;

cout<<"Enter an Interger value ";

cin>>value;

if (value % 2 == 0)

cout<<"Your number is Even\n";

else

cout<<"Your number is Odd\n“;

(42)

Small / Capital Letter

Write a program in C++ that take a single

character from the user, and tells it's a Small Letter or it's a CAPITAL letter using nested if statement only

(43)

Small / Capital Letter (Code)

char letter;

cout<<"Enter the letter in \"CAPITAL\" or in \"SMALL\"-->";

cin>>letter;

if (letter >='A' && letter <= 'Z') {

cout<<"\n\nYou Entered a Capital Letter\n";

if (letter == 'a' || letter == 'e'|| letter == 'i' || letter == 'o'|| letter == 'u' ||

letter == 'A' || letter == 'E'|| letter == 'I' || letter == 'O'|| letter == 'U') cout<<"\n\nIts a VOWEL\n";

else

cout<<"\n\nIts a CONSONANT\n“;

}

(44)

Small / Capital Letter (Code)

else if (letter >= 'a' && letter <= 'z') {

cout<<"\n\nYou Entered a Small Letter\n";

If (letter == 'a' || letter == 'e'|| letter == 'i' || letter == 'o'|| letter =='u' ||

letter == 'A' || letter == 'E'|| letter == 'I' || letter == 'O'|| letter == 'U') cout<<"\n\nIts a VOWEL\n";

else

cout<<"\n\nIts a CONSONANT\n“;

}

else cout<<"\n\nIts Not a letter";

cout<<endl;

return 0; }

(45)

Condition Operator (?:)

Write a Program in C++ to input a single letter in a char variable. If "m" is input print "You are Male” otherwise "You are Female" by Using Condition Operator (?:)

(46)

Condition Operator (?:)

int main () {

char gender;

cout<<"Enter the Gender of the Person=";

cin>>gender;

/*(?:) First Value is Printed if the condition

is true and if it is false second condition is printed*/

cout<<( gender>= 'm' ? "You are Male\n" : "You are Female\n");

}

References

Related documents