Di Posting Oleh : Simple Learning
Kategori : control structure example if statement nested if statement positive number
If statement in C++
IF the condition is true then the block of statement is executed and if the condition is false the the block of statement is ignored and the control is transferred to the next statement after if statement.
Syntax to write IF statement:
if (relational or logical condition)
{
Block of statements
}
Next statement after if:Flowchart of if Statement
For Example:
A Program to check number is positive and how many digits number have
Code:
If is a keyword in C++ language. It is used to execute or ignore a set of statements after testing it.A condition is a relational or logical expression and it produces either true(means 1) or false(means 0) result.
IF the condition is true then the block of statement is executed and if the condition is false the the block of statement is ignored and the control is transferred to the next statement after if statement.
Syntax to write IF statement:
if (relational or logical condition)
{
Block of statements
}
Next statement after if:Flowchart of if Statement
For Example:
A Program to check number is positive and how many digits number have
Code:
- #include<iostream>
- using namespace std;
- int main()
- {int num;
- cout<<" Enter Number btween 1 to 999 ";
- cin>>num;
- if(num>0)
- { cout<<num<<" is a Positive Number "<<endl;
- if(num<10)
- {
- cout<<num<<" is a ONE digit Number ";
- }
- else if(num<100)
- {
- cout<<num<<" is a TWO digit Number ";
- }
- else if(num<1000)
- {
- cout<<num<<" is a Three digit Number ";
- }
- }
- return 0;}
0 Response to "IF and Nested IF Statements in C++"
Post a Comment