+ 3
how can I do if else statement im just a starter?
what can I do with the if-else statement when i program? if (statement) { System.out.println("__"); } else { Statement } is it wrong??????
3 Answers
+ 18
suppose u want to output something different if it condition goes false
//for example u want to output whether a number is odd or even
then , u will write
if (n%2==0) {System.out.print("even");}
else {System.out.print("false");}
0
suppose u want to take an input of 2 no. and print the greater one hence u have to use the if else statement.. in the if(here u have to the condition in parenthesis) if it is true then the program is executed for the resulting if conditions else it proceeds via else..
example::::::
#include<iostrem>
using namespace std;
int main()
{
int x, y ; //assuming x and y are not equal.
cout<< "Enter 2 numbers : " ;
cin >> x >> y ;
if (x>y) //condition for if
cout<< x ;
else
cout<< y ; //when the condition for if is not true this is followed.
return 0 ;
}
// when the if or else algorithms(steps when the condition is true) are single line then u have to put no braces but if they have multiple steps then u have to write the steps in curly braces
{
like this
}
Thats all...