Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
162 views

C++ Conditional Statements: Prepared By: Engr. Yolly D. Austria

This document discusses conditional statements in C++, including if statements, else if statements, and switch statements. It provides the syntax for each statement and sample programs to demonstrate their use. The if statement executes code based on a true/false expression. Else if statements add additional conditions. Switch statements select among multiple options by matching an expression to case labels. Break statements are used to terminate code blocks early within if, else if, and switch statements.

Uploaded by

MelissaTaan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
162 views

C++ Conditional Statements: Prepared By: Engr. Yolly D. Austria

This document discusses conditional statements in C++, including if statements, else if statements, and switch statements. It provides the syntax for each statement and sample programs to demonstrate their use. The if statement executes code based on a true/false expression. Else if statements add additional conditions. Switch statements select among multiple options by matching an expression to case labels. Break statements are used to terminate code blocks early within if, else if, and switch statements.

Uploaded by

MelissaTaan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

C++ Conditional Statements

Prepared by: Engr. Yolly D. Austria

Introduction
The program-control statements are the
essence of any computer language because
they govern the flow of program execution.
They modify the order of statement
execution. This can cause other program
statements to execute multiple times or not
execute at all, depending on the
circumstances.

if Statement

The if statement evaluates an expression and


directs program execution depending on the result
of the evaluation.
If expression evaluates as true, statement is
executed. In either case, execution then passes to
whatever code follows the if statement.

if Statement
Syntax:
if (expression)
single statement;

if (expression)
{
statement1;
statement2; // and so on..
statement_last;
}

A program block, or compound statement, is a series of


statements placed inside curly braces {}.
When the if statement is true, then all the statements
inside the {} are run. When the if statement is false, then
none of them are run and the program jumps to the next
statement after the closing brace `}
The condition to be tested appears in parenthesis () after
the keyword if.

These expressions are evaluated in order.


If any expression is true, the statement associated with it
is executed and this terminates the whole chain.
The last else part handles the "none of the above" or
default case, when none of the other conditions are
satisfied. This is optional and can be omitted.

Sample Program 1:
#include <iostream.h>
#include <conio.h>
int main ()
{
int a;
cout<<\n<< Enter a number : ;
cin>>a;
if(a<0)
cout<<The number is negative.;
else if(a>0)
cout<<The number is positive.;
else
cout<<The number is zero;
return 0;
}

Sample Program 2:
#include <iostream.h>
int main()
{
int number;
cout << "Enter a number between 1 and 99: ";
cin >> number;
if (number > 0 && number < 100 )
{
if (number < 10)
cout << "One digit number\n";
else
cout << "Two digit number\n";
}
else
cout << "Number not in range\n";
return 0;
}

The selective Structure: switch

Switch is a multiple-branch decision statement.


In switch, a variable is successively tested against a
list of integer or character constants. When the
match is found, the statement or statements
associated with the constant is executed until the
break is reached.

The selective Structure: switch


Once the case statement has been matched then all
the following statements in the switch statement
are executed unless you specifically stop them.
You stop execution of any further statements by
using a break statement. This breaks out of the
switch statement and continues execution with
whatever code follows the closing brace of the
switch.

The selective Structure: switch


you must not use floats in switch statements.
expression evaluated by switch must evaluate to an
integer.
The default is executed if no matches are found.
The default is optional and, if it is not present, no
action takes place if all matches fail. The default
statement is used to inform the user that an invalid
response was entered.

switch Statement
Syntax:
switch (expression)
{
case const_expr_1:
statements 1
break;
case const_expr_2:
statements 2
break;
default:
statements n
}

Sample Program 1:
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int code;
cout<<Enter a code: ;
cin>>code;
switch (code)
{
case 1:
cout<<\nFirst Year ;
break;
case 2:
cout<<\nSecond Year ;
break;

case 3:
cout<<\nThird Year ;
break;
case 4:
cout<<\nFourth Year ;
break;
case 5:
cout<<\nFifth Year ;
break;
default:
cout<<\nNot in the choices ;
}
getch();
return 0;
}

Sample Program 2:
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
char LtrGrade;
cout <<Enter a letter grade: ;
cin>> LtrGrade;
switch (LtrGrade)
{
case a:
case A:
cout<<\nExcellent ;
break;
case b:
case B:
cout<<\nSuperior ;
break;

case c:
case C:
cout<<\nAverage ;
break;
case d:
case D:
cout<<\nPoor ;
break;
case e:
case E:
cout<<\nTry Again ;
break;
default:
cout<<\nNo match was found for
the ENTRY<<LtrGrade<<endl;
}
getch();
return 0;
}

You might also like