DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Java
  4. Basic Programming Operators in Java, PHP,C, C++ and more

Basic Programming Operators in Java, PHP,C, C++ and more

Aalia Dhurue user avatar by
Aalia Dhurue
·
Dec. 07, 11 · Interview
Like (0)
Save
Tweet
Share
1.77K Views

Join the DZone community and get the full member experience.

Join For Free

1. if

The if statement evaluates an expression. If that expression is true, then a statement is executed. If an else clause is given and if the expression is false, then the else’s statement is executed.

Syntax:


if( expression ) statement1;

//    or

if( expression ) statement1;
else statement2 ;

Examples:


if(loop<3) counter++;

if(x==y)
  x++;
else
  y++;
if(z>x)
 {
  z=5;
  x=3;
 }
else
 {
  z=3;
  x=5;
 }

2. switch

A switch statement allows a single variable to be compared with several possible constants. If the variable matches one of the constants, then a execution jump is made to that point. A constant can not appear more than once, and there can only be one default expression.

Syntax:


switch ( variable ) { case const:
   statements...; default: statements...; } 

Examples:


If betty is 1, then two lines are printed: betty=1 and betty=2. If betty is 2, then only one line is printed: betty=2. If betty=3, then only one line is printed: betty=3. If betty does not equal 1, 2, or 3, then “Not sure.” is printed.

3. while

The while statement provides an iterative loop.
Syntax:


statement is executed repeatedly as long as expression is true. The test on expression takes place before each execution of statement.
Examples:


4. do

The do…while construct provides an iterative loop.

Syntax:


statement is executed repeatedly as long as expression is true. The test on expression takes place after each execution of statement.

Examples:

5. for

The for statement allows for a controlled loop.

Syntax:

expression1 is evaluated before the first iteration. After each iteration, expression3 is evaluated. Both expression1 and expression3 may be ommited. If expression2 is ommited, it is assumed to be 1. statement is executed repeatedly until the value of expression2 is 0. The test on expression2 occurs before each execution of statement.

Examples:

Prints numbers 0 through 999.


Prints numbers 3 through 53. some_function is called 51 times.

6. continue

The continue statement can only appear in a loop body. It causes the rest of the statement body in the loop to be skipped.

Syntax:


Examples:


The numbers 0 through 99 are printed except for 50.


Each number from 0 to 999 is printed 100 times except for the number 500 which is not printed at all.

7. break

The break statement can only appear in a switch body or a loop body. It causes the execution of the current enclosing switch or loop body to terminate.

Syntax:


Examples:


If henry is equal to 2, nothing happens.


Only numbers 0 through 9 are printed.

8. return

The return statement causes the current function to terminate. It can return a value to the calling function. A return statement can not appear in a function whose return type is void. If the value returned has a type different from that of the function’s return type, then the value is converted. Using the return statement without an expression creates an undefined result. Reaching the } at the end of the function is the same as returning without an expression.

Syntax:


Examples:


 

Source: http://letsbytecode.com/programming/basic-programming-operators-in-java-php-c-and-more/

Syntax (programming languages) Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 10 Things to Know When Using SHACL With GraphDB
  • The Power of Docker Images: A Comprehensive Guide to Building From Scratch
  • What Is JavaScript Slice? Practical Examples and Guide
  • A Beginner’s Guide To Styling CSS Forms

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: