Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Fundamental-1

Similar presentations


Presentation on theme: "Programming Fundamental-1"— Presentation transcript:

1 Programming Fundamental-1
Instructor Name: Muhammad Safyan Lecture-6

2 Lecture Outline Nested if Statement 2

3 BLOCKS if ( studentGrade >= 60 ) cout << "Passed.\n"; else {
cout << "Failed.\n"; cout << "You must take this course again.\n"; }

4 Nested If if condition within a if condition is called Nested if

5 Nested If #include <iostream.h> #include<conio.h> main() {
int code; cout<<"enter the vlaue of code"; cin>>code; if(code>1) cout<<“ "; else if(code<1) cout<<"\n "; cout<<"\n "; getch(); }

6 Nested If if condition within a if condition is called Nested if
if ( x > 5 ) if ( y > 5 ) cout << "x and y are > 5"; else cout << "x is >= 5"; ============================================= { } cout << "x is <= 5";

7 IF Condition (scenarios)
if (a == b)  {   if (b <= 3)     x = 4;   else     x = 0; }  else   if (a > b)     x = a;      x = b; if a is given as 5, b is given as 5 (Answer: 0) if a is given as 2, b is given as 2 (Answer: 4) (Answer: 5) if a is given as 5, b is given as 2 if a is given as 2, b is given as 7 (Answer: 7)

8 IF Condition (multiple scenario)
Example 2. According to the values given, what will be the value of x after each of the following? if a is given as 2, b is given as 5 (Answer: 10) x = 5; if (a < b)           if (a < 10)                x = x + 3; x = x + 2; if a is given as 15, b is given as 20 (Answer: 7) if a is given as 3, b is given as 3 (Answer: 7)

9 IF Condition (multiple scenario)
Trace the following and show the output int a,b,c; a=3; b=-5; if(a<2*b) { b=a; c=b; cout<<c; } else if (b>0) b=0; cout<<b;

10 IF Condition (multiple scenario)
Trace the following and show the output

11 #include<conio.h>
#include<iostream.h> main() {       int a,b,c;       cout<<"Enter first number: ";       cin>>a;       cout<<"Enter second number: ";       cin>>b;       cout<<"Enter third number: ";       cin>>c;                       if(a>b)             {                      if(a>c)                      {                       cout<<a<<"  is biggest number";                      }                       else                      {                         cout<<c<<"  is biggest number";            }              else       {                      if(b>c)                      cout<<b<<"  is biggest number";                      else                      cout<<c<<"  is biggest number";       }        getch();

12 Write a Program that prompt the user to enter the vlaue for variable a, b and c. Code the program based on condition written in left column of the table and print corresponding value written in right column of the table (Only use Nested if Condition. Condition Print a>b>c White a>c>b Yellow b>a>c Pink b>c>a Red c>a>b Green c>b>a Black


Download ppt "Programming Fundamental-1"

Similar presentations


Ads by Google