Conditional and circular statements

Posted by Toneboy on Tue, 23 Nov 2021 09:14:25 +0100

Relational and logical operators

Relational operator

Logical operator

&&And: when both operands are true, the result is true
||Or: at least one of the two operands is true and the result is true
!Non: the operand is true and the result is false

Short circuit characteristics

  • The value of the expression can be derived from the value of the calculated left operand, and the value of the right operand will not be calculated
  • Division by 0 can be prevented

priority

  • Logical non > arithmetic operators > relational operators > logical and > logical or
  • You can also increase the priority by adding parentheses

Conditional statement

Single branch selection structure (if statement)

  • structure
if(expression P)
	sentence A;

Double branch structure (if else)

if(expression P)
{
	sentence A;
}
else
{
	sentence B;
}
  • Multiple statements can be expressed in curly braces

Multi branch structure (else if)

if(Expression 1)
{
	sentence A;
}
else if(Expression 2)
{
	sentence B;
}
else
{
	sentence C;
}

Conditional expression

  • Expression 1? Expression 2: expression 3;
  • Meaning: when expression 1 is true, the value of expression 2 is output; otherwise, the value of expression 3 is output

Switch statement

switch Statements

switch(expression)//The expression is of type int or char
{
	case Constant 1:
	Statement sequence 1;
	break;//break jumps out of switch statement structure
	case Constant 2:
	Statement sequence 2;
	break;
	......
	default://When none of the above case s can be executed, execute this statement
	Statement sequence n;
}

Numerical overflow and loss problems

numeric overflow

  • Problems with automatic type conversion
  • Value overflow occurs when a type with a large value range is converted to a type with a small value range
  • Limited range of type tables

Integer value overflow

Overflow

  • The maximum number that a numeric operation knot > type can represent
  • Carry loss occurs when the carry exceeds the highest bit
  • The carry reaches the highest bit and changes the sign bit

Floating point value overflow

Overflow

  • Floating point operation result > maximum number that type can represent

underflow

  • Floating point arithmetic structure < the minimum number that can be represented by type

Accuracy loss

  • Precision loss occurs when converting from high precision to low precision
  • float to int (missing decimal part)
  • There are many non one-to-one correspondence between binary decimals and decimal decimals
  • float can represent 7 significant digits and double can represent 16 significant digits

Cycle control

Counting control cycle

for statement

  • Mainly used for counting cycles
for(Expression 1;Expression 2;Expression 3)//Expression 1 initial condition, expression 2 control condition, expression 3 conversion condition
{
	Statement 1;
	sentence n;
}
//Execution order: expression 1, expression 2, statement, expression 3

Comma Operator

Expression 1, expression 2,..., expression n

for(Expression 1,expression n;Expression 2;Expression 3,expression m)//Comma expressions are mainly used in loop statements to assign initial values to multiple variables at the same time
{
	Statement 1;
	sentence n;
}

While and do while statements

while statement

Expression 1;//Expression 1 loop initial condition
while(Expression 2)//Expression 2 loop control condition
{
	sentence;
	Expression 3;//Expression 3 loop conversion condition
}
//Execution sequence: from top to bottom
  • Current cycle
  • The number of cycles is known

do while statement

Expression 1;//Expression 1 loop initial condition
	do{
		sentence;
		Expression 3;//Expression 3 loop conversion condition
	}while(Expression 2);//Expression 2 loop control condition. When the expression holds, the loop continues
  • The loop is executed at least once
  • Until type cycle
  • The number of cycles may not be known

General principles for selecting circular statements

The number of cycles is knownfor statement
The number of cycles is unknown, and the number of cycles is controlled by a given conditionWhile statement and do while statement
The loop body needs to be executed oncedo-while Statements

Recurrence

  1. Forward recurrence
  2. Reverse thrust

Exhaustive method

  • List all the possibilities and test them one by one

  • The range of solution is estimated according to some known conditions of the problem

  • Until the solution satisfying the known conditions is found

Bug

Common program errors

  1. Compilation error: syntax error
  2. Link error: missing include file, etc
  3. Runtime error: error running result

Debugging method

  1. Reverse reasoning: locating approximate range
  2. Divide and Conquer: cut off some code with comments and open it after debugging
  3. Reduce input: reduce input data

Exit loop statement function

goto Statement

  1. Unconditional turn statement
  2. Jump out of multiple loops
  3. Jump out of a common exit position
  4. Do not label too many gotu statements
  5. Title: how many soldiers does Han Xin ask for
#include <stdio.h>
int main()
{
	int x;
	for(x=1;;x++)
	{
		if(x%5==1 && x%6==5 && x%7==4 && x%11==10)
		{
			printf("x=%d\n",x );
			goto END;//Unconditional turn statement
		}
	}
	END: ;//There must be a statement after the identification (identifier), even if it is an empty statement
	return 0;
}

break statement

  • Can be used to jump out of loop statements
  • Jump out to the first statement immediately after the loop statement
#include <stdio.h>
int main()
{
	int x;
	for(x=1;;x++)
	{
		if(x%5==1 && x%6==5 && x%7==4 && x%11==10)
		{
			printf("x=%d\n",x );
			break;
		}
	}
	return 0;
}

exit() function

  • Add #include < stdlib. H > at the beginning

  • Terminate the whole program. When the parameter is 0, the table program exits normally; Non 0 indicates abnormal exit

    #include <stdio.h>
    #include<stdlib.h>
    int main()
    {
    	int x;
    	for(x=1;;x++)
    	{
    		if(x%5==1 && x%6==5 && x%7==4 && x%11==10)
    		{
    			printf("x=%d\n",x );
    			exit(0);
    		}
    	}
    	return 0;
    }
    

continue Statement

  • Continue to loop this statement
  • But he has to skip the statement after continue before executing the loop until the loop ends
#include <stdio.h>
int main()
{
	int i,n;
	for(i=1;i<5;i++)
	{
		printf("Please enter n:\n");
		scanf("%d",&n);
		if(n<0)
		{
			continue;//If the conditions are met, continue the cycle and replace it with break for comparison
			printf("n=%d\n",n );
		}
	}
	printf("Program is over!\n");
	return 0;
}

Topics: C