In this article we will learn about If-else control statement.
There are four types of if-else control statement.
if statement
Syntax
if(condition)
{
//code to be executed
}
Program
public class If_Example
{
public static void main(String args[])
{
int age = 20;
if(age > 18)
{
System.out.println("You can give vote.");
}
}
}
Output
You can give vote.
Explanation
If statement executes the block if the condition becomes true.
if-else statement
Syntax
if(condition)
{
//code if condition is true
}
else
{
//code if condition is false
}
Program
public class IfElse_Example
{
public static void main(String args[])
{
int age = 17;
if(age > 18)
{
System.out.println("You can give vote.");
}
else
{
System.out.println("You can't give vote.");
}
}
}
Output
You can't give vote.
Explanation
If condition becomes true then if block will be executed. otherwise else block will be executed.
nested if statement
Syntax
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
else
{
// Executes when condition2 is false
}
}
Program
public class Nested_If
{
public static void main(String args[])
{
int age = 20;
if(age > 18)
{
System.out.println("You can give vote.");
if(age % 2 == 0)
{
System.out.println("Your age is even.");
}
else
{
System.out.println("Your age is odd.");
}
}
}
}
Output
You can give vote.
Your age is even.
Explanation
If condition becomes true it executes statement of if block. if there is another if block exists it checks condition if condition becomes true then if block will be executed otherwise else block will be executed.
if-else-if ladder
Syntax
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}
Program
public class IfElse_leader
{
public static void main(String args[])
{
int age = 22;
if(age < 12)
{
System.out.println("You are child.");
}
else if(age > 12 && age < 20)
{
System.out.println("You are teenager.");
}
else if(age > 20 && age < 50)
{
System.out.println("You are mature.");
}
else
{
System.out.println("You are old.");
}
}
}
Output
You are mature.
Explanation
Here, if condition becomes true then it's block will be executed and rest of the condition will be ignored. If all conditions become false then else block will be executed.
Environment class allows you to obtain a number of details regarding the operating system currently hosting your .NET application using various static members. To illustrate the usefulness of System.Environment, update your Main() method to call a helper method named EnvironmentDetails() . Properties Meaning ExitCode Gets or sets the exit code for the application Is64BitOperatingSystem Returns a bool to represent whether the host machine is running a 64-bit OS MachineName Gets the name of the current machine NewLine Gets the newline symbol for the current environment SystemDirectory Returns the full path to the system directory UserName Returns the name of the user that started this application Version Returns a Version object that represents the version of the .NET platform Create EnvironmentDetails() method in class under Main() method and call this method in Main() method. Program
It is true that a console user interface (CUI) may not be as enticing as a graphical user interface (GUI) or web application, restricting the early examples to console programs will allow you to keep focused on the syntax of C# and the core aspects of the .NET platform, rather than dealing with the complexities of building desktop GUIs or web sites.As its name implies, the Console class encapsulates input, output, and error-stream manipulations for console-based applications. As you can see in below table the Console class does provide some members that can spice up a simple command-line application, such as the ability to change background and foreground colors and issue beep noises in a variety of frequencies. Members of System.Console Member Meaning Beep() This method forces the console to emit a beep of a specified frequency and duration. BackgroundColor,ForegroundColor These properties
Like any programming language, C# defines keywords for fundamental data types, which are used to represent local variables, class data member variables, method return values, and parameters. Unlike other programming languages, however, these keywords are much more than simple compiler- recognized tokens. Rather, the C# data type keywords are actually shorthand notations for full-blown types in the System namespace. Table lists each system data type, its range, the corresponding C# keyword, and the type’s compliance with the common language specification (CLS). C# keyword System type Range Meaning CLS complaint? bool System.Boolean true or false Represents truth or falsity Yes sbyte System.SByte -128 to 127 signed 8-bit number No byte System.Byte 0 to 255 Unsigned 8-bit
Comments
Post a Comment