How to use if and for loops?
1 Direct conversion
2 Forced conversion
byte -- short
char
Left to right, direct conversion
Right to left, forced conversion
Boolean cannot be converted to other types
// Convert long to float directly (if the value of long is relatively large, convert it to float and use scientific notation to express it)
# float f1 = l1; ## Float f2 = 3.14f;
System.out.println(l2);
--------------------------------------------- ------
Classification of statements in Java
1 Sequential statements
Statements from front to back Execute
2 branch statement
Select the appropriate statement to execute. if, if/else, if/else if/else, switch/case
3 Loop statement
Loop execution of a certain piece of code (including multiple statements). while, do/while, for
------------------------------ -----------------------
How to write if statement
1 Only if no else
if(score >= 60) {
System.out.println("pass");
}
2 An if and an else
if(score >= 60) {
System.out.println("pass");
} else {
System.out.println("Failed");
}
3 if includes multiple else branches
if(score >= 90) {
System.out.println("Excellent");
} else if(score >= 80) {
System.out.println("Good");
} else if(score >= 70) {
System.out.println("medium");
} else if(score >= 60) {
System.out.println("passed");
} else {
System.out.println("failed" ");
}
Thinking: How to implement this code using switch...case...
------------------------------------------------ ---
switch case break default
Which data type is supported in switch
The standard is int, but also The following types can be supported
byte short int char String string
Note: boolean float double long
standard is not supported Syntax:
int value = 1;
## switch(value) {
case 1:
System.out.println("1");
break;
case 2:
System.out .println("2");
break;
default:
System.out.println("default");
break;
}
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Use the example of the above score To write switch case, it is
## switch(score/10) {
case 10:
case 9:
System.out.println("Excellent");
break;
case 8:## System.out .println("moderate");
break;
case 6:
System.out.println("pass") ;
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
System.out.println(" Failed");
default:
System.out.println("Exception score");
break;
}
---------------- -------------------------------------
while
Initialization;
while(condition) {
Loop statement;
Iteration;
}
It should be noted that the conditional statement in while must be of boolean type
// Initialization
int sum = 0;
int i = 0;
// Start of loop
while(i < 100) {
System .out.println("sum="+sum);
do...while
Initialization
do {
Iteration;
}while(condition);
// Initialization
int sum = 0;
int i = 0;
// Start of loop
do {
##
sum = sum + 1;
}while(i < 100);
for
for(initialization; condition; iteration) {
Loop statement
}
The various ways of writing for are as follows:
for The first way of writing
// Initialization
int sum = 0;
for(int i = 0; i < = 100; i++) {
sum = sum + i;
}
System.out.println("sum="+sum);
The second way of writing for. The initialization condition is external
int sum = 0;
int i = 0;
for(; i <= 100; i++) {
// Loop statement
sum = sum + i;
}
System.out.println("sum="+sum);
## The third way of writing for. Built-in judgment statement
// Initialization
int sum = 0;
for(int i = 0; ; i++) {
if(i > 100) {
break;
}
// Loop statement
sum = sum + i;
}
System.out.println("sum="+sum);
The fourth way of writing for. Iteration statement built-in
// Initialization
int sum = 0;
for(int i = 0; i <= 100 ; ) {
##
# i++;
}
System.out.println("sum="+sum);
The fifth way to write for. External initialization conditions, built-in judgment statements, and built-in iteration statements
int sum = 0;
int i = 0;
for(; ; ) {
// Judgment statement
if(i > 100) {
break; ##
i++;
}
System.out.println("sum="+ sum);
Thinking: Use a loop statement to print the following content
********
* ******
*****
****
***
**
*
hint:
System.out.println("*"); Prints a * sign without line breaks
System.out.print("*"); Prints a * sign without line breaks
System.out.println(); Line break
How many lines are printed? How many are printed per line?
The above is the detailed content of How to use if and for loops?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Lambda expression breaks out of the loop, specific code examples are needed. In programming, the loop structure is an important syntax that is often used. However, in certain circumstances, we may want to break out of the entire loop when a certain condition is met within the loop body, rather than just terminating the current loop iteration. At this time, the characteristics of lambda expressions can help us achieve the goal of jumping out of the loop. Lambda expression is a way to declare an anonymous function, which can define simple function logic internally. It is different from an ordinary function declaration,

Note: This article compares loops and recursion from the perspective of Go language. When writing programs, you often encounter situations where a series of data or operations need to be processed repeatedly. To achieve this we need to use loops or recursion. Loops and recursions are both commonly used processing methods, but in practical applications, they each have advantages and disadvantages, so the actual situation needs to be considered when choosing which method to use. This article will conduct a comparative study of loops and recursion in the Go language. 1. Loops A loop is a mechanism that repeatedly executes a certain piece of code. There are three main types of Go language

This article will explain in detail how PHP returns all the values of an array to form an array. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. Using the array_values() function The array_values() function returns an array of all the values in an array. It does not preserve the keys of the original array. $array=["foo"=>"bar","baz"=>"qux"];$values=array_values($array);//$values will be ["bar","qux"]Using a loop can Use a loop to manually get all the values of the array and add them to a new

Iterator interface The Iterator interface is an interface used to traverse collections. It provides several methods, including hasNext(), next() and remove(). The hasNext() method returns a Boolean value indicating whether there is a next element in the collection. The next() method returns the next element in the collection and removes it from the collection. The remove() method removes the current element from the collection. The following code example demonstrates how to use the Iterator interface to iterate over a collection: Listnames=Arrays.asList("John","Mary","Bob");Iterator

Replacement of recursive calls in Java functions with iteration In Java, recursion is a powerful tool used to solve various problems. However, in some cases, using iteration may be a better option because it is more efficient and less prone to stack overflows. Here are the advantages of iteration: More efficient since it does not require the creation of a new stack frame for each recursive call. Stack overflows are less likely to occur because stack space usage is limited. Iterative methods as an alternative to recursive calls: There are several methods in Java to convert recursive functions into iterative functions. 1. Use the stack Using the stack is the easiest way to convert a recursive function into an iterative function. The stack is a last-in-first-out (LIFO) data structure, similar to a function call stack. publicintfa

All programming languages are inseparable from loops. So, by default, we start executing a loop whenever there is a repeating operation. But when we are dealing with large number of iterations (millions/billions of rows), using loops is a crime. You might be stuck for a few hours, only to realize later that it doesn't work. This is where implementing vectorization in python becomes very critical. What is vectorization? Vectorization is a technique for implementing (NumPy) array operations on data sets. Behind the scenes, it applies the operation to all elements of the array or series at once (unlike a "for" loop that operates one row at a time). Next we use some use cases to demonstrate what vectorization is. Find the sum of numbers##Use the loop importtimestart

How to handle PHP loop nesting errors and generate corresponding error messages. During development, we often use loop statements to handle repeated tasks, such as traversing arrays and processing database query results. However, when using loop nesting, you sometimes encounter errors, such as infinite loops or too many nesting levels. This problem can cause server performance to degrade or even crash. In order to better handle such errors and generate corresponding error messages, this article will introduce some common processing methods and give corresponding code examples. 1. Use counters to

Difference: 1. for loops through each data element through the index, while forEach loops through the data elements of the array through the JS underlying program; 2. for can terminate the execution of the loop through the break keyword, but forEach cannot; 3. for can control the execution of the loop by controlling the value of the loop variable, but forEach cannot; 4. for can call loop variables outside the loop, but forEach cannot call loop variables outside the loop; 5. The execution efficiency of for is higher than forEach.
